forked from makovalab-psu/NoiseCancellingRepeatFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncrf_consensus_filter.py
executable file
·560 lines (448 loc) · 16.9 KB
/
ncrf_consensus_filter.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python
"""
Filter Noise Cancelling Repeat Finder alignments, discarding alignments that
has a consensus different than the motif unit.
"""
from sys import argv,stdin,stdout,stderr,exit
from math import log,exp
from ncrf_parse import alignments,reverse_complement,canonical_motif, \
parse_probability,int_with_unit,commatize
def usage(s=None):
message = """
usage: ncrf_cat <output_from_NCRF> | ncrf_consensus_filter [options]
--consensusonly just report the consensus motif(s) for each alignment,
instead of filtering; these are added to the alignment
file with a "# consensus" tag; note that the reported
consensus will be canonical, the lexigographical minimum
of all rotations including reverse complement
[<name>:]<motif> dna repeat motif to process; if no motifs are specified,
we process all of them (however, see note below)
(more than one motif can be specified)
--head=<number> limit the number of input alignments
--progress=<number> periodically report how many alignments we've tested
Any motif that was given a name during the alignment process has to be
specified here, and with the same name. A motif was 'named' if an option of the
form <name>:<motif> was given to NCRF. The nt sequence for named motifs does
not appear in the alignment file produced by NCRF, but this program needs that
sequence."""
if (s == None): exit (message)
else: exit ("%s\n%s" % (s,message))
def main():
global headLimit,reportProgress,requireEof
global winnerThreshold,filterToKeep,reportConsensus,reportMsa
global canonicalizeConsensuses
global debug
canonicalizeConsensuses = True
# parse the command line
filterToKeep = "consensus"
nameToMotif = {}
motifsOfInterest = []
reportConsensus = False
reportMsa = False
winnerThreshold = 0.50 # (see derive_consensuses)
sliceWidth = None
sliceStep = None
headLimit = None
reportProgress = None
requireEof = True
debug = []
for arg in argv[1:]:
if ("=" in arg):
argVal = arg.split("=",1)[1]
if (arg == "--nonconsensus"): # (unadvertised)
filterToKeep = "non consensus"
reportMsa = False
reportConsensus = True
elif (arg == "--nonconsensus,msa"): # (unadvertised)
filterToKeep = "non consensus"
reportMsa = True
reportConsensus = True
elif (arg == "--consensusonly"):
filterToKeep = "no filter"
reportMsa = False
reportConsensus = True
elif (arg == "--filter,consensus"): # (unadvertised)
filterToKeep = "consensus"
reportMsa = False
reportConsensus = True
elif (arg == "--msa"): # (unadvertised)
filterToKeep = "no filter"
reportMsa = True
reportConsensus = True
elif (arg.startswith("--winner=")) or (arg.startswith("W=")): # (unadvertised)
winnerThreshold = parse_probability(argVal)
elif (arg.startswith("--slice=")): # (unadvertised)
if ("by" in argVal):
(sliceWidth,sliceStep) = map(int_with_unit,argVal.split("by",1))
else:
sliceWidth = sliceStep = int_with_unit(argVal)
elif (arg.startswith("--head=")):
headLimit = int_with_unit(argVal)
elif (arg.startswith("--progress=")):
reportProgress = int_with_unit(argVal)
elif (arg in ["--noendmark","--noeof","--nomark"]): # (unadvertised)
requireEof = False
elif (arg == "--debug"):
debug += ["debug"]
elif (arg.startswith("--debug=")):
debug += argVal.split(",")
elif (arg.startswith("--")):
usage("unrecognized option: %s" % arg)
elif (":" in arg):
(name,motif) = arg.split(":",1)
if (name in nameToMotif) and (nameToMotif[name] != motif):
usage("\"%s\" is given for more than one motif" % name)
if (name not in nameToMotif):
nameToMotif[name] = motif
motifsOfInterest += [motif]
else:
motifsOfInterest += [arg]
if (motifsOfInterest == []):
motifsOfInterest = None # this really means all motifs are of interest
else:
motifsOfInterest = set(motifsOfInterest)
# process the alignments
if (sliceWidth == None):
simple_consensus_filter(stdin,motifsOfInterest,nameToMotif)
else:
sliced_consensus_filter(stdin,motifsOfInterest,nameToMotif,
sliceWidth,sliceStep)
# simple_consensus_filter--
def simple_consensus_filter(f,motifsOfInterest,nameToMotif):
alignmentNum = 0
alignmentsWritten = 0
for a in alignments(f,requireEof):
alignmentNum += 1
if (reportProgress != None):
if (alignmentNum == 1) or (alignmentNum % reportProgress == 0):
print >>stderr, "progress: testing alignment %s" \
% commatize(alignmentNum)
if (headLimit != None) and (alignmentNum > headLimit):
print >>stderr, "limit of %d alignments reached" % headLimit
break
if (a.motif in nameToMotif):
a.motif = nameToMotif[a.motif]
if (motifsOfInterest != None) and (a.motif not in motifsOfInterest):
continue
if ([ch for ch in a.motif if (ch not in "ACGT")] != []):
abort_warn_about_named_motifs(a)
motifText = a.motifText
seqText = a.seqText
if ("noflip" in debug):
pass
elif (a.strand == "-") and (a.start < a.end):
# alignment was reported in reverse complement of motif, so flip it
motifText = reverse_complement(motifText)
seqText = reverse_complement(seqText)
# derive consensus(es)
seqChunks = chunkify(a.motif,motifText,seqText)
if ("consensus" in debug):
print >>stderr
print >>stderr, "%d score=%d" % (a.lineNumber,a.score)
consensuses = derive_consensuses(seqChunks,winnerThreshold=winnerThreshold)
consensuses = list(consensuses)
# discard the alignment if it meets the filtering criterion (if there
# is any such criterion)
if (filterToKeep == "consensus"):
if (a.motif not in consensuses): continue # (discard it)
elif (filterToKeep == "non consensus"):
if (a.motif in consensuses): continue # (discard it)
else: # if (filterToKeep == "no filter"):
pass
# copy the (unfiltered) alignment to the output
if (alignmentsWritten > 0): print
alignmentsWritten += 1
print "\n".join(a.lines)
# report the consensus, if we're supposed to
if (reportConsensus):
if (consensuses == []):
print "# consensus (none)"
else:
canonicalized = []
for motif in consensuses:
if (motif != a.motif) and (canonicalizeConsensuses):
(motif,strand) = canonical_motif(motif)
canonicalized += [motif]
print "# consensus %s" % ",".join(canonicalized)
# report the MSA from which the consensus was derived, if we're
# supposed to
if (reportMsa):
motifLen = len(a.motif)
positionLength = [1] * motifLen
for chunk in seqChunks:
for (motifIx,seqNucs) in enumerate(chunk):
if (seqNucs == None): continue
positionLength[motifIx] = max(positionLength[motifIx],len(seqNucs))
line = []
for (motifIx,motifNuc) in enumerate(a.motif):
line += [motifNuc.ljust(positionLength[motifIx],".")]
print "# msa.query %s" % "".join(line)
for chunk in seqChunks:
line = []
for (motifIx,seqNucs) in enumerate(chunk):
if (seqNucs == None):
line += ["."*positionLength[motifIx]]
elif (seqNucs == a.motif[motifIx]):
line += ["="*positionLength[motifIx]]
else:
line += [seqNucs.ljust(positionLength[motifIx],".")]
print "# msa.seq %s" % "".join(line)
if (requireEof):
print "# ncrf end-of-file"
# sliced_consensus_filter--
userHasBeenWarned = False
def sliced_consensus_filter(f,motifsOfInterest,nameToMotif,sliceWidth,sliceStep):
global userHasBeenWarned
if (reportMsa) and (not userHasBeenWarned):
print >>stderr, "WARNING: sliced consensus doesn't report MSA, ignoring that request"
userHasBeenWarned = True
alignmentNum = 0
alignmentsWritten = 0
for a in alignments(f,requireEof):
alignmentNum += 1
if (reportProgress != None):
if (alignmentNum == 1) or (alignmentNum % reportProgress == 0):
print >>stderr, "progress: testing alignment %s" \
% commatize(alignmentNum)
if (headLimit != None) and (alignmentNum > headLimit):
print >>stderr, "limit of %d alignments reached" % headLimit
break
if (a.motif in nameToMotif):
a.motif = nameToMotif[a.motif]
if (motifsOfInterest != None) and (a.motif not in motifsOfInterest):
continue
if ([ch for ch in a.motif if (ch not in "ACGT")] != []):
abort_warn_about_named_motifs(a)
motifText = a.motifText
seqText = a.seqText
if ("noflip" in debug):
pass
elif (a.strand == "-") and (a.start < a.end):
# alignment was reported in reverse complement of motif, so flip it
motifText = reverse_complement(motifText)
seqText = reverse_complement(seqText)
# look for consensus over each slice, separately
consensuses = set()
numSlices = (len(motifText) + sliceStep-1) / sliceStep # (an overestimate)
minSlice = 10*len(a.motif)
for sliceNum in xrange(numSlices):
sliceStart = sliceNum * sliceStep
sliceEnd = min(sliceStart+sliceWidth,len(motifText))
if (sliceEnd - sliceStart < minSlice): break
motifTextSlice = motifText[sliceStart:sliceEnd]
seqTextSlice = seqText [sliceStart:sliceEnd]
# derive consensus(es)
seqChunks = chunkify(a.motif,motifTextSlice,seqTextSlice)
if ("consensus" in debug):
print >>stderr
print >>stderr, "%d score=%d slice.start=%d slice.end=%d" \
% (a.lineNumber,a.score,sliceStart,sliceEnd)
sliceConsensuses = derive_consensuses(seqChunks,winnerThreshold=winnerThreshold)
sliceConsensuses = list(sliceConsensuses)
if (sliceConsensuses == []):
consensuses.add(None)
else:
for word in sliceConsensuses:
consensuses.add(word)
if ("consensus" in debug):
for word in sliceConsensuses:
print >>stderr, "consensus %s" % word
consensuses = list(consensuses)
# discard the alignment if it meets the filtering criterion (if there
# is any such criterion)
if (filterToKeep == "consensus"):
if (a.motif not in consensuses): continue # (discard it)
elif (filterToKeep == "non consensus"):
if (a.motif in consensuses): continue # (discard it)
else: # if (filterToKeep == "no filter"):
pass
# copy the (unfiltered) alignment to the output
if (alignmentsWritten > 0): print
alignmentsWritten += 1
print "\n".join(a.lines)
# report the consensus, if we're supposed to
if (reportConsensus):
if (consensuses == []):
print "# consensus (none)"
else:
canonicalized = []
for motif in consensuses:
if (motif == None): continue
if (motif != a.motif) and (canonicalizeConsensuses):
(motif,strand) = canonical_motif(motif)
canonicalized += [motif]
if (None in consensuses):
canonicalized += ["(none)"]
print "# consensus %s" % ",".join(canonicalized)
if (requireEof):
print "# ncrf end-of-file"
# chunkify--
# Returns a list of sublists. Each sublist is as long as the motif, consisting
# of the string (the nt or nts) matched to each position in the motif.
#
# Note: the caller should not depend on the order of the returned chunks
def chunkify(motif,motifText,seqText):
motifText = motifText.upper()
motifLen = len(motif)
motifPosInfo = position_in_motif(motif,motifText)
if (motifPosInfo == None):
# no match found, just use position 0,forward, with the expectation
# that we the chunks we return won't produce a consensus
(motifPos,direction) = (0,"+")
if ("motifpos" in debug):
print >>stderr, "motifPos=%d%s (None)" % (motifPos,direction)
else:
(motifPos,direction) = motifPosInfo
if ("motifpos" in debug):
if (direction == "+"):
alignedMotif = motif[motifPos:] + motif[:motifPos]
else: # if (direction == "-"):
revMotif = reverse_complement(motif)
alignedMotif = revMotif[motifPos:] + revMotif[:motifPos]
print >>stderr, "motifPos=%d%s (%s)" \
% (motifPos,direction,alignedMotif)
chunks = []
motifIx = motifPos
chunk = [None] * motifIx
for (ix,textNuc) in enumerate(motifText):
seqNuc = seqText[ix]
if (textNuc == "-"): # insertion, extra character in seqText
if (chunk != []): # add inserted character to latest token
if (chunk[-1] != None): chunk[-1] += seqNuc
elif (chunks != []): # add inserted character to final token in latest chunk
latestChunk = chunks[-1]
latestChunk[-1] += seqNuc
chunks[-1] = latestChunk
continue
chunk += [seqNuc] # match or deletion
motifIx += 1
if (motifIx == motifLen):
chunks += [chunk]
chunk = []
motifIx = 0
if (chunk != []):
if (len(chunk) < motifLen): chunk += [None] * (motifLen - len(chunk))
chunks += [chunk]
if (direction == "-"):
for (ix,chunk) in enumerate(chunks):
chunk.reverse()
chunks[ix] = map(tolerant_reverse_complement,chunk)
if ("chunks" in debug):
for (chunkIx,chunk) in enumerate(chunks):
print >>stderr, "chunk[%d] = %s" % (chunkIx,str(chunk))
return chunks
# position_in_motif--
# Returns the index in 'motif' at which the first position of 'text" aligns
def position_in_motif(motif,text):
motifLen = len(motif)
# try matching in same orientation
for rotIx in xrange(motifLen):
rotMotif = motif[rotIx:] + motif[:rotIx]
pos = 0
motifIx = 0
isMatch = True
while (pos < len(text)):
nuc = text[pos]
pos += 1
if (nuc == '-'): continue
if (nuc != rotMotif[motifIx]):
isMatch = False
break
motifIx += 1
if (motifIx == motifLen):
break
if (isMatch):
return (rotIx,"+")
# try matching in reverse orientation
motif = reverse_complement(motif)
for rotIx in xrange(motifLen):
rotMotif = motif[rotIx:] + motif[:rotIx]
pos = 0
motifIx = 0
isMatch = True
while (pos < len(text)):
nuc = text[pos]
pos += 1
if (nuc == '-'): continue
if (nuc != rotMotif[motifIx]):
isMatch = False
break
motifIx += 1
if (motifIx == motifLen):
break
if (isMatch):
return (rotIx,"-")
# no match found
return None
# derive_consensuses--
# Yields a series of potential consensus motifs for a given alignment. The
# input is a list of sublists (as created by chunkify).
def derive_consensuses(seqChunks,winnerThreshold=0.50):
if (seqChunks == []): return
motifLen = len(seqChunks[0])
# count the number of times each motif position is observed to match a
# particular 'token'
ixToTokens = {}
for motifIx in xrange(motifLen):
ixToTokens[motifIx] = {}
for chunk in seqChunks:
for (motifIx,seqNucs) in enumerate(chunk):
if (seqNucs == None): seqNucs = ""
if (seqNucs not in ixToTokens[motifIx]):
ixToTokens[motifIx][seqNucs] = 1
else:
ixToTokens[motifIx][seqNucs] += 1
# at each position, sort the tokens from most-observed to least-observed,
# then reduce them to best (or almost best)
ixToWinners = {}
for motifIx in xrange(motifLen):
tokensSeen = ixToTokens[motifIx]
ixToTokens[motifIx] = [(ixToTokens[motifIx][seqNucs],seqNucs) for seqNucs in tokensSeen]
ixToTokens[motifIx].sort()
ixToTokens[motifIx].reverse()
tokensCount = sum([count for (count,_) in ixToTokens[motifIx]])
if ("consensus" in debug):
(bestCount,_) = ixToTokens[motifIx][0]
s = []
for (count,seqNucs) in ixToTokens[motifIx]:
s += ["%d:\"%s\"" % (count,seqNucs)]
print >>stderr, "# [%d]%s tokensCount=%d %s" % \
(motifIx,
" !!!" if (bestCount < winnerThreshold*tokensCount) else "",
tokensCount," ".join(s))
ixToWinners[motifIx] = [ixToTokens[motifIx][ix]
for (ix,(count,_)) in enumerate(ixToTokens[motifIx])
if (count >= winnerThreshold*tokensCount)]
# generate the possible consensus motifs
#
# at each position i, we previously determined which token (or tokens) is
# a clear winner; here we just catenate all these winners to form the
# consensus; if any position fails to have a winner we report no consensus
#
# $$$ change this to include positions that have more than one winner,
# reporting all possible paths through the winners list
motifsReported = set()
if (winnerThreshold != None):
motif = []
for motifIx in xrange(motifLen):
tokensSeen = ixToWinners[motifIx]
if (tokensSeen == []): # (no consensus can be formed, because nothing
return # .. in this column is a clear winner)
(_,bestSeqNucs) = tokensSeen[0]
if (bestSeqNucs != "-"): motif += [bestSeqNucs]
motif = "".join(motif)
if (motif not in motifsReported):
yield motif
motifsReported.add(motif)
# tolerant_reverse_complement--
def tolerant_reverse_complement(nukes):
if (nukes == None): return None
return reverse_complement(nukes)
# abort_warn_about_named_motifs--
def abort_warn_about_named_motifs(a):
print >>stderr, "ERROR: alignment at line %d contains non-ACGT:\n\"%s\"" % (a.lineNumber,a.motif)
print >>stderr, "This is probably because the alignment file uses named motifs. If so, you"
print >>stderr, "need to specify the same <name>:<motif> options on this command line as were"
print >>stderr, "provided to the NCRF alignment command."
exit()
if __name__ == "__main__": main()