+ minor changes to filterReads
[qpalma.git] / qpalma / computeSpliceAlignWithQuality.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import numpy
5 from numpy.matlib import mat,zeros,ones,inf
6 import pdb
7 from Plif import Plf,base_coord,linspace
8 import Configuration as Conf
9
10 def computeSpliceAlignWithQuality(dna, exons, est=None, quality=None,
11 qualityPlifs=None):
12 """
13 Exonpos: Anfang Exon bis 1 rechts neben Ende Exon
14 Daraus folgt: SpliceposAnfang ist gleich Ende Exon von Vorgaenger
15 (ausser dem letzten und ersten)
16 und Ende Splicestelle ist gleich Anfang Exon vom naechsten, da Splicestelleende notiert 1 rechts neben Splciestellenende
17 bsp:
18 cccccA1cccccE1cccccA2 ... AmcccccEmcccccccc
19 cccccXXcccccA1cccccE1 ... Em-1cccXXcccccccc
20 """
21
22 numberOfExons = (exons.shape)[0] # how many rows ?
23 exonSizes = [-1]*numberOfExons
24
25 #assert numberOfExons == 3
26
27 for idx in range(numberOfExons):
28 exonSizes[idx] = exons[idx,1] - exons[idx,0]
29
30 # SpliceAlign vector describes alignment:
31 # 1:donorpos, 3:intron 2:acceptorpos, 0:exon, 4: dangling end
32 SpliceAlign = []
33
34 if exons[0,0] > 0:
35 SpliceAlign.extend([4]*(exons[0,0]))
36
37 for idx in range(numberOfExons):
38 exonLength = exonSizes[idx]
39 SpliceAlign.extend([0]*exonLength)
40
41 if idx < numberOfExons-1:
42 intronLength = exons[idx+1,0] - exons[idx,1]
43 SpliceAlign.extend([1]+[3]*(intronLength-2)+[2])
44
45 if len(dna) > exons[-1,1]:
46 SpliceAlign.extend([4]*(len(dna)-exons[-1,1]))
47
48 assert len(SpliceAlign) == len(dna), pdb.set_trace()
49
50 # number of matches: just have to look at the underlying est
51 # est = dna(find(SpliceAlign==0)) # exon nucleotides
52 exonPos = [pos for pos,elem in enumerate(SpliceAlign) if elem == 0]
53 est = [elem for pos,elem in enumerate(dna) if pos in exonPos]
54
55 #length_est = sum(exon_sizes) ;
56 totalESTLength = 0
57 for elem in exonSizes:
58 totalESTLength += elem
59
60 assert totalESTLength == len(est)
61
62 sizeMatchmatrix = Conf.sizeMatchmatrix
63 # counts the occurences of a,c,g,t,n in this order
64 numChar = [0]*sizeMatchmatrix[0]
65
66 # counts the occurrences of a,c,g,t,n with their quality scores
67 trueWeightQualityMat = [0.0]*5 # 5 rows
68 for row in range(5):
69 trueWeightQualityMat[row] = [0.0]*6 # 6 col per row
70
71 for elem in est:
72 if elem == 'a':
73 numChar[0] += 1
74 if elem == 'c':
75 numChar[1] += 1
76 if elem == 'g':
77 numChar[2] += 1
78 if elem == 't':
79 numChar[3] += 1
80 if elem == 'n':
81 numChar[4] += 1
82
83 for row in range(5):
84 for col in range(6):
85 trueWeightQualityMat[row][col] = [0.0]*Conf.numQualSuppPoints # supp. points per plif
86
87 first_exon = dna[exons[0,0]:exons[0,1]]
88 second_exon = dna[exons[1,0]:exons[1,1]]
89 dna_part = first_exon + second_exon
90
91 assert len(dna_part) == len(est)
92
93 map = {'-':0, 'a':1, 'c':2, 'g':3, 't':4, 'n':5}
94
95 if Conf.mode == 'using_quality_scores':
96
97 for idx in range(len(dna_part)):
98 dnanum = map[dna_part[idx]]
99 estnum = map[est[idx]]
100 value = quality[idx]
101
102 cur_plf = qualityPlifs[(estnum-1)*6+dnanum]
103
104 Lower = len([elem for elem in cur_plf.limits if elem <= value])
105
106 if Lower == 0:
107 trueWeightQualityMat[estnum-1][dnanum][0] += 1
108 elif Lower == len(cur_plf.limits):
109 trueWeightQualityMat[estnum-1][dnanum][-1] += 1
110 else:
111 # because we count from 0 in python
112 Lower -= 1
113 Upper = Lower+1 ; # x-werte bleiben fest
114 #print value,Lower,Upper
115 weightup = 1.0*(value - cur_plf.limits[Lower]) / (cur_plf.limits[Upper] - cur_plf.limits[Lower])
116 weightlow = 1.0*(cur_plf.limits[Upper] - value) / (cur_plf.limits[Upper] - cur_plf.limits[Lower])
117
118 trueWeightQualityMat[estnum-1][dnanum][Upper] += weightup
119 trueWeightQualityMat[estnum-1][dnanum][Lower] += weightlow
120
121 trueWeightQuality = zeros((Conf.numQualSuppPoints*Conf.numQualPlifs,1))
122
123 ctr = 0
124 for row in range(5):
125 for col in range(6):
126 for p_idx in range(Conf.numQualSuppPoints):
127 #print ctr, row, col, p_idx
128 trueWeightQuality[ctr] = trueWeightQualityMat[row][col][p_idx]
129 ctr += 1
130
131 assert int(sum(trueWeightQuality.flatten().tolist()[0])) == len(est)
132
133 totalNumChar = 0
134 for idx in range(sizeMatchmatrix[0]):
135 totalNumChar += numChar[idx]
136
137 assert totalNumChar == len(est), "numChar %d, len(est) %d" % (totalNumChar,len(est))
138
139 # writing in weight match matrix
140 # matrix is saved columnwise
141 if Conf.mode == 'normal':
142 trueWeightMatch = zeros((sizeMatchmatrix[0],sizeMatchmatrix[1])) # Scorematrix fuer Wahrheit
143 for idx in range(1,sizeMatchmatrix[0]):
144 trueWeightMatch[idx,idx] = numChar[idx-1]
145
146 if Conf.mode == 'using_quality_scores':
147 trueWeightMatch = zeros((sizeMatchmatrix[0],sizeMatchmatrix[1])) # Scorematrix fuer Wahrheit
148 #for idx in range(1,sizeMatchmatrix[0]):
149 # trueWeightMatch[idx] = numChar[idx-1]
150
151 trueWeightMatch = trueWeightMatch.reshape(sizeMatchmatrix[0]*sizeMatchmatrix[1],1)
152
153 return SpliceAlign, trueWeightMatch, trueWeightQuality