-
Notifications
You must be signed in to change notification settings - Fork 6
/
count_geno.py
44 lines (37 loc) · 1.19 KB
/
count_geno.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Calculate the number of genotypes per locus
'''
import argparse
import gzip
#count_work.sh
#sed -i 's/\t/ /g' $1;
#cut -f 1,2,3 -d ' ' $1 >head_geno.txt;
#cut -f 1,2,3 --complement -d ' ' $1 >bady_geno.txt;
#python count_geno.py -i bady_geno.txt >count.txt;
#paste head_geno.txt count.txt >result.txt
def count_geno():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-i", "--input", action="store", dest="filename", required=True,help="Name of genotype(generated by iTools)")
args = parser.parse_args()
filename = args.filename
pattern = {'-':'--','A':'AA','T':'TT','C':'CC','G':'GG','K':'GT','M':'AC','R':'AG','S':'CG','Y':'CT','W':'AT'}
if filename.endswith('gz'):
opener = gzip.open
else:
opener = open
with opener(filename,'rt') as IN:
for i in IN:
geno= []
if '#' in i:
pass
else:
geno = [base for base in i.strip('\n').split(' ')]
tmp = [pattern[x] if x in pattern else x for x in geno]
set_geno = sorted(set(tmp))
for e in set_geno:
print(e,tmp.count(e),end=' ')
print("\n",end='')
if __name__ == "__main__":
count_geno()