-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiCLIP.py
471 lines (341 loc) · 15.9 KB
/
iCLIP.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
''' This a modeule that holds functions and classes useful for analysing iCLIP data '''
import CGAT.Experiment as E
import numpy as np
import pandas as pd
import CGAT.GTF as GTF
import collections
def find_first_deletion(cigar):
'''Find the position of the the first deletion in a
read from the cigar string, will return 0 if no deletion
found '''
position = 0
for operation, length in cigar:
if operation == 2:
return position
else:
position += length
return position
class TranscriptCoordInterconverter:
''' A class to interconvert between genome co-ordinates
and transcript co-ordinates. Implemented as a class because
there are expected to be many calls against the same transcript,
so time can be saved by precomputation
TranscriptCoordInterconverter.genome2transcript should be the
interverse of TranscriptCoordInterconverter.transcript2genome.
That is
if myConverter = TranscriptCoordInterverter(transcript)
then
myConverter.genome2transcript(myConverter.transcript2genome(x)) == x
and
myConverter.transcript2genome(myConverter.genome2transcript(x)) == x'''
def __init__(self, transcript, introns=False):
''' Pre compute the conversions for each exon '''
if not introns:
intervals = GTF.asRanges(transcript, feature="exon")
else:
intervals = GTF.toIntronIntervals(transcript)
# get strand
self.strand = transcript[0].strand
# store transcript_id
self.transcript_id = transcript[0].transcript_id
# sort the exons into "transcript" order
if self.strand == "-":
intervals.sort(reverse=True)
intervals = [(y-1, x-1) for x, y in intervals]
else:
intervals.sort(reverse=False)
self.offset = intervals[0][0]
self.genome_intervals = [map(abs, (x-self.offset, y-self.offset))
for x, y in intervals]
interval_sizes = [abs(y-x) for x, y in intervals]
total = 0
transcript_intervals = [None]*len(interval_sizes)
for i in range(len(interval_sizes)):
transcript_intervals[i] = (total,
interval_sizes[i] + total)
total += interval_sizes[i]
self.transcript_intervals = transcript_intervals
self.length = transcript_intervals[-1][1]
def genome2transcript(self, pos):
''' Convert genome coordinate into transcript coordinates.
pos can be a single value or a nunpy array like object.
Passing an array ensures that the transcript is only
searched once, ensuring O(n) performance rather than
O(nlogn)'''
if len(pos) == 0:
return np.array([])
try:
relative_pos = pos - self.offset
except TypeError:
relative_pos = np.array(pos) - self.offset
if self.strand == "-":
relative_pos = relative_pos * -1
ordering = np.argsort(relative_pos)
relative_pos = np.sort(relative_pos)
# pre allocate results list for speed
try:
results = np.zeros(len(relative_pos))
except TypeError:
relative_pos = np.array([relative_pos])
results = np.zeros(1)
i = 0
i_max = len(relative_pos)
# do linear search for correct exon
for exon, interval in enumerate(self.genome_intervals):
if relative_pos[i] < interval[0]:
raise ValueError("Position %i is not in transcript %s" %
(pos[i], self.transcript_id) )
while relative_pos[i] < interval[1]:
pos_within_exon = relative_pos[i]-interval[0]
transcript_exon = self.transcript_intervals[exon]
transcript_position = transcript_exon[0] + pos_within_exon
results[i] = transcript_position
i += 1
if i == i_max:
return results[ordering]
# exon has not been found
raise ValueError("Position %i (%i relative) is not in transcript %s\n exons are %s" %
(pos[i], relative_pos[i], self.transcript_id, self.genome_intervals))
def transcript2genome(self, pos):
''' Convert transcript coodinate into genome coordinate,
pos can be a single value or a nunpy array like object.
Passing an array ensures that the transcript is only
searched once, ensuring O(n) performance rather than
O(nlogn)'''
try:
if len(pos) == 0:
return np.array([])
except TypeError:
pos = np.array([pos])
# Converting a list is only efficient if the list is ordered
# however want to be able to return list in the same order it
# arrived, so remember the order and then sort.
ordering = np.argsort(pos)
pos = np.sort(pos)
# pre allocate results list for speed
results = np.zeros(len(pos))
i = 0
i_max = len(pos)
# do linear search for correct exon
for exon, interval in enumerate(self.transcript_intervals):
while pos[i] < interval[1]:
pos_within_exon = pos[i] - interval[0]
genome_exon = self.genome_intervals[exon]
relative_genome_position = genome_exon[0] + pos_within_exon
if self.strand == "-":
results[i] = (self.offset - relative_genome_position)
i += 1
else:
results[i] = (self.offset + relative_genome_position)
i += 1
if i == i_max:
return results[ordering]
# beyond the end of the transcript
ValueError("Transcript postion %i outside of transcript %s" %
(pos[i], self.transcript_id))
def transcript_interval2genome_intervals(self, interval):
'''Take an interval in transcript coordinates and returns
a list of intervals in genome coordinates representing the
interval on the genome '''
outlist = []
for exon in self.transcript_intervals:
if interval[0] < exon[1]:
start = interval[0]
if interval[1] <= exon[1]:
outlist.append((start, interval[1]))
break
else:
outlist.append((start, exon[1]))
interval = (exon[1], interval[1])
genome_list = [tuple(self.transcript2genome((x, y-1))) for
x, y in outlist]
# these intervals are zero based-closed. Need to make half open
if self.strand == "+":
genome_list = [(x, y+1) for x, y in genome_list]
else:
genome_list = [(y, x+1) for x, y in genome_list]
return sorted(genome_list)
def getCrosslink(read):
''' Finds the crosslinked base from a pysam read.
Cross linked bases are definated as in Sugimoto et al, Genome Biology 2012
The nucleotide preceding the iCLIP cDNAs mapped by Bowtie was used to
define the cross link sites identified by truncated cDNAs.
[For reads with deletions] The deleted nucleotide in CLIP and iCLIP
cDNAs mapped by Novoalign was used to define the cross-link sites
identified by read-through cDNAs. If a cDNA had more than one deletion,
we selected the one closest to the beginning of the read.
returns a tuple with the position of the read and one of the following
categories:
* truncated_neg
* truncated_pos
* deletion_neg
* deletion_pos
to record whether the position came from a truncation or a deletion '''
if not 'D' in read.cigarstring:
if read.is_reverse:
pos = read.aend
cat = "truncated_neg"
else:
pos = read.pos - 1
cat = "truncated_pos"
else:
if read.is_reverse:
cigar = reversed(read.cigar)
position = find_first_deletion(cigar)
pos = read.aend - position - 1
cat = "deletion_neg"
else:
position = find_first_deletion(read.cigar)
pos = read.pos + position
cat = "deletion_pos"
return (pos,cat)
def countChr(reads, chr_len, dtype = 'uint16'):
''' Counts the crosslinked bases for each read in the pysam rowiterator
reads and saves them in pandas Series: those on the positive strand
and those on the negative strand. The Series are indexed on genome position,
and are sparse.
Cross linked bases are definated as in Sugimoto et al, Genome Biology 2012
The nucleotide preceding the iCLIP cDNAs mapped by Bowtie was used to
define the cross link sites identified by truncated cDNAs.
[For reads with deletions] The deleted nucleotide in CLIP and iCLIP
cDNAs mapped by Novoalign was used to define the cross-link sites
identified by read-through cDNAs. If a cDNA had more than one deletion,
we selected the one closest to the beginning of the read.
The dtype to use internally for storage can be specified. Large types
reduce the chance of overflow, but require more memory. With 'uint16'
the largest count that can be handled is 255. Data is stored sparse,
so memory is less of a problem. Overflow will cause a ValueError.
returns a tuple of pandas Series objects, with the positive and negative
strand arrays and also a counter object that contains the counts for each
type of site. '''
pos_depths = collections.defaultdict(int)
neg_depths = collections.defaultdict(int)
counter = E.Counter()
for read in reads:
(pos, cat) = getCrosslink(read)
counter[cat] += 1
if read.is_reverse:
neg_depths[float(pos)] += 1
else:
pos_depths[float(pos)] += 1
try:
pos_depths = pd.Series(pos_depths, dtype=dtype)
except ValueError:
pos_depths = pd.Series({}, dtype=dtype)
try:
neg_depths = pd.Series(neg_depths, dtype=dtype)
except ValueError:
neg_depths = pd.Series({}, dtype=dtype)
# check for integer overflow: counter sum should add up to array sum
counter_sum = sum([y for x, y in counter.iteritems()])
array_sum = pos_depths.sum() + neg_depths.sum()
if not counter_sum == array_sum:
raise (ValueError,
"Sum of depths is not equal to number of "
"reads counted, possibly dtype %s not large enough" % dtype)
# E.debug("Counted %i truncated on positive strand, %i on negative"
# % (counter.truncated_pos, counter.truncated_neg))
# E.debug("and %i deletion reads on positive strand, %i on negative"
# % (counter.deletion_pos, counter.deletion_neg))
return (pos_depths, neg_depths, counter)
def count_intervals(bam, intervals, contig, strand=".", dtype='uint16'):
''' Count the crosslinked bases accross a transcript '''
chr_len = bam.lengths[bam.gettid(contig)]
exon_counts = []
for exon in intervals:
# X-linked position is first base before read: need to pull back
# reads that might be one base out. Extra bases will be filtered out
# later.
try:
reads = bam.fetch(reference=contig,
start=max(0,exon[0]-1),
end=exon[1]+1)
except ValueError as e:
E.debug(e)
E.warning("Skipping intervals on contig %s as not present in bam"
% contig)
return pd.Series()
count_results = countChr(reads, chr_len, dtype)
# fetch pulls back any reads that *overlap* the specified coordinates
# exlude Xlinked bases outside the interval (prevents double counting)
if strand == "+":
if len(count_results[0]) > 0:
exon_counts.append(count_results[0].sort_index().loc[
float(exon[0]):float(exon[1]-1)])
elif strand == "-":
if len(count_results[1]) > 0:
exon_counts.append(count_results[1].sort_index().loc[
float(exon[0]):float(exon[1]-1)])
else:
sum_counts = count_results[0].loc[(count_results[0].index >= exon[0]) &
(count_results[0].index < exon[1])] + \
count_results[1].loc[(count_results[1].index >= exon[0]) &
(count_results[1].index < exon[1])]
exon_counts.append(sum_counts)
if len(exon_counts) == 0:
transcript_counts = pd.Series()
else:
transcript_counts = pd.concat(exon_counts)
# transcript_counts = transcript_counts.sort_index()
return transcript_counts
def calcAverageDistance(profile1, profile2):
''' This function calculates the average distance of all
pairwise distances in two profiles'''
def _cartesian(x, y):
return np.transpose([np.tile(x, len(y)), np.repeat(y, len(x))])
positions = _cartesian(profile1.index.values, profile2.index.values)
counts = _cartesian(profile1.values, profile2.values)
counts = np.prod(counts, axis=1)
distances = np.abs(positions[:, 0] - positions[:, 1])
mean_distance = (distances.astype("float64") * counts).sum() / np.sum(counts)
return mean_distance
def findMinDistance(profile1, profile2):
'''Finds mean distance between each read in profile1
and a read in profile2'''
locations1 = profile1.index.values
locations2 = profile2.index.values # .astype("int16")
mat1 = np.repeat(locations1, locations2.size).reshape(
(locations1.size, locations2.size))
mat2 = np.tile(locations2, locations1.size).reshape(
(locations1.size, locations2.size))
distances = np.abs(mat1-mat2).min(axis=1)
return distances.mean()
def randomiseSites(profile, start, end, keep_dist=True):
'''Randomise clipped sites within an interval (between start and end)
if keep_dist is true, then reads on the same base are kept togehter'''
if keep_dist:
profile = profile.copy()
profile.index = np.random.choice(
np.arange(start, end), profile.size, replace=False)
profile = profile.sort_index()
return profile
else:
randomised = np.random.choice(
np.arange(start, end), profile.sum(), replace=True)
randomised = pd.Series(randomised).value_counts().sort_index()
return randomised
def spread(profile, bases, reindex=True):
start = int(profile.index[0] - 2*bases)
end = int(profile.index[-1] + 2*bases+1)
if reindex:
profile = profile.reindex(range(start, end))
profile = profile.fillna(0)
return pd.rolling_sum(profile, window=2*bases+1, center=True).dropna()
def corr_profile(profile1, profile2, nspread, profile2_ready=False):
profile1 = profile1.reindex(
range(int(profile1.index.values.min())-1,
int(profile1.index.values.max())+1)).fillna(0)
profile1 = spread(profile1,nspread, False)
if not profile2_ready:
profile2 = profile2.reindex(
range(int(profile2.index.values.min()),
int(profile2.index.values.max()))).fillna(0)
profile2 = spread(profile2, nspread, False)
return profile1.corr(profile2, method="spearman")
def rand_apply(profile, exon, n, func, keep_dist=False, *args, **kwargs):
dummy = pd.Series(range(n))
def _inner_func(x):
rand = randomiseSites(profile, exon.start, exon.end,
keep_dist=keep_dist)
return func(rand, *args, **kwargs)
return dummy.apply(_inner_func)