-
Notifications
You must be signed in to change notification settings - Fork 9
/
07g_count_progenators.py
280 lines (239 loc) · 9.9 KB
/
07g_count_progenators.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#-----------------------------------------------------------+
# |
# template.py - Python template |
# |
#-----------------------------------------------------------+
# |
# AUTHOR: Vikas Gupta |
# CONTACT: [email protected] |
# STARTED: 09/06/2013 |
# UPDATED: 09/06/2013 |
# |
# DESCRIPTION: |
# Short script to convert and copy the wheat BACs |
# Run this in the parent dir that the HEX* dirs exist |
# |
# LICENSE: |
# GNU General Public License, Version 3 |
# http://www.gnu.org/licenses/gpl.html |
# |
#-----------------------------------------------------------+
# Example:
# python ~/script/python/100b_fasta2flat.py -i 02_Stegodyphous_cdna.refined.fa.orf.tr_longest_frame
### import modules
import os,sys,getopt, re
import threading
from multiprocessing import Process, Queue, Manager
from threading import Thread
import classGene
### global variables
global infile
### make a logfile
import datetime
now = datetime.datetime.now()
o = open(str(now.strftime("%Y-%m-%d_%H%M."))+'logfile','w')
def get_size(file):
size = {}
for line in open(file,'r'):
line = line.strip()
if len(line) > 0:
if line[0] != '#':
token = line.split('\t')
if token[0] not in size:
size[token[0]] = int(token[4])
else:
if int(token[4]) > size[token[0]]:
size[token[0]] = int(token[4])
size_sorted={}
for w in sorted(size, key=size.get, reverse=False):
size_sorted[w]=size[w]
return size_sorted
### write logfile
def logfile(infile):
o.write("Program used: \t\t%s" % "100b_fasta2flat.py"+'\n')
o.write("Program was run at: \t%s" % str(now.strftime("%Y-%m-%d_%H%M"))+'\n')
o.write("Infile used: \t\t%s" % infile+'\n')
def help():
print '''
python 100b_fasta2flat.py -i <infile>
'''
sys.exit(2)
def temp(file):
num_lines = sum(1 for line in open(file))
return num_lines
### main argument to
def options(argv):
global infile, threads, bam, contig_len
infile = ''
threads = 2
try:
opts, args = getopt.getopt(argv,"hi:t:b:l:",["infile=","threads=","bam=","len="])
except getopt.GetoptError:
help()
for opt, arg in opts:
if opt == '-h':
help()
elif opt in ("-i", "--infile"):
infile = arg
elif opt in ("-t", "--threads"):
threads = int(arg)
elif opt in ("-b", "--bam"):
bam = arg
elif opt in ("-l", "--len"):
contig_len = arg
logfile(infile)
def hash_st():
HASH_ST = {}
first_line = True
for line in open(infile,'r'):
line = line.strip()
tokens = line.split()
contig = tokens[0]
st = int(tokens[1])
en = int(tokens[2])
g_id = tokens[3].replace('id=','')
score = tokens[4]
strand = tokens[5]
if first_line == False and contig == last_contig:
if last_g_id[0] != g_id:
HASH_ST[last_contig, last_g_id, last_st] = st
last_contig = contig
last_st = st
last_en = en
last_g_id = g_id
first_line = False
return HASH_ST
def hash_map():
HASH_MAP = {}
for line in open(bam,'r'):
line = line.strip()
tokens = line.split('\t')
g_id = tokens[0].replace('ID=','')
if (tokens[1], g_id) not in HASH_MAP:
HASH_MAP[tokens[1], g_id] = int(tokens[3])
else:
if HASH_MAP[tokens[1], g_id] < int(tokens[3]):
HASH_MAP[tokens[1], g_id] = int(tokens[3])
return HASH_MAP
def hash_len():
HASH_LEN = {}
for line in open(contig_len, 'r'):
line = line.strip()
tokens = line.split('\t')
HASH_LEN[tokens[0]] = tokens[1]
return HASH_LEN
def count_prog(HASH_ST, HASH_MAP, HASH_LEN):
first_line = True
first_model = True
occi_bp = 0
palle_bp = 0
No_switch = 0
Switch = 0
Over_prog = 0
Non_over_prog = 0
last_contig = 'temp'
last_g_id = 'temp'
last_en = 1000000000000
first_frag = True
switches = ''
bp_coords = ''
print 'Contig\tContig_len\tOcci_bases\tPalle_bases\tOcci_fraction\tNo_switch\tSwitch\tOverlapping_progenators\tNon-Overlapping_progenators\tswitches\tBreakpoints'
for line in open(infile,'r'):
line = line.strip()
tokens = line.split()
contig = tokens[0]
st = int(tokens[1])
en = int(tokens[2])
g_id = tokens[3].replace('id=','')
score = tokens[4]
strand = tokens[5]
if first_line == False and contig != last_contig:
if switches == '':
bp_coords = ''
if last_g_id == 'temp':
switches = last_temp_id
else:
switches = last_g_id
if occi_bp > 0 or palle_bp > 0:
print last_contig + '\t' + str(HASH_LEN[last_contig]) + '\t' + str(occi_bp) + '\t' + str(palle_bp) + '\t' + str(round(float(occi_bp)/(occi_bp+palle_bp),3)) + '\t' + str(No_switch) + '\t' + str(Switch) + '\t' + str(Over_prog) + '\t' + str(Non_over_prog) + '\t' + str(switches) + '\t' + str(bp_coords)
occi_bp = 0
palle_bp = 0
No_switch = 0
Switch = 0
Over_prog = 0
Non_over_prog = 0
first_frag = True
first_model = True
switches = ''
bp_coords = ''
last_g_id = 'temp'
last_en = 1000000000000
if en - st > 1000:
if re.search('occi',line):
occi_bp += en - st
if re.search('palle',line):
palle_bp += en - st
if contig == last_contig and first_frag == False:
### Count the switch/Non-switch
#print last_g_id, g_id
if (last_g_id[0:4] != g_id[0:4]) and (st > last_en) and (last_g_id != 'temp'):
if (contig, g_id, st) in HASH_ST:
if HASH_ST[contig, g_id, st] > en:
### check for mapping quality
max_map = 0
gene_id_tokens = g_id.split('_')
for i in range(len(gene_id_tokens)/2):
gene_id = gene_id_tokens[2*i] + '_' + gene_id_tokens[2*i+1]
if HASH_MAP[contig, gene_id] > max_map:
max_map = HASH_MAP[contig, gene_id]
if max_map > 100:
#print 'accepted: ' + last_g_id +'_'+ g_id
Switch += 1
last_en = en
switches += last_g_id + '_' + g_id
bp_coords += contig + '_' + str(st) + '/'
last_g_id = g_id
if last_g_id[0] == g_id[0]:
No_switch += 1
last_en = en
last_g_id = g_id
#print last_g_id[0] , g_id[0], No_switch, Switch
### Count overlapping fragments
if st < last_en:
Over_prog += 1
else:
Non_over_prog += 1
if first_frag == True:
if (contig, g_id, st) in HASH_ST:
if HASH_ST[contig, g_id, st] > en:
### check for mapping quality
max_map = 0
gene_id_tokens = g_id.split('_')
for i in range(len(gene_id_tokens)/2):
gene_id = gene_id_tokens[2*i] + '_' + gene_id_tokens[2*i+1]
if HASH_MAP[contig, gene_id] > max_map:
max_map = HASH_MAP[contig, gene_id]
if max_map > 100:
last_g_id = g_id
first_frag = False
last_contig = contig
last_temp_id = g_id
first_line = False
if last_g_id == 'temp':
switches = last_temp_id
else:
switches = last_g_id
if occi_bp > 0 or palle_bp > 0:
print last_contig + '\t' + str(HASH_LEN[last_contig]) + '\t' + str(occi_bp) + '\t' + str(palle_bp) + '\t' + str(round(float(occi_bp)/(occi_bp+palle_bp),3)) + '\t' + str(No_switch) + '\t' + str(Switch) + '\t' + str(Over_prog) + '\t' + str(Non_over_prog) + '\t' + str(switches) + '\t' + str(bp_coords)
if __name__ == "__main__":
options(sys.argv[1:])
start_time = datetime.datetime.now()
print >> sys.stderr, "Running temp script: " + str(datetime.datetime.now())
print >> sys.stderr, "Input count: " + str(temp(infile))
HASH_ST = hash_st()
HASH_MAP = hash_map()
HASH_LEN = hash_len()
count_prog(HASH_ST, HASH_MAP, HASH_LEN)
print >> sys.stderr, "Output count: " + str(temp(infile))
print >> sys.stderr, "Completed temp script: " + str(datetime.datetime.now())
print >> sys.stderr, "Time take to complete: " + str(datetime.datetime.now() - start_time)