forked from pimbongaerts/radseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipyrad_include.py
More file actions
executable file
·46 lines (40 loc) · 1.62 KB
/
ipyrad_include.py
File metadata and controls
executable file
·46 lines (40 loc) · 1.62 KB
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
#!/usr/bin/env python
"""
Reduces ipyrad file to only those samples listed in supplied text file.
"""
import sys
import argparse
__author__ = 'Pim Bongaerts'
__copyright__ = 'Copyright (C) 2016 Pim Bongaerts'
__license__ = 'GPL'
def main(loci_filename, list_filename, min_samples):
# Read list from file and convert to set (unique values only)
with open(list_filename) as file:
lines = [line.strip() for line in file]
samples = set(lines)
loci_file = open(loci_filename, 'r')
seqs_to_output = []
for line in loci_file:
if line[0] != '/':
# Evaluate if sample in list of samples
if line.split()[0] in samples:
seqs_to_output.append(line.strip())
elif line[0] == '/' and len(seqs_to_output) >= int(min_samples):
# Output locus info if sequences were outputted
for seq in seqs_to_output:
print(seq)
print(line.strip())
seqs_to_output = []
else:
seqs_to_output = []
loci_file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('loci_filename', metavar='loci_file',
help='samples input file')
parser.add_argument('inclusion_filename', metavar='inclusion_file',
help='text file with names of samples to be included')
parser.add_argument('min_samples', metavar='min_samples',
help='minimum number of samples for locus to be included')
args = parser.parse_args()
main(args.loci_filename, args.inclusion_filename, args.min_samples)