-
Notifications
You must be signed in to change notification settings - Fork 18
/
PhylipOrderedFasta.py
executable file
·51 lines (43 loc) · 1.4 KB
/
PhylipOrderedFasta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Order a multifasta by the order of sequence names in a phylip
def main():
"""Order a fasta by the corresponding order of names in a phylip"""
import argparse
try:
parser = argparse.ArgumentParser(description='Output a multifasta ordered by an input file (phylip alignment).')
parser.add_argument(
'-f',
'--fasta',
action='store',
required=True,
help='The multifasta to re-rorder.')
parser.add_argument(
'-p',
'--phylip',
action='store',
required=True,
help='The phylip file which specifies the order of the sequences.')
parser.add_argument(
'-o',
'--outfile',
action='store',
default=None,
help='Output file to store the new fasta sequences in. Just prints to screen by default.')
parser.add_argument(
'-v',
'--verbose',
action='store_true',
help='Set whether to print the key list out before the fasta sequences. Useful for debugging.')
args = parser.parse_args()
except:
print('An exception occured with argument parsing. Check your provided options.')
traceback.print_exc()
from Bio import AlignIO, SeqIO
align = AlignIO.read(args.phylip,'phylip')
fasta_dict = SeqIO.to_dict(SeqIO.parse(args.fasta, 'fasta'))
phynames = [rec.id for rec in align]
for key in phynames:
print(fasta_dict[key].format('fasta'))
if outfile is not None:
SeqIO.write(fasta_dict[key], args.outfile, "fasta")
if __name__ == '__main__':
main()