-
Notifications
You must be signed in to change notification settings - Fork 7
/
statistic_rslt.py
173 lines (140 loc) · 5 KB
/
statistic_rslt.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys
import os
from subprocess import *
from multiprocessing import Pool
from Bio import SeqIO
def run_statistic(id):
#sf_picked="./picked/{0}.fa".format(id)
#sf_true_seq="./only_gap_seqs/{0}.fa".format(id)
sf_true_seq="./picked/{0}.fa".format(id)
sf_picked="./../only_gap_seqs/{0}.fa".format(id)
if os.path.exists(sf_picked)==False or os.path.exists(sf_true_seq)==False:
return
cmd="bwa index {0}".format(sf_picked)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="bwa mem {0} {1} | samtools view -h -b -S - | samtools sort - -o ./alignment/{2}.sort.bam".format(sf_picked,sf_true_seq,id)
Popen(cmd, shell = True, stdout = PIPE).communicate()
cmd="samtools view ./alignment/{0}.sort.bam > ./alignment/{1}.sam".format(id,id)
Popen(cmd, shell = True, stdout = PIPE).communicate()
def do_statistic(sf_fai, sf_gap_pos, n, sf_picked):
m_scaffold_id={}
m_gap_length={}
cnt=0
with open(sf_fai) as fin_fai:
for line in fin_fai:
fields=line.split()
scaffold_id=fields[0]
m_scaffold_id[scaffold_id]=cnt
cnt=cnt+1
fq_list=[]
cnt=1
pre_id=0
with open(sf_gap_pos) as fin_gap_pos:#for each gap
for line in fin_gap_pos:
fields=line.split()
scaffold=fields[3]
scaffold_id=m_scaffold_id[scaffold]
if pre_id!=scaffold_id:
cnt=1
sf_fq="{0}_{1}".format(scaffold_id,cnt)
m_gap_length[sf_fq]=fields[2] ####################################save gap length
cnt=cnt+1
pre_id=scaffold_id
fq_list.append(sf_fq)
#return m_gap_length #########################################################################################
for record in SeqIO.parse(sf_picked, "fasta"):
s_id=str(record.id)
fields=s_id.split("_")
s_new_id=fields[0]+"_"+fields[1]
s_p="./picked/{0}.fa".format(s_new_id)
with open(s_p,"w") as fout_picked:
fout_picked.write(">"+s_id+"\n")
fout_picked.write(str(record.seq)+"\n")
pool = Pool(n)
pool.map(run_statistic, fq_list)
pool.close()
pool.join()
cmd="cat ./alignment/*.sam > hit.sam"
Popen(cmd, shell = True, stdout = PIPE).communicate()
return m_gap_length
def is_qualified_clipped(cigar, cutoff_len):
l=len(cigar)
signal=[]
lenth=[]
temp=""
for i in range(l):
if cigar[i]>="0" and cigar[i]<="9":
temp=temp+cigar[i]
else:
signal.append(cigar[i])
lenth.append(int(temp))
temp=""
if (signal[0]=="S" or signal[0]=="H") and lenth[0]>=cutoff_len:
return True
if (signal[len(signal)-1]=="S" or signal[len(signal)-1]=="H") and lenth[len(signal)-1]>=cutoff_len:
return True
return False
def cnt(sf_algn, m_gap_length):
cnt=0
m_id={}
m_hit={}
with open(sf_algn) as fin_algn:
for line in fin_algn:
fields=line.split()
rname=fields[0]
m_id[rname]=1
cigar=fields[5]
if cigar=="*":
continue
if is_qualified_clipped(cigar,20) ==False:
m_hit[fields[2]]=0
#cnt=cnt+1
#print rname, fields[2]
print len(m_hit),len(m_id)
with open("hit_list.txt", "w") as fout_hit_list:
for id in m_hit:
fout_hit_list.write(id+"\n")
with open("closed_gap_length.txt", "w") as fout_gap_lenth:
fout_gap_lenth.write("[") #################################################
for id in m_hit:
fields=id.split("_")
fout_gap_lenth.write(m_gap_length[fields[0]+"_"+fields[1]]+",")
fout_gap_lenth.write("0]") #################################################
def output_hit_list(sf_algn):
cnt=0
m_id={}
m_hit={}
with open(sf_algn) as fin_algn:
for line in fin_algn:
fields=line.split()
rname=fields[0]
m_id[rname]=1
cigar=fields[5]
if cigar=="*":
continue
if is_qualified_clipped(cigar,20) ==False:
m_hit[fields[2]]=0
#cnt=cnt+1
#print rname, fields[2]
#print len(m_hit),len(m_id)
with open("hit_list.txt", "w") as fout_hit_list:
for id in m_hit:
fout_hit_list.write(id+"\n")
if __name__ == "__main__":
# sf_fai=sys.argv[1]
# sf_pos=sys.argv[2]
# n_jobs=int(sys.argv[3])
# sf_picked=sys.argv[4]
#
# sf_temp="./alignment"
# if os.path.exists(sf_temp)==False:
# cmd="mkdir {0}".format(sf_temp)
# Popen(cmd, shell = True, stdout = PIPE).communicate()
# sf_temp="./picked"
# if os.path.exists(sf_temp)==False:
# cmd="mkdir {0}".format(sf_temp)
# Popen(cmd, shell = True, stdout = PIPE).communicate()
#m_gap_length=do_statistic(sf_fai, sf_pos, n_jobs, sf_picked)
#cnt(sys.argv[1])
#cnt("hit.sam", m_gap_length)
output_hit_list(sys.argv[1])