forked from sc-zhang/bioscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatAgpDetail.py
executable file
·57 lines (52 loc) · 1.67 KB
/
StatAgpDetail.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 python3
import sys
def stat_agp(in_agp, out_csv):
asm_db = {}
unanc_cnt = 0
unanc_len = 0
anc_cnt = 0
anc_len = 0
gap_cnt = 0
gap_len = 0
with open(in_agp, 'r') as fin:
for line in fin:
data = line.strip().split()
if data[4] == 'U':
gap_cnt += 1
gap_len += 100
else:
chrn = data[0]
if chrn[:3] != 'tig':
allele = chrn[-1]
chrn = chrn[:-1]
if chrn not in asm_db:
asm_db[chrn] = {}
if allele not in asm_db[chrn]:
asm_db[chrn][allele] = {'cnt': 0, 'len': 0}
asm_db[chrn][allele]['cnt'] += 1
asm_db[chrn][allele]['len'] = int(data[2])
anc_cnt += 1
anc_len += int(data[7])
else:
unanc_cnt += 1
unanc_len += int(data[2])
for chrn in asm_db:
break
with open(out_csv, 'w') as fout:
fout.write(",%s\n"%(',,'.join(sorted(asm_db[chrn]))))
for chrn in sorted(asm_db):
info = [chrn]
for allele in sorted(asm_db[chrn]):
info.append("\"%s\""%("{:,}".format(asm_db[chrn][allele]['cnt'])))
info.append("\"%s\""%("{:,}".format(asm_db[chrn][allele]['len'])))
fout.write("%s\n"%(','.join(info)))
fout.write("Anchored contigs,\"%s\",\"%s\"\n"%("{:,}".format(anc_cnt), "{:,}".format(anc_len/1e6)))
fout.write("Unanchored contigs,\"%s\",\"%s\"\n"%("{:,}".format(unanc_cnt), "{:,}".format(unanc_len/1e6)))
fout.write("Gaps,\"%s\",\"%s\"\n"%("{:,}".format(gap_cnt), "{:,}".format(gap_len/1e6)))
fout.write("Total,\"%s\",\"%s\"\n"%("{:,}".format(anc_cnt+unanc_cnt), "{:,}".format((anc_len+unanc_len)/1e6)))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python %s <in_agp> <out_csv>"%sys.argv[0])
else:
in_agp, out_csv = sys.argv[1:]
stat_agp(in_agp, out_csv)