Skip to content

Commit d5cb639

Browse files
authored
Merge pull request #44 from PoonLab/dev
fixes to python interface
2 parents 8395c06 + c6ae2db commit d5cb639

3 files changed

Lines changed: 39 additions & 22 deletions

File tree

bin/openrdp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ if __name__ == "__main__":
1515
)
1616
parser.add_argument('infile',
1717
help='File containing sequence alignment (FASTA or CLUSTAL) format')
18-
parser.add_argument('outfile', help='Path to the output file')
18+
parser.add_argument('-o', '--outfile', type=argparse.FileType('w'),
19+
default=None, help='Path to write CSV output')
1920
parser.add_argument('-c', '--cfg', type=str,
2021
help='Path to file that contains parameters. Defaults to '
2122
'default.ini.',
@@ -36,7 +37,8 @@ if __name__ == "__main__":
3637
if args.methods is None:
3738
args.methods = all_methods # run all methods by default
3839

39-
results = openrdp.openrdp(args.infile, args.outfile, cfg=args.cfg,
40-
methods=tuple(args.methods), quiet=args.quiet)
40+
results = openrdp.openrdp(args.infile, cfg=args.cfg, methods=tuple(args.methods),
41+
quiet=args.quiet)
4142
print(results) # calls custom __str__
42-
43+
if args.outfile:
44+
results.write(args.outfile)

openrdp/__init__.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from itertools import combinations
66
from glob import glob
77

8+
from openrdp import __path__ as basepath
89
from openrdp.bootscan import Bootscan
910
from openrdp.chimaera import Chimaera
1011
from openrdp.common import generate_triplets, Triplet
@@ -63,32 +64,41 @@ def __str__(self):
6364
f"{e[1][0]:<7}\t{e[1][1]:<7}\t{float(e[4]):.2E}\n"
6465
return outstr
6566

67+
def __getitem__(self, key):
68+
events = self.dict.get(key, None)
69+
if key == 'geneconv':
70+
return [{'start': e[2][0], 'end': e[2][1], 'recombinant': e[0],
71+
'parent1': e[1][0], 'parent2': e[1][1], 'pvalue': float(e[3])}
72+
for e in events]
73+
else:
74+
return [{'start': e[2], 'end': e[3], 'recombinant': e[0],
75+
'parent1': e[1][0], 'parent2': e[1][1], 'pvalue': float(e[4])}
76+
for e in events]
77+
78+
def keys(self):
79+
return self.dict.keys()
80+
6681

6782
class Scanner:
68-
def __init__(self, names, infile, outfile, cfg=None, methods=None,
83+
def __init__(self, names, infile, cfg=None, methods=None,
6984
quiet=False):
7085
"""
7186
:param names: list, sequence labels
7287
:param infile: str, path to input FASTA
73-
:param outfile: str, path to write output CSV
7488
:param cfg: str, path to configuration file. Defaults to None, causing
7589
each method to use default settings.
7690
:param methods: tuple, names of methods to run
7791
:param quiet: bool, if True, suppress console messages
7892
"""
7993
self.seq_names = names
8094
self.infile = infile
81-
82-
self.config = None
83-
self.cfg_file = cfg
84-
if self.cfg_file:
85-
print(f"Loading configuration from {cfg}")
86-
self.config = configparser.ConfigParser()
87-
self.config.read(self.cfg_file)
88-
8995
self.methods = methods
9096
self.quiet = quiet
91-
self.outfile = outfile
97+
98+
self.config = configparser.ConfigParser()
99+
self.cfg_file = cfg
100+
self.print(f"Loading configuration from {self.cfg_file}")
101+
self.config.read(self.cfg_file)
92102

93103
def print(self, msg):
94104
""" Implements self.quiet """
@@ -277,11 +287,10 @@ def read_fasta(handle):
277287
return headers, seqs
278288

279289

280-
def openrdp(infile, outfile, cfg=None, methods=None, quiet=False):
290+
def openrdp(infile, cfg=None, methods=None, quiet=True):
281291
"""
282292
Main function
283293
:param infile: str, path to input FASTA file
284-
:param outfile: str, path to write output CSV
285294
:param methods: list, names of methods to run
286295
:param quiet: bool, if True, suppress console messages
287296
:return: dict, results from each method
@@ -306,6 +315,10 @@ def openrdp(infile, outfile, cfg=None, methods=None, quiet=False):
306315
if not valid_alignment(aln) and not valid_chars(aln):
307316
sys.exit(1)
308317

309-
scanner = Scanner(names, infile, outfile, cfg=cfg, methods=methods, quiet=quiet)
318+
if cfg is None:
319+
# load default configuration from package file
320+
cfg = os.path.join(basepath[0], 'default.ini')
321+
322+
scanner = Scanner(names, infile, cfg=cfg, methods=methods, quiet=quiet)
310323
results = scanner.run_scans(aln)
311324
return results

openrdp/bootscan.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ def __init__(self, alignment, win_size=200, step_size=20, use_distances=True,
3131
self.align = alignment
3232
random.seed(self.random_seed)
3333
np.random.seed(self.random_seed)
34-
if not quiet:
34+
35+
self.quiet = quiet
36+
if not self.quiet:
3537
print('Starting Scanning Phase of Bootscan/Recscan')
3638

3739
self.do_scanning_phase(alignment)
38-
39-
if not quiet:
40+
if not self.quiet:
4041
print('Finished Scanning Phase of Bootscan/Recscan')
4142

4243
self.raw_results = []
@@ -166,7 +167,8 @@ def execute(self, arg):
166167
raw_results = []
167168
triplet = Triplet(self.align, self.seq_names, trp)
168169

169-
print("Scanning triplet {} / {}".format(i, self.total_triplet_combinations))
170+
if not self.quiet:
171+
print("Scanning triplet {} / {}".format(i, self.total_triplet_combinations))
170172

171173
# Look at boostrap support for sequence pairs
172174
ab_support = [0] * (self.align.shape[1] // self.step_size)

0 commit comments

Comments
 (0)