-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_matrix_annotate.py
406 lines (229 loc) · 7.24 KB
/
s_matrix_annotate.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/env python3
##Author: JooHee Choi, [email protected], used by Erin Newcomer for ICU Sink project.
##this script will take SNP matrices and write new matrices with gene names. It will also output each mutation per strain.
##usage: python3 s_matrix_annotate.py > output_file_name
strain_dict={} ##'fixed' matrix names: gff file
strain_matrix=[] #'fixed' matrix names
matrices=[] #list containing all matrix file names ex) DK-001_1
gff=[] #list containing gff file names ex) xyz.gff
matrices=[] #list of patient matrices ex) DK-001_1
def main():
snp_m=open('patients.txt', 'r') # list of _merged_parsed_txt file names
gff_f=open('refgff.txt', 'r') # list of reference gff files _.gff
for line in snp_m:
matrices.append(line.strip()) ##?? the numbers will get rid of '_merged_parsed_txt'
for line in gff_f:
gff.append(line.strip())
snp_m.close()
gff_f.close()
n =len(matrices)
for j in range(n):
temp=matrices[j] ###patient name
strain_dict[temp]=gff[j]
strain_matrix.append(temp)
###function to match node list to gff file
def matchnodes(patient):
########get list of nodes
matrix_file='snp_matrix/'+ patient+'_merged_parsed.txt'
mfile=open(matrix_file, 'r')
nodelist=[]
parsednodes=[]
first='T'
for line in mfile:
if first=='T':
nodelist=line.strip().split('\t')
for i in nodelist:
node=i.split('_')[0]
pos=i.split('_')[1]
code=node+'@'+pos ##add in something that's easy to parse
parsednodes.append(code)
first='F'
mfile.close()
return parsednodes
def matchgff(name, org):
gff_name=strain_dict[name] #need to open gff file
gff_name='gff/'+str(gff_name)
gff_lib={} ##node:[[start,end,gene],[start,end,gene],....,]
gff_file=open(gff_name, 'r')
for line in gff_file:
if (line[0]!='#' and line[0]!='>' and line[0]!='A' and line[0]!='T' and line[0]!='G' and line[0]!='C'):
line_list=line.strip().split('\t')
info=str(line_list[8])
if 'gene=' in info:
temp=info.split('gene=')[1]
gene=temp.split(';')[0]
else:
gene='hypothetical protein'
temp=[]
node=line_list[0]
temp.append(line_list[3])
temp.append(line_list[4])
temp.append(gene) ##location of 'gene' in info tab too inconsistent, more over some don't even have it
if node not in gff_lib:
gff_lib[node]=[]
gff_lib[node].append(temp)
else:
gff_lib[node].append(temp)
gff_file.close()
return gff_lib
##final function to mach between node LIST and node LIBRARY
def nodestogenes(nodes, gfflib):
newdict={} #node_pos: gene to be used to translate matrix
for i in nodes:
split_list=i.split('@')
node=split_list[0]
pos=int(split_list[1])
repair=node+'_'+str(pos) ##original form for dictionary
if node in gfflib:
list_pos=gfflib[node] ##list of [start, end, gene], [start, end, gene],
m=len(list_pos)
gene=''
n=0
for k in range(m):
if int(list_pos[k][0]) < pos and int(list_pos[k][1]) > pos: # any hit
n+=1
if n==1: ##first hit
gene+=str(list_pos[k][2])
else:
gene=gene+','+str(list_pos[k][2])
if gene=='': #because no matches were found at all
#temp='intron'
gene='intron'
"""
for j in range(1000):
pos2=pos+j
pos3=pos-j
if int(list_pos[k][0]) < pos2 and int(list_pos[k][1]) > pos2: # any hit
n+=1
#m+=1 ##do wee need this???
if n==1: ##first hit
temp=temp+'_ds'+str(j)+'_'+str(list_pos[k][2])
break
if int(list_pos[k][0]) < pos3 and int(list_pos[k][1]) > pos3: # any hit
n+=1
if n==1: ##first hit
temp=temp+'_us'+str(j)+'_'+str(list_pos[k][2])
break
gene=temp
"""
newdict[repair]=gene
else:
#print(i, 'node not in dictionary')
newdict[repair]='unknown node'
#print('length of nodes list: ', len(nodes))
#print('length of newdict: ', len(newdict))
return(newdict)
def rewritematrix(patient):
#######
#get list of nodes
matrix_file='snp_matrix/'+ patient+'_merged_parsed.txt'
mfile=open(matrix_file, 'r')
nodelist=[]
convertedmatrix={} ##{genes: [gene1,gene2,gene3,...], sample: [0,1,0,1,...]}
convertedmatrix['genes']=[]
first='T'
for line in mfile:
if first=='T':
nodelist=line.strip().split('\t')
for i in nodelist:
gene=newdict[i]
#print(i, gene)
convertedmatrix['genes'].append(gene)
first='F'
elif first=='F': ##need to take info from matrix
sampleline=line.strip().split('\t')
sample=sampleline[0][48:]
sample=sample[0:9]
#print(sample)
convertedmatrix[sample]=[]
n =len(sampleline)
for i in range(1,n):
convertedmatrix[sample].append(sampleline[i])
mfile.close()
return convertedmatrix
def writefile(newmatrix):
filename='snp_new_matrices/'+patient+'_merged_parsed.txt'
newfile=open(filename, 'w')
for i in newmatrix:
n=len(newmatrix[i])
newfile.write(i)
newfile.write('\t')
for k in range(n):
newfile.write(newmatrix[i][k])
newfile.write('\t')
newfile.write('\n')
newfile.close()
def countgenes(newmatrix):
genes=newmatrix['genes']
n = len(genes)
m=len(newmatrix)
temp={}
introns={}
for i in range(n):
if 'intron_' not in genes[i]:
if genes[i] not in temp:
temp[genes[i]]=0
for j in newmatrix:
if j!='genes':
temp[genes[i]] +=int(newmatrix[j][i])
"""
if 'intron_' in genes[i]:
intron_list=genes[i].strip().split('_')
gene2='_'.join(intron_list[2:])
if gene2 in introns:
introns[gene2].append(intron_list[1])
else:
introns[gene2]=[]
introns[gene2].append(intron_list[1])
'''
if gene2 not in temp:
temp[gene2]=0
for j in newmatrix:
if j!='genes':
temp[gene2] +=int(newmatrix[j][i])
'''
"""
return(temp)
#return(temp, introns)
##############################################################################################################
#########################################EXECUTION FROM HERE ############################################
##############################################################################################################
if __name__ == '__main__':
main()
n = len(matrices) #19
total=0
parallel={} #dict for counting # strains have a given mutation
for i in range(n):
patient=matrices[i] ##matrix file name
#patient="MAB_20"
name=patient ##+'_01'
org='E_coli'
gfflib=matchgff(name, org) #library of node:[[start,end,gene],[start,end,gene],....,]
nodes=matchnodes(patient) ##list of NODE@POS
l=len(nodes) ##need to make sure node list is not 0 bc everything is 0
if l!=0:
newdict=nodestogenes(nodes,gfflib)
#print(newdict)
##now that we have the dictionary, Write onto the header file of each matrix the new names
newmatrix=rewritematrix(patient)
#print(newmatrix)
temp=newmatrix['genes']
m=len(temp)
tempdict={}
for k in range(m):
if temp[k] not in tempdict:
tempdict[temp[k]]=1
## optional : saves a record of which genes are mutated in parallel..
for key in tempdict:
if key not in parallel:
parallel[key]=1
else:
parallel[key]+=1
#writefile(newmatrix)
t=countgenes(newmatrix)
for item in t:
print(patient, item, t[item], sep=',')
"""
for key in parallel:
print(key, parallel[key], sep=',')
"""