-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocMotif.py
930 lines (723 loc) · 37.3 KB
/
procMotif.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#!/usr/bin/python
__author__ = "Donghoon Lee"
__copyright__ = "Copyright 2016, Gerstein Lab"
__credits__ = ["Donghoon Lee"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Donghoon Lee"
__email__ = "[email protected]"
from Bio import motifs, SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
import math, collections, ConfigParser, glob, re
import numpy as np
from os import listdir
from os.path import isfile, join
import subprocess
### LOAD CONFIG ###
Config = ConfigParser.ConfigParser()
Config.read("./config.ini")
C_PSEUDOCOUNTS = float(Config.get("param","C_PSEUDOCOUNTS"))
C_BACKGROUND_A = float(Config.get("param","C_BACKGROUND_A"))
C_BACKGROUND_C = float(Config.get("param","C_BACKGROUND_C"))
C_BACKGROUND_G = float(Config.get("param","C_BACKGROUND_G"))
C_BACKGROUND_T = float(Config.get("param","C_BACKGROUND_T"))
C_BACKGROUND = {'A':C_BACKGROUND_A,'C':C_BACKGROUND_C,'G':C_BACKGROUND_G,'T':C_BACKGROUND_T}
C_PRECISION = int(Config.get("param","C_PRECISION"))
C_PVAL_THRESHOLD = float(Config.get("param","C_PVAL_THRESHOLD"))
###
def getPeakWithVariant(bedFile, vcfFile, outFile):
subprocess.check_call("awk -F'\t' 'BEGIN {OFS=\"\t\"}{print $1,$2,$3,$4}' "+bedFile+" | "+Config.get("app","bedtools")+" intersect -a - -b "+vcfFile+" -wa -wb > "+outFile, shell=True)
return outFile
def getFasta(fastaFile, bedFile, outFile):
subprocess.check_call(Config.get("app","bedtools")+" getfasta -fi "+fastaFile+" -bed "+bedFile+" -fo "+outFile, shell=True)
return outFile
def sortUniq(inFile, outFile):
subprocess.check_call("sort -k1,1 -k2,2n "+inFile+" | uniq > "+outFile, shell=True)
return outFile
def listOnlyFiles(path):
return [f for f in listdir(path) if isfile(join(path, f))]
def ls(path):
return glob.glob(path)
def grep(pattern, file):
output = []
with open(file) as f:
for line in f.readlines():
if re.search(pattern, line, re.IGNORECASE):
output.append(line.rstrip())
return output
def findMatchedPeak(cell, rawTF):
for filepath in ls(Config.get("data","peak_bed")+'/*.bed'):
filename = filepath.rstrip().split("/")[-1]
filename_split = filename.rstrip().split("_")
if filename_split[4] == cell and filename_split[5] == rawTF:
print "Found",filename,"for TF:",parseTF(filename_split[5])
def jaspar2pfm(jasparFile, outDir):
with open(jasparFile) as handle:
for m in motifs.parse(handle, "jaspar"):
fileName = outDir+"/"+str(m.name).replace(":","_").upper()+".pfm"
with open(fileName, "w") as output:
output.write(m.format("jaspar"))
def loadJasparMotif(path2pfm):
with open(path2pfm) as handle:
return motifs.read(handle, "jaspar")
def motif2pssm(path2motif,format):
if format == "ppm":
ppm = np.loadtxt(path2motif)
print "PPM:"
print ppm
print ""
pssm=np.log2((ppm+1E-9)/0.25)
return pssm
else:
with open(path2motif) as handle:
m = motifs.read(handle, format)
pfm = m.counts
print "PFM:"
print pfm
ppm = pfm.normalize(pseudocounts=C_PSEUDOCOUNTS)
print "PPM:"
print ppm
pssm = ppm.log_odds(background=C_BACKGROUND)
np_pssm = np.zeros(shape=(4,pssm.length))
for i, nt in enumerate(['A','C','G','T']):
np_pssm[i] = pssm[nt]
return np_pssm
def meme2pfm(memeFile,outFile):
with open(memeFile, 'r') as f:
id = ""
name = ""
w = -1
nsite = 0
motif = ""
for idx,line in enumerate(f.readlines()):
if line.startswith("MOTIF"):
name = line.split(" ")[1].rstrip()
id = line.split(" ")[2].rstrip()
print name
if line.startswith("letter-probability matrix"):
w = int(line.split(" ")[5])
nsite = int(line.split(" ")[7])
print w, nsite
elif w>0:
motif=motif+line.rstrip()+"\n"
w -= 1
elif w==0:
print motif
m=np.fromstring(motif,sep=" ")
m=np.reshape(m,(-1,4))
m=nsite*m
m=np.rint(m)
m=m.transpose()
np.savetxt(outFile,m,fmt='%i')
lines = []
lines.append(">"+id+" "+name)
seq = ["A","C","G","T"]
with open(outFile, 'r') as g:
for idx,line in enumerate(g.readlines()):
lines.append(seq[idx]+" ["+line.rstrip()+"]")
with open(outFile, 'w') as h:
for line in lines:
h.write(line+"\n")
break
def meme2pfm_all(memeFile):
with open(memeFile, 'r') as f:
version=0
id = ""
name = ""
w = -1
nsite = 0
motif = ""
for idx,line in enumerate(f.readlines()):
if line.startswith("MOTIF"):
### reset
version+=1 # version ++
motif = ""
###
name = line.split(" ")[1].rstrip()
id = line.split(" ")[2].rstrip()
print name, id
if line.startswith("letter-probability matrix"):
prefix = memeFile.split(".")[0].rstrip()+"_v"+str(version) # filename prefix
w = int(line.split(" ")[5])
nsite = int(line.split(" ")[7])
print w, nsite
elif w>0:
motif=motif+line.rstrip()+"\n"
w -= 1
elif w==0:
m=np.fromstring(motif,sep=" ")
m=np.reshape(m,(-1,4))
m=nsite*m
m=np.rint(m)
m=m.transpose()
print m
np.savetxt(prefix+".pfm",m,fmt='%i')
lines = []
lines.append(">"+id+" "+name)
seq = ["A","C","G","T"]
with open(prefix+".pfm", 'r') as g:
for idx,line in enumerate(g.readlines()):
lines.append(seq[idx]+" ["+line.rstrip()+"]")
with open(prefix+".pfm", 'w') as h:
for line in lines:
h.write(line+"\n")
w -= 1
###
def get_jaspar_motif(tfName):
with open(Config.get("data","pfm_db_jaspar")) as handle:
for m in motifs.parse(handle, "jaspar"):
if str(m.name).upper() == str(tfName).upper():
return m
# if not found
return None
def parseJaspar(tfName):
m = get_jaspar_motif(tfName)
if m == None:
return None # stop if not found
writePFM(m)
writePPM(m,C_PSEUDOCOUNTS)
writePWM(m,C_PSEUDOCOUNTS,C_BACKGROUND)
return m
def parseTF(rawName):
return rawName.rstrip().split("-")[-1]
def writePFM(m):
with open("MOTIF_"+str(m.name).upper()+".pfm", "w") as output:
for nt in ['A','C','G','T']:
for x in m.counts[nt]:
output.write(str(int(x))+" ")
output.write("\n")
def writePPM(m):
ppm = m.counts.normalize(pseudocounts=C_PSEUDOCOUNTS)
with open("MOTIF_"+str(m.name).upper()+".ppm", "w") as output:
for nt in ['A','C','G','T']:
for x in ppm[nt]:
output.write(str(round(x,3))+" ")
output.write("\n")
def writePWM(m):
ppm = m.counts.normalize(pseudocounts=C_PSEUDOCOUNTS)
pwm = ppm.log_odds(background=C_BACKGROUND)
with open("MOTIF_"+str(m.name).upper()+".pwm", "w") as output:
for nt in ['A','C','G','T']:
for x in pwm[nt]:
output.write(str(round(x,3))+" ")
output.write("\n")
def pssm2scaled_pssm(pssm):
###
### scale raw PSSM scores ###
###
# subtract by min PWM score, to make non-negative matrix
scale_const = np.min(pssm)
nonneg_pssm = pssm - scale_const
# scale PWM to range from 0 to C_PRECISION
scale_factor = (C_PRECISION/np.max(nonneg_pssm))
scaled_pssm = nonneg_pssm * scale_factor
# round to nearest integer
scaled_pssm = np.rint(scaled_pssm).astype(int)
return scaled_pssm
def scaled_pwm2scoredist(scaled_pwm):
###
### score distribution
###
### For each value in the first column of your motif, put a 1 in the corresponding entry in the first row of the matrix
### There are only 4 possible sequences of length 1
score_distribution = np.zeros(shape=(scaled_pwm.shape[1],scaled_pwm.shape[1]*C_PRECISION+1), dtype=np.int)
print np.shape(score_distribution)
###
### proc tf motif
###
# init first row
for i, nt in enumerate(['A','C','G','T']):
score_distribution[0,scaled_pwm[i,0]] += 1
# rest of motif
for j in range(1,scaled_pwm.shape[1]): ### j: 1 to length of motif
# print "MOTIF pos:", j
for k in scaled_pwm[:,j]:
for idx, count in enumerate(score_distribution[j-1,:]):
if count > 0:
score_distribution[j,idx+k] += count
return score_distribution
def pwm2pval(motifName, seq):
with open(Config.get("data","pfm_db_jaspar")) as handle:
for m in motifs.parse(handle, "jaspar"):
if str(m.name).upper() == str(motifName).upper():
ppm = m.counts.normalize(pseudocounts=C_PSEUDOCOUNTS)
pwm = ppm.log_odds(background=C_BACKGROUND)
writePWM(m)
break ### stop if motif is found
print "PWM:"
print pwm
### scale raw PWM scores
scaled_pwm = np.zeros(shape=(4,len(m)))
for i, nt in enumerate(['A','C','G','T']):
scaled_pwm[i] = pwm[nt]
# print scaled_pwm
# subtract by min PWM score, to make non-negative matrix
scale_const = np.min(scaled_pwm)
nonneg_pwm = scaled_pwm - scale_const
# scale
scale_factor = (C_PRECISION/np.max(nonneg_pwm))
scaled_pwm = nonneg_pwm * scale_factor
# round to nearest integer
scaled_pwm = np.rint(scaled_pwm).astype(int)
print "Scaled PWM:"
print scaled_pwm
### score distribution
score_distribution = np.zeros(shape=(len(m),len(m)*C_PRECISION+1), dtype=np.int)
print np.shape(score_distribution)
# init first row
for i, nt in enumerate(['A','C','G','T']):
score_distribution[0,scaled_pwm[i,0]] += 1
# print "first row:",score_distr[0,:]
# proc rest of motif
for j in range(1,len(m)): ### j: 1 to length of motif
print "MOTIF pos:", j
for k in scaled_pwm[:,j]:
for idx, count in enumerate(score_distribution[j-1,:]):
if count > 0:
score_distribution[j,idx+k] += count
scaled_score = int(seq2score(seq,scaled_pwm))
raw_score = seq2score(seq,pwm)
print "scaled score:",scaled_score
print "raw score:",raw_score
pval = score2pval(scaled_score, score_distribution)
print "pval:",pval
def seq2score(seq, pwm):
score = 0.0
seq2idx = {'A':0, 'C':1, 'G':2, 'T':3}
for idx, nt in enumerate(seq):
if nt.upper() in ['A','C','G','T']: ### some sequence contains IUPAC code other than ACGT
# print seq,idx
score += pwm[seq2idx[nt.upper()], idx]
return score
def score2pval(scaled_score, score_distribution):
return float(sum(score_distribution[-1,scaled_score:])) / float(math.pow(4,len(score_distribution)))
def pval2score(pval, score_distribution):
cumsum = 0
for idx, count in reversed(list(enumerate(score_distribution[-1,:]))):
cumsum += count
if float(cumsum) / float(math.pow(4,len(score_distribution))) > pval:
# print float(cumsum) / float(math.pow(4,len(score_distribution)))
return idx
def makeWeblogo(path2motif,format):
with open(path2motif) as handle:
getWeblogo(motifs.read(handle, format))
def getWeblogo(m):
tfName = str(m.name).upper()
tfID = m.matrix_id
pctGC = str(C_BACKGROUND_C+C_BACKGROUND_G)
m.weblogo(fname='weblogo_'+tfName+'.png', format='PNG', stack_width='large', logo_title=tfName, logo_label=tfID, percentCG=pctGC)
# unit_name='bits'
# unit_name='probability'
# the height of the y-axis is the maximum entropy for the given sequence type. (log2 4 = 2 bits for DNA/RNA, log2 20 = 4.3 bits for protein.)
def dscoreAnalysis(name, motifFile, motifFormat, bedFile, fastaFile, outFile):
print "Name:",name
print "Motif:",motifFile
print "Format:",motifFormat
print "BED:",bedFile
print "FASTA:",fastaFile
print "OUTPUT:",outFile
##############################
### 1. PROCESS TF MOTIF
##############################
### scale PSSM to non-negative integer matrix
pssm = motif2pssm(motifFile,motifFormat)
print "PSSM:"
print pssm
print ""
scaled_pssm = pssm2scaled_pssm(pssm)
print "Scaled PSSM:"
print scaled_pssm
print ""
motif_len = pssm.shape[1]
print "Motif length:",motif_len
### non-negative integer PSSM score distribution
score_distribution = scaled_pwm2scoredist(scaled_pssm)
### score threshold: discard motif with score smaller than this
score_threshold = pval2score(C_PVAL_THRESHOLD, score_distribution)
##############################
### 2. PROCESS FASTA SEQ
##############################
### LOAD FASTA SEQ
peak_seq = collections.OrderedDict()
for seq_item in SeqIO.parse(fastaFile,"fasta",alphabet=IUPAC.unambiguous_dna):
peak_seq[seq_item.id]=seq_item.seq
# print seq_item.id
print "Processing",len(peak_seq),"peaks from REF genome"
##############################
### 3. PROCESS BED
##############################
with open(outFile, 'w') as output:
with open(bedFile, 'r') as input:
for line in input.readlines():
line_split = line.rstrip().split("\t")
# print line_split
# sequence info
seq_chr = line_split[0]
seq_start = int(line_split[1])
seq_end = int(line_split[2])
skey = seq_chr+":"+str(seq_start)+"-"+str(seq_end) # 0-based
# print skey
if skey in peak_seq:
seq = peak_seq[skey]
else:
continue # handle out of bound seq range: Feature (chrM:16236-16616) beyond the length of chrM size (16571 bp). Skipping.
# print seq
# variant info
var_chr = line_split[4]
var_pos = int(line_split[5])-1 # 1-based pos to 0-based pos
var_ref = line_split[7]
var_alt = line_split[8]
# print var_chr,var_pos,var_ref,var_alt
# seq before variant
seq_prefix = seq[:var_pos-seq_start]
# seq after variant
seq_postfix = seq[var_pos+1-seq_start:]
left = max(var_pos+1-motif_len,var_pos-len(seq_prefix))
right = min(var_pos+motif_len,var_pos+1+len(seq_postfix))
# print left,right
for subseq_start in range(left,right+1-motif_len):
ucsc_coord = seq_chr+":"+str(subseq_start+1)+"-"+str(subseq_start+motif_len) # 0-based to 1-based
# print ucsc_coord
# var pos wrt to motif
motif_varpos_pos = var_pos-subseq_start+1 # 1-based variant position wrt motif
motif_varpos_neg = motif_len-var_pos+subseq_start # 1-based variant position wrt motif
# print "var pos wrt TF motif (+):",motif_varpos_pos
# print "var pos wrt TF motif (-):",motif_varpos_neg
subseq_prefix = seq[subseq_start-seq_start:var_pos-seq_start]
subseq_var = seq[var_pos-seq_start]
subseq_postfix = seq[var_pos-seq_start+1:subseq_start-seq_start+motif_len]
if subseq_var.upper() != var_ref.upper():
print "WARNING! Reference sequence mismatch: at the position",var_chr,":",var_pos+1,"reference sequence has the base",subseq_var,", but the variant has",var_ref,">",var_alt,"mutation. USCS coordinate:",ucsc_coord,subseq_prefix,subseq_var,subseq_postfix
subseq_ref_pos = subseq_prefix+var_ref.upper()+subseq_postfix
subseq_alt_pos = subseq_prefix+var_alt.upper()+subseq_postfix
if "N" in subseq_ref_pos.upper():
continue
subseq_ref_pos_print = subseq_prefix+"["+var_ref.upper()+"]"+subseq_postfix
subseq_alt_pos_print = subseq_prefix+"["+var_alt.upper()+"]"+subseq_postfix
# print subseq_ref_pos, subseq_alt_pos
# print subseq_ref_pos_print, subseq_alt_pos_print
subseq_ref_neg = subseq_ref_pos.reverse_complement()
subseq_alt_neg = subseq_alt_pos.reverse_complement()
subseq_ref_neg_print = subseq_postfix.reverse_complement()+"["+Seq(var_ref.upper(),IUPAC.unambiguous_dna).reverse_complement()+"]"+subseq_prefix.reverse_complement()
subseq_alt_neg_print = subseq_postfix.reverse_complement()+"["+Seq(var_alt.upper(),IUPAC.unambiguous_dna).reverse_complement()+"]"+subseq_prefix.reverse_complement()
# print subseq_ref_neg, subseq_alt_neg
# print subseq_ref_neg_print, subseq_alt_neg_print
### calc motif score ###
### FORWARD STRAND ###
ref_score_pos = int(seq2score(subseq_ref_pos,scaled_pssm))
alt_score_pos = int(seq2score(subseq_alt_pos,scaled_pssm))
if ref_score_pos > score_threshold or alt_score_pos > score_threshold:
ref_pval_pos = score2pval(ref_score_pos, score_distribution)
alt_pval_pos = score2pval(alt_score_pos, score_distribution)
dscore_pos = -10 * math.log10(ref_pval_pos/alt_pval_pos)
ref_rawscore_pos = seq2score(subseq_ref_pos,pssm)
alt_rawscore_pos = seq2score(subseq_alt_pos,pssm)
if abs(dscore_pos) > 0:
# print dscore_pos
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(dscore_pos)+"\t+\t"+str(ref_pval_pos)+"\t"+str(alt_pval_pos)+"\t"+str(subseq_ref_pos_print)+"\t"+str(subseq_alt_pos_print)+"\t"+str(ref_rawscore_pos)+"\t"+str(alt_rawscore_pos)+"\t"+str(motif_varpos_pos)+"\n")
### REVERSE STRAND ###
ref_score_neg = int(seq2score(subseq_ref_neg,scaled_pssm))
alt_score_neg = int(seq2score(subseq_alt_neg,scaled_pssm))
if ref_score_neg > score_threshold or alt_score_neg > score_threshold:
ref_pval_neg = score2pval(ref_score_neg, score_distribution)
alt_pval_neg = score2pval(alt_score_neg, score_distribution)
dscore_neg = -10 * math.log10(ref_pval_neg/alt_pval_neg)
ref_rawscore_neg = seq2score(subseq_ref_neg,pssm)
alt_rawscore_neg = seq2score(subseq_alt_neg,pssm)
if abs(dscore_neg) > 0:
# print dscore_neg
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(dscore_neg)+"\t-\t"+str(ref_pval_neg)+"\t"+str(alt_pval_neg)+"\t"+str(subseq_ref_neg_print)+"\t"+str(subseq_alt_neg_print)+"\t"+str(ref_rawscore_neg)+"\t"+str(alt_rawscore_neg)+"\t"+str(motif_varpos_neg)+"\n")
def bscoreAnalysis(name, motifFile, motifFormat, bedFile, fastaFile, outFile):
##############################
### 1. PROCESS TF MOTIF
##############################
### scale PSSM to non-negative integer matrix
pssm = motif2pssm(motifFile,motifFormat)
print "PSSM:"
print pssm
print ""
scaled_pssm = pssm2scaled_pssm(pssm)
print "Scaled PSSM:"
print scaled_pssm
print ""
motif_len = pssm.shape[1]
print "Motif length:",motif_len
### non-negative integer PSSM score distribution
score_distribution = scaled_pwm2scoredist(scaled_pssm)
### score threshold: discard motif with score smaller than this
score_threshold = pval2score(C_PVAL_THRESHOLD, score_distribution)
##############################
### 2. PROCESS FASTA SEQ
##############################
### LOAD FASTA SEQ
peak_seq = collections.OrderedDict()
for seq_item in SeqIO.parse(fastaFile,"fasta",alphabet=IUPAC.unambiguous_dna):
peak_seq[seq_item.id]=seq_item.seq
# print seq_item.id
print "Processing",len(peak_seq),"peaks from REF genome"
##############################
### 3. PROCESS BED
##############################
count = np.zeros((motif_len), dtype=np.int)
with open(outFile, 'w') as output:
with open(bedFile, 'r') as input:
for line in input.readlines():
line_split = line.rstrip().split("\t")
# print line_split
# sequence info
seq_chr = line_split[0]
seq_start = int(line_split[1])
seq_end = int(line_split[2])
skey = seq_chr+":"+str(seq_start)+"-"+str(seq_end) # 0-based
# print skey
if skey in peak_seq:
seq = peak_seq[skey]
else:
continue # handle out of bound seq range: Feature (chrM:16236-16616) beyond the length of chrM size (16571 bp). Skipping.
# print seq
# variant info
var_chr = line_split[4]
var_pos = int(line_split[5])-1 # 1-based pos to 0-based pos
var_ref = line_split[7]
var_alt = line_split[8]
# print var_chr,var_pos,var_ref,var_alt
# seq before variant
seq_prefix = seq[:var_pos-seq_start]
# seq after variant
seq_postfix = seq[var_pos+1-seq_start:]
left = max(var_pos+1-motif_len,var_pos+1-len(seq_prefix))
right = min(var_pos+motif_len,var_pos+len(seq_postfix))
# print left,right
for subseq_start in range(left,right+1-motif_len):
ucsc_coord = seq_chr+":"+str(subseq_start+1)+"-"+str(subseq_start+motif_len) # 0-based to 1-based
# print ucsc_coord
# var pos wrt to motif
motif_varpos_pos = var_pos-subseq_start+1 # 1-based variant position wrt motif
motif_varpos_neg = motif_len-var_pos+subseq_start # 1-based variant position wrt motif
# print "var pos wrt TF motif (+):",motif_varpos_pos
# print "var pos wrt TF motif (-):",motif_varpos_neg
subseq_prefix = seq[subseq_start-seq_start:var_pos-seq_start]
subseq_var = seq[var_pos-seq_start]
subseq_postfix = seq[var_pos-seq_start+1:subseq_start-seq_start+motif_len]
if subseq_var.upper() != var_ref.upper():
print "WARNING! Reference sequence mismatch: at the position",var_chr,":",var_pos+1,"reference sequence has the base",subseq_var,", but the variant has",var_ref,">",var_alt,"mutation. USCS coordinate:",ucsc_coord,subseq_prefix,subseq_var,subseq_postfix
subseq_ref_pos = subseq_prefix+var_ref.upper()+subseq_postfix
subseq_alt_pos = subseq_prefix+var_alt.upper()+subseq_postfix
if "N" in subseq_ref_pos.upper():
continue
subseq_pos_print = subseq_prefix+"["+var_ref.upper()+"/"+var_alt.upper()+"]"+subseq_postfix
# print subseq_ref_pos, subseq_alt_pos
# print subseq_ref_pos_print, subseq_alt_pos_print
subseq_ref_neg = subseq_ref_pos.reverse_complement()
subseq_alt_neg = subseq_alt_pos.reverse_complement()
subseq_neg_print = subseq_postfix.reverse_complement()+"["+Seq(var_ref.upper(),IUPAC.unambiguous_dna).reverse_complement()+"/"+Seq(var_alt.upper(),IUPAC.unambiguous_dna).reverse_complement()+"]"+subseq_prefix.reverse_complement()
# print subseq_ref_neg, subseq_alt_neg
# print subseq_ref_neg_print, subseq_alt_neg_print
### calc motif score ###
### FORWARD STRAND ###
ref_score_pos = int(seq2score(subseq_ref_pos,scaled_pssm))
alt_score_pos = int(seq2score(subseq_alt_pos,scaled_pssm))
if ref_score_pos > score_threshold or alt_score_pos > score_threshold:
ref_pval_pos = score2pval(ref_score_pos, score_distribution)
alt_pval_pos = score2pval(alt_score_pos, score_distribution)
# count relative position
count[motif_varpos_pos-1]+=1
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(motif_varpos_pos)+"\t+\t"+str(subseq_pos_print)+"\t"+str(ref_pval_pos)+"\t"+str(alt_pval_pos)+"\n")
### REVERSE STRAND ###
ref_score_neg = int(seq2score(subseq_ref_neg,scaled_pssm))
alt_score_neg = int(seq2score(subseq_alt_neg,scaled_pssm))
if ref_score_neg > score_threshold or alt_score_neg > score_threshold:
ref_pval_neg = score2pval(ref_score_neg, score_distribution)
alt_pval_neg = score2pval(alt_score_neg, score_distribution)
# count relative position
count[motif_varpos_neg-1]+=1
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(motif_varpos_neg)+"\t-\t"+str(subseq_neg_print)+"\t"+str(ref_pval_neg)+"\t"+str(alt_pval_neg)+"\n")
print "DONE"
print ""
print "### SUMMARY ###"
print "TOTAL:", sum(count)
print ""
print "POS\tCOUNT"
for idx, val in enumerate(count):
print str(idx+1)+"\t"+str(val)
def callMotif(name, motifFile, motifFormat, bedFile, fastaFile, outFile):
print "Name:",name
print "Motif:",motifFile
print "Format:",motifFormat
print "BED:",bedFile
print "FASTA:",fastaFile
print "OUTPUT:",outFile
##############################
### 1. PROCESS TF MOTIF
##############################
### scale PSSM to non-negative integer matrix
pssm = motif2pssm(motifFile,motifFormat)
print "PSSM:"
print pssm
print ""
scaled_pssm = pssm2scaled_pssm(pssm)
print "Scaled PSSM:"
print scaled_pssm
print ""
motif_len = pssm.shape[1]
print "Motif length:",motif_len
### non-negative integer PSSM score distribution
score_distribution = scaled_pwm2scoredist(scaled_pssm)
### score threshold: discard motif with score smaller than this
score_threshold = pval2score(C_PVAL_THRESHOLD, score_distribution)
##############################
### 2. PROCESS FASTA SEQ
##############################
### LOAD FASTA SEQ
peak_seq = collections.OrderedDict()
for seq_item in SeqIO.parse(fastaFile,"fasta",alphabet=IUPAC.unambiguous_dna):
peak_seq[seq_item.id]=seq_item.seq
# print seq_item.id
print "Processing",len(peak_seq),"peaks from REF genome"
##############################
### 3. PROCESS BED
##############################
with open(outFile, 'w') as output:
with open(bedFile, 'r') as input:
for line in input.readlines():
line_split = line.rstrip().split("\t")
# print line_split
# sequence info
seq_chr = line_split[0]
seq_start = int(line_split[1])
seq_end = int(line_split[2])
skey = seq_chr+":"+str(seq_start)+"-"+str(seq_end) # 0-based
# print skey
if skey in peak_seq:
seq = peak_seq[skey]
else:
continue # handle out of bound seq range: Feature (chrM:16236-16616) beyond the length of chrM size (16571 bp). Skipping.
# print seq
for subseq_start in range(seq_start,seq_end+1-motif_len):
ucsc_coord = seq_chr+":"+str(subseq_start+1)+"-"+str(subseq_start+motif_len) # 0-based to 1-based
# print ucsc_coord
subseq = seq[subseq_start-seq_start:subseq_start-seq_start+motif_len]
if "N" in subseq.upper():
continue
subseq_ref_pos = subseq
subseq_ref_neg = subseq_ref_pos.reverse_complement()
### call motif ###
### FORWARD STRAND ###
ref_score_pos = int(seq2score(subseq_ref_pos,scaled_pssm))
if ref_score_pos > score_threshold:
ref_pval_pos = score2pval(ref_score_pos, score_distribution)
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"\t"+str(ref_pval_pos)+"\t+\t"+str(subseq_ref_pos).upper()+"\n")
### REVERSE STRAND ###
ref_score_neg = int(seq2score(subseq_ref_neg,scaled_pssm))
if ref_score_neg > score_threshold:
ref_pval_neg = score2pval(ref_score_neg, score_distribution)
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"\t"+str(ref_pval_neg)+"\t-\t"+str(subseq_ref_neg).upper()+"\n")
def denovoAnalysis(name, motifFile, motifFormat, fastaFile, outFile):
print "Name:",name
print "Motif:",motifFile
print "Format:",motifFormat
print "FASTA:",fastaFile
print "OUTPUT:",outFile
##############################
### 1. PROCESS TF MOTIF
##############################
### scale PSSM to non-negative integer matrix
pssm = motif2pssm(motifFile,motifFormat)
print "PSSM:"
print pssm
print ""
scaled_pssm = pssm2scaled_pssm(pssm)
print "Scaled PSSM:"
print scaled_pssm
print ""
motif_len = pssm.shape[1]
print "Motif length:",motif_len
### non-negative integer PSSM score distribution
score_distribution = scaled_pwm2scoredist(scaled_pssm)
### score threshold: discard motif with score smaller than this
score_threshold = pval2score(C_PVAL_THRESHOLD, score_distribution)
##############################
### 2. PROCESS FASTA SEQ
##############################
### LOAD FASTA SEQ
peak_seq = collections.OrderedDict()
for seq_item in SeqIO.parse(fastaFile,"fasta",alphabet=IUPAC.unambiguous_dna):
peak_seq[seq_item.id]=seq_item.seq
# print seq_item.id
print "Processing",len(peak_seq),"peaks from REF genome"
with open(outFile, 'w') as output:
for skey in peak_seq.keys():
# print "skey=",skey
# sequence info
seq_chr = skey.split(":")[0]
seq_start = int(skey.split(":")[1].split("-")[0])
seq_end = int(skey.split(":")[1].split("-")[1])
seq = peak_seq[skey]
# denovo variants
var_chr = seq_chr
for var_pos in range(seq_start,seq_end): # 0-based pos
var_ref = seq[var_pos-seq_start]
for i, nt in enumerate(['A','C','G','T']):
if nt!=var_ref:
var_alt=nt
# seq before variant
seq_prefix = seq[:var_pos-seq_start]
# seq after variant
seq_postfix = seq[var_pos+1-seq_start:]
# print seq_prefix,var_ref,">",var_alt,seq_postfix
left = max(var_pos+1-motif_len,var_pos-len(seq_prefix))
right = min(var_pos+motif_len,var_pos+1+len(seq_postfix))
# print left,right,right-left
for subseq_start in range(left,right+1-motif_len):
ucsc_coord = seq_chr+":"+str(subseq_start+1)+"-"+str(subseq_start+motif_len) # 0-based to 1-based
# print "Testing",ucsc_coord,var_ref,">",var_alt
# var pos wrt to motif
motif_varpos_pos = var_pos-subseq_start+1 # 1-based variant position wrt motif
motif_varpos_neg = motif_len-var_pos+subseq_start # 1-based variant position wrt motif
# print "var pos wrt TF motif (+):",motif_varpos_pos
# print "var pos wrt TF motif (-):",motif_varpos_neg
subseq_prefix = seq[subseq_start-seq_start:var_pos-seq_start]
subseq_var = seq[var_pos-seq_start]
subseq_postfix = seq[var_pos-seq_start+1:subseq_start-seq_start+motif_len]
if subseq_var.upper() != var_ref.upper():
print "WARNING! Reference sequence mismatch: at the position",var_chr,":",var_pos+1,"reference sequence has the base",subseq_var,", but the variant has",var_ref,">",var_alt,"mutation. USCS coordinate:",ucsc_coord,subseq_prefix,subseq_var,subseq_postfix
subseq_ref_pos = subseq_prefix+var_ref.upper()+subseq_postfix
subseq_alt_pos = subseq_prefix+var_alt.upper()+subseq_postfix
if "N" in subseq_ref_pos.upper():
continue
subseq_ref_pos_print = subseq_prefix+"["+var_ref.upper()+"]"+subseq_postfix
subseq_alt_pos_print = subseq_prefix+"["+var_alt.upper()+"]"+subseq_postfix
# print subseq_ref_pos, subseq_alt_pos
# print "(+)",subseq_ref_pos_print, subseq_alt_pos_print
subseq_ref_neg = subseq_ref_pos.reverse_complement()
subseq_alt_neg = subseq_alt_pos.reverse_complement()
subseq_ref_neg_print = subseq_postfix.reverse_complement()+"["+Seq(var_ref.upper(),IUPAC.unambiguous_dna).reverse_complement()+"]"+subseq_prefix.reverse_complement()
subseq_alt_neg_print = subseq_postfix.reverse_complement()+"["+Seq(var_alt.upper(),IUPAC.unambiguous_dna).reverse_complement()+"]"+subseq_prefix.reverse_complement()
# print subseq_ref_neg, subseq_alt_neg
# print "(-)",subseq_ref_neg_print, subseq_alt_neg_print
### calc motif score ###
### FORWARD STRAND ###
ref_score_pos = int(seq2score(subseq_ref_pos,scaled_pssm))
alt_score_pos = int(seq2score(subseq_alt_pos,scaled_pssm))
if ref_score_pos > score_threshold or alt_score_pos > score_threshold:
ref_pval_pos = score2pval(ref_score_pos, score_distribution)
alt_pval_pos = score2pval(alt_score_pos, score_distribution)
dscore_pos = -10 * math.log10(ref_pval_pos/alt_pval_pos)
ref_rawscore_pos = seq2score(subseq_ref_pos,pssm)
alt_rawscore_pos = seq2score(subseq_alt_pos,pssm)
if abs(dscore_pos) > 0:
# print dscore_pos
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(dscore_pos)+"\t+\t"+str(ref_pval_pos)+"\t"+str(alt_pval_pos)+"\t"+str(subseq_ref_pos_print)+"\t"+str(subseq_alt_pos_print)+"\t"+str(ref_rawscore_pos)+"\t"+str(alt_rawscore_pos)+"\t"+str(motif_varpos_pos)+"\n")
### REVERSE STRAND ###
ref_score_neg = int(seq2score(subseq_ref_neg,scaled_pssm))
alt_score_neg = int(seq2score(subseq_alt_neg,scaled_pssm))
if ref_score_neg > score_threshold or alt_score_neg > score_threshold:
ref_pval_neg = score2pval(ref_score_neg, score_distribution)
alt_pval_neg = score2pval(alt_score_neg, score_distribution)
dscore_neg = -10 * math.log10(ref_pval_neg/alt_pval_neg)
ref_rawscore_neg = seq2score(subseq_ref_neg,pssm)
alt_rawscore_neg = seq2score(subseq_alt_neg,pssm)
if abs(dscore_neg) > 0:
# print dscore_neg
output.write(seq_chr+"\t"+str(subseq_start)+"\t"+str(subseq_start+motif_len)+"\t"+name+"_"+var_chr+":"+str(var_pos+1)+"_"+var_ref+">"+var_alt+"\t"+str(dscore_neg)+"\t-\t"+str(ref_pval_neg)+"\t"+str(alt_pval_neg)+"\t"+str(subseq_ref_neg_print)+"\t"+str(subseq_alt_neg_print)+"\t"+str(ref_rawscore_neg)+"\t"+str(alt_rawscore_neg)+"\t"+str(motif_varpos_neg)+"\n")