forked from jeffhussmann/ribosomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore_UTRs.py
408 lines (338 loc) · 14.2 KB
/
explore_UTRs.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
407
408
from collections import Counter, defaultdict
from Sequencing import utilities
import gtf
import gff
import pyliftover
from glob import glob
import os.path
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm
import scipy.stats
from mpl_toolkits.axes_grid1 import make_axes_locatable
def maybe_int(string):
try:
value = int(string)
except ValueError:
value = string
return value
def read_weinberg_file():
fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/EF4/transcriptome/Cerevisiae_refFlat.txt'
fh = open(fn)
for i in range(3):
fh.readline()
labels = fh.readline().strip().split()
genes = {}
for line in fh:
fields = line.strip().split('\t')
pairs = zip(labels, map(maybe_int, fields))
genes[fields[0]] = dict(pairs[2:])
return genes
def read_nagalakshmi_file(fn):
fh = open(fn)
for i in range(2):
fh.readline()
labels = fh.readline().strip().split()
genes = {}
for line in fh:
fields = line.strip('\n').split('\t')
pairs = zip(labels, map(maybe_int, fields))
genes[fields[0]] = dict(pairs[1:])
return genes
def sanity_check_nagalakshmi_file():
gtf_fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/SGD1.01/transcriptome/genes.gtf'
CDSs = gtf.get_CDSs(gtf_fn)
gtf_dict = {t.name: t for t in CDSs}
genes = read_nagalakshmi_file('nagalakshmi_annotations.txt')
discrepancies_after = {}
for name in genes:
if name not in gtf_dict:
print name, 'not in gtf_dict'
continue
start, end = genes[name]['SGD_Start'] - 1, genes[name]['SGD_End'] - 1
chrom = genes[name]['Chrom']
if start > end:
start, end = end, start
if start != gtf_dict[name].start or end != gtf_dict[name].end:
#print name, chrom
#print start, end
#print gtf_dict[name].start, gtf_dict[name].end
#print
if chrom not in discrepancies_after:
discrepancies_after[chrom] = start
else:
discrepancies_after[chrom] = min(start, discrepancies_after[chrom])
return discrepancies_after
def sanity_check_lifted_nagalakshmi_file(fn):
gtf_fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/EF4/transcriptome/genes.gtf'
CDSs = gtf.get_CDSs(gtf_fn)
gtf_dict = {t.name: t for t in CDSs}
genes = read_nagalakshmi_file(fn)
discrepancies_after = defaultdict(list)
for name in genes:
if name not in gtf_dict:
print name, 'not in gtf_dict'
continue
start, end = genes[name]['SGD_Start'], genes[name]['SGD_End']
chrom = genes[name]['Chrom']
if start > end:
start, end = end, start
end -= 1
if start != gtf_dict[name].start or end != gtf_dict[name].end:
#print name, chrom
#print start, end
#print gtf_dict[name].start, gtf_dict[name].end
#raw_input()
discrepancies_after[chrom].append(name)
return discrepancies_after
def sanity_check_weinberg_file():
gtf_fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/EF4/transcriptome/genes.gtf'
CDSs = gtf.get_CDSs(gtf_fn)
gtf_dict = {t.name: t for t in CDSs}
genes = read_weinberg_file()
discrepancies_after = defaultdict(list)
for name in genes:
if name not in gtf_dict:
print name, 'not in gtf_dict'
continue
start, end = genes[name]['CdsStart'], genes[name]['CdsEnd']
chrom = genes[name]['Chromosome']
end -= 1
if start != gtf_dict[name].start or end != gtf_dict[name].end:
print name, chrom
print start, end
print gtf_dict[name].start, gtf_dict[name].end
raw_input()
discrepancies_after[chrom].append(name)
return discrepancies_after
def do_liftover(chain_fn, description):
original_fn = 'nagalakshmi_annotations.txt'
lifted_fn = 'nagalakshmi_annotations_lifted_{0}.txt'.format(description)
original_fh = open(original_fn)
for i in range(2):
original_fh.readline()
labels = original_fh.readline().strip().split()
keys_to_convert = ['SGD_Start', 'SGD_End', '5\'-UTR_Start', '3\'-UTR_End']
lo = pyliftover.LiftOver(chain_fn)
with open(lifted_fn, 'w') as lifted_fh:
original_fh = open(original_fn)
for i in range(2):
lifted_fh.write(original_fh.readline())
labels_line = original_fh.readline()
lifted_fh.write(labels_line)
labels = labels_line.strip().split()
for line in original_fh:
fields = line.strip('\n').split('\t')
name = fields[0]
if name == 'YBR013C':
# This gets its 5' UTR deleted by liftover. Ignore it for now
continue
#if name == 'YJR122W':
# # This has its coding sequence misannotated in nagalakshmi.
# continue
pairs = zip(labels, map(maybe_int, fields))
gene = dict(pairs[1:])
if gene['Chrom'] == 'chrMito':
# Renamed in EF4, and not included in weinberg anyways.
continue
bad_lift = False
for key in keys_to_convert:
if gene[key] != '':
lift = lo.convert_coordinate(gene['Chrom'], gene[key] - 1)
if lift == []:
print gene, 'empty list'
bad_lift = True
break
seqname, coord, _, _ = lift[0]
gene[key] = coord
if bad_lift:
continue
if gene['SGD_Start'] < gene['SGD_End']:
# plus strand
gene['SGD_End'] = gene['SGD_End'] + 1
elif gene['SGD_Start'] > gene['SGD_End']:
# minus strand
gene['SGD_Start'] = gene['SGD_Start'] + 1
else:
raise ValueError(name)
lifted_line = '\t'.join([name] + [str(gene[key]) for key in labels[1:]]) + '\n'
lifted_fh.write(lifted_line)
def lift_all():
chrs = [
'chrI',
'chrII',
'chrIII',
'chrIV',
'chrIX',
'chrV',
'chrVI',
'chrVII',
'chrVIII',
'chrX',
'chrXI',
'chrXII',
'chrXIII',
'chrXIV',
'chrXV',
'chrXVI',
]
num_disagreeing = {key: [] for key in chrs}
chain_fns = glob('/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/chains/*sanitized.over.chain')
for chain_fn in sorted(chain_fns):
head, tail = os.path.split(chain_fn)
print tail[:3]
if '57' not in tail[:3]:
continue
do_liftover(chain_fn, tail[:3])
discs = sanity_check_lifted_nagalakshmi_file(tail[:3])
for key in chrs:
num_disagreeing[key].append(len(discs[key]))
for key in chrs:
print key, num_disagreeing[key]
array = np.asarray([num_disagreeing[key] for key in chrs])
return array
def scatter_with_hists_colors(x_list, y_list, x_label, y_label, title):
sampled_points = np.vstack([x_list[:10000], y_list[:10000]])
points = np.vstack([x_list, y_list])
kernel = scipy.stats.gaussian_kde(sampled_points)
colors = kernel(points)
fig, ax_scatter = plt.subplots()
ax_scatter.scatter(x_list, y_list, c=colors, cmap=matplotlib.cm.jet, s=4, linewidths=(0.1,))
ax_scatter.set_aspect(1.)
divider = make_axes_locatable(ax_scatter)
ax_hist_x = divider.append_axes('top', 1.2, pad=0.1, sharex=ax_scatter)
ax_hist_y = divider.append_axes('right', 1.2, pad=0.1, sharey=ax_scatter)
plt.setp(ax_hist_x.get_xticklabels() + ax_hist_x.get_yticklabels(),
visible=False)
plt.setp(ax_hist_x.get_xticklines() + ax_hist_x.get_yticklines(),
visible=False)
plt.setp(ax_hist_y.get_xticklabels() + ax_hist_y.get_yticklabels(),
visible=False)
plt.setp(ax_hist_y.get_xticklines() + ax_hist_y.get_yticklines(),
visible=False)
ax_scatter.set_xlabel(x_label)
ax_scatter.set_ylabel(y_label)
ax_hist_x.hist(x_list, histtype='step', bins=100)
ax_hist_y.hist(y_list, histtype='step', bins=100, orientation='horizontal')
#ax_scatter.set_xlim(left=-20)
#ax_scatter.set_ylim(bottom=-20)
fig.suptitle(title)
def check_for_overlap():
weinberg_genes = read_weinberg_file()
gff_fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/EF4/saccharomyces_cerevisiae.gff'
starts_in_region_finder = gff.make_starts_in_region_finder(gff_fn)
print '- strand'
for name, gene in weinberg_genes.iteritems():
if gene['Strand'] != '-':
continue
region_start = (gene['Chromosome'], gene['CdsEnd'])
region_end = (gene['Chromosome'], gene['TxEnd'])
starts_in_region = starts_in_region_finder(region_start, region_end)
if starts_in_region != []:
print name
print '{0}:{1}-{2}'.format(gene['Chromosome'], gene['CdsEnd'], gene['TxEnd'])
for feature in starts_in_region:
print feature.attribute['Name'], feature.start
raw_input()
print '+ strand'
#sorted_transcripts = gtf.sort_transcripts(all_transcripts, by_end=True)
#name_to_sorted_index = {t.name: i for i, t in enumerate(sorted_transcripts)}
#for name, gene in weinberg_genes.iteritems():
# if name not in name_to_sorted_index:
# continue
# i = name_to_sorted_index[name]
# if (sorted_transcripts[i - 1].end <= gene['CdsStart'] and
# gene['TxStart'] <= sorted_transcripts[i - 1].end and
# gene['Strand'] == '+' and sorted_transcripts[i - 1].strand == '+') or \
# name == 'YNL054W':
# print name
# print gene['TxStart']
# print sorted_transcripts[i]
# print sorted_transcripts[i - 1]
if __name__ == '__main__':
nagalakshmi_lifted_fn = '/home/jah/projects/arlen/data/organisms/saccharomyces_cerevisiae/EF4/transcriptome/nagalakshmi_annotations_lifted_V57.txt'
weinberg_genes = read_weinberg_file()
nagalakshmi_genes = read_nagalakshmi_file(nagalakshmi_lifted_fn)
# Check that the liftover worked.
bad_genes = defaultdict(list)
UTRs_dict = {}
weinberg_fives = []
weinberg_threes = []
nagalakshmi_fives = []
nagalakshmi_threes = []
discrepancies = sanity_check_lifted_nagalakshmi_file(nagalakshmi_lifted_fn)
changed_names = set(reduce(lambda x, y: x + y, discrepancies.values(), []))
valid_genes = set(weinberg_genes) & set(nagalakshmi_genes) - changed_names
for name in set(weinberg_genes) & set(nagalakshmi_genes):
weinberg_gene = weinberg_genes[name]
nagalakshmi_gene = nagalakshmi_genes[name]
if weinberg_genes[name]['Strand'] == '+':
disagree = (weinberg_gene['CdsStart'] != nagalakshmi_gene['SGD_Start'] or
weinberg_gene['CdsEnd'] != nagalakshmi_gene['SGD_End'])
else:
disagree = (weinberg_gene['CdsStart'] != nagalakshmi_gene['SGD_End'] or
weinberg_gene['CdsEnd'] != nagalakshmi_gene['SGD_Start'])
if disagree:
bad_genes[weinberg_gene['Chromosome']].append(name)
else:
before = weinberg_gene['CdsStart'] - weinberg_gene['TxStart']
after = weinberg_gene['TxEnd'] - weinberg_gene['CdsEnd']
if weinberg_gene['Strand'] == '+':
weinberg_five = before
weinberg_three = after
elif weinberg_gene['Strand'] == '-':
weinberg_five = after
weinberg_three = before
nagalakshmi_five = nagalakshmi_gene['5\'-UTR_length']
if isinstance(nagalakshmi_five, str):
nagalakshmi_five = 0
nagalakshmi_three = nagalakshmi_gene['3\'-UTR_length']
if isinstance(nagalakshmi_three, str):
nagalakshmi_three = 0
UTRs_dict[name] = {5: {'weinberg': weinberg_five,
'nagalakshmi': nagalakshmi_five,
},
3: {'weinberg': weinberg_three,
'nagalakshmi': nagalakshmi_three,
},
}
weinberg_fives.append(weinberg_five)
weinberg_threes.append(weinberg_three)
nagalakshmi_fives.append(nagalakshmi_five)
nagalakshmi_threes.append(nagalakshmi_three)
#for title, xs, ys in [('5\' UTR lengths', weinberg_fives, nagalakshmi_fives),
# ('3\' UTR lengths', weinberg_threes, nagalakshmi_threes),
# ]:
# scatter_with_hists_colors(xs, ys, 'Weinberg/Arribere annotation', 'Nagalakshi annotation', title)
#for name, UTR in UTRs_dict.items():
# if UTR[5]['nagalakshmi'] > 0 and UTR[5]['weinberg'] - UTR[5]['nagalakshmi'] > 200:
# print name, 'changed' if name in changed_names else ''
# print weinberg_genes[name]['Chromosome'], weinberg_genes[name]['TxStart']
# print UTR[5]
sorted_UTRs = sorted(UTRs_dict.items(), key=lambda (name, UTR): UTR[5]['weinberg'], reverse=True)
for name, UTR in sorted_UTRs[:20]:
print name
print '{0}:{1}-{2}'.format(weinberg_genes[name]['Chromosome'], weinberg_genes[name]['CdsEnd'], weinberg_genes[name]['TxEnd'])
print UTR[5]
#fives = Counter()
#threes = Counter()
#
#five_length_to_name = defaultdict(list)
#three_length_to_name = defaultdict(list)
#
#for name in genes:
# before = int(genes[name]['CdsStart']) - int(genes[name]['TxStart'])
# after = int(genes[name]['TxEnd']) - int(genes[name]['CdsEnd'])
# if genes[name]['Strand'] == '+':
# fives[before] += 1
# threes[after] += 1
# five_length_to_name[before].append(name)
# three_length_to_name[after].append(name)
# elif genes[name]['Strand'] == '-':
# fives[after] += 1
# threes[before] += 1
# five_length_to_name[after].append(name)
# three_length_to_name[before].append(name)
#
#fives = utilities.counts_to_array(fives)
#threes = utilities.counts_to_array(threes)