-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpyrad2fasta.py
executable file
·57 lines (51 loc) · 1.96 KB
/
pyrad2fasta.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
52
53
54
55
56
57
#!/usr/bin/env python
"""
Create FASTA file with a representative sequence (using first sample) or
all sequences (when --all_seqs flag is set) for each locus in pyRAD/ipyrad
`.loci` or `.allele` file.
"""
import sys
import argparse
__author__ = 'Pim Bongaerts'
__copyright__ = 'Copyright (C) 2016 Pim Bongaerts'
__license__ = 'GPL'
def main(input_filename, all_seqs):
input_file = open(input_filename, 'r')
current_seqlength = 0
current_sequence = ''
sequences_in_loci = {}
for line in input_file:
if line[0] == '/':
# Retrieve locus number
cols = line.split('|')
locus_number = cols[1].strip()
# Write sequence(s) to output file
if all_seqs:
for sample in sorted(sequences_in_loci):
print('>{0}_{1}'.format(locus_number, sample))
print('{0}'.format(sequences_in_loci[sample]))
sequences_in_loci.clear()
else:
print('>{0}'.format(locus_number))
print('{0}'.format(current_sequence))
current_sequence = ''
else:
# Retrieve sequence
cols = line.split()
sequence = cols[1].strip()
if all_seqs:
# Store all sequences in dict
sample = cols[0].replace('>', '')
sequences_in_loci[sample] = sequence
elif current_sequence == '':
# Store first sequence in locus
current_sequence = sequence
input_file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input_filename', metavar='pyrad_file',
help='PyRAD allele file (`.loci` or `.allele`)')
parser.add_argument('-a', '--all_seqs', action='store_true',
help='set flag to output all sequences')
args = parser.parse_args()
main(args.input_filename, args.all_seqs)