+ minor changes to filterReads
[qpalma.git] / dyn_prog / qpalma_dp.cpp
1
2 #include "qpalma_dp.h"
3 #include <cstring>
4
5 using namespace std;
6
7 /*[splice_align, est_align, weightMatch, alignmentscores, dnaest] = ...
8 myalign([nr_paths], dna, est, {h}, matchmatrix, donor, acceptor, remove_duplicate_scores, ...
9 print_matrix) */
10
11 Alignment::Alignment(int numQPlifs, int numq, bool use_qscores) {
12 len = 0;
13 limits = 0;
14 penalties = 0;
15 max_len = 0;
16 min_len = 0;
17 cache = 0;
18 enum ETransformType transform = T_LINEAR;
19 id = 0;
20 name = 0;
21 use_svm = 0;
22
23 // set ptrs to zero first
24 splice_align = 0;
25 est_align = 0;
26 mmatrix_param = 0;
27 alignmentscores = 0;
28 qualityFeaturesAllPaths = 0;
29 mlen = 6; // score matrix: length of 6 for "- A C G T N"
30
31 numQualSuppPoints = numq;
32 numPlifs = numQPlifs;
33 use_quality_scores = use_qscores;
34
35 //printf("number of support points: %d\n",numQualSuppPoints);
36 //printf("number of plifs: %d\n",numPlifs );
37 FA( numQualSuppPoints >= 0 );
38 FA( numPlifs >= 0 );
39 }
40
41 void Alignment::getDNAEST(){}
42
43 void Alignment::myalign(int nr_paths_p, char* dna, int dna_len_p, char* est,
44 int est_len_p, double* prb, double* chastity, struct penalty_struct h, double* matchmatrix, int mm_len,
45 double* donor, int d_len, double* acceptor, int a_len, struct penalty_struct* qualityScores,
46 bool remove_duplicate_scores, bool print_matrix) {
47
48 // printf("Entering myalign...\n");
49 dna_len = dna_len_p + 1 ;
50 est_len = est_len_p + 1 ;
51 nr_paths = nr_paths_p;
52
53 /***************************************************************************/
54 // initialize alignment matrices and call fill_matrix()
55 /***************************************************************************/
56
57 mode currentMode;
58 if (use_quality_scores)
59 currentMode = USE_QUALITY_SCORES;
60 else
61 currentMode = NORMAL;
62
63 // dnaest
64 DNA_ARRAY = 0;
65 EST_ARRAY = 0;
66
67 //printf("[qpalma_dp] read is: %s\n",est);
68
69 //possible donor positions
70 int nr_donor_sites = 0 ;
71 for (int ii=0; ii<d_len; ii++) {
72 if(isfinite(donor[ii])) {
73 nr_donor_sites++ ;
74 }
75 }
76
77 int* donor_sites = new int[nr_donor_sites];
78 int donor_idx = 0 ;
79 for (int ii=0; ii<d_len; ii++) {
80 if(isfinite(donor[ii])) {
81 donor_sites[donor_idx] = ii+1 ;
82 donor_idx++ ;
83 }
84 }
85
86 int* max_score_positions = new int[nr_paths*2];
87
88 Pre_score** matrices = new Pre_score*[nr_paths];
89 for (int z=0; z<nr_paths; z++) {
90 matrices[z] = new Pre_score[dna_len * est_len];
91 }
92
93 //printf("qualityScores\n");
94 //for(int qidx=0;qidx<numPlifs;qidx++) {
95 // penalty_struct p;
96 // p = qualityScores[qidx];
97 // printf("%d ",qidx);
98 // for(int pidx=0;pidx<p.len;pidx++)
99 // printf("%f ",pidx,p.penalties[pidx]);
100 // printf("\n");
101 //}
102
103 //int h_idx;
104 //for(h_idx=0;h_idx<h.len;h_idx++)
105 // printf("%d %f %f\n",h_idx,h.limits[h_idx],h.penalties[h_idx]);
106
107 //printf("calling fill_matrix...\n");
108 fill_matrix(nr_paths, matrices, est_len, dna_len, est, dna, prb, &h, matchmatrix, qualityScores, donor, acceptor, remove_duplicate_scores, nr_donor_sites, donor_sites, max_score_positions,currentMode);
109 //printf("after call to fill_matrix...\n");
110
111 /***************************************************************************/
112 // return arguments etc.
113 /***************************************************************************/
114 int result_length; //Eine Variable fuer alle Matrizen
115
116 splice_align_size = (dna_len-1)*nr_paths;
117 est_align_size = (est_len-1)*nr_paths;
118
119 int mmatrix_size;
120
121 if (currentMode == USE_QUALITY_SCORES) {
122 mmatrix_param_size = mlen*nr_paths;
123 mmatrix_size = mlen;
124 }
125
126 if (currentMode == NORMAL) {
127 mmatrix_param_size = (mlen*mlen)*nr_paths;
128 mmatrix_size = mlen*mlen;
129 }
130
131 alignmentscores_size = nr_paths; //alignment score for each path/matrix
132 numPathsPlifs = numPlifs*nr_paths; //alignment score for each path/matrix
133
134 splice_align = new int[splice_align_size];
135 est_align = new int[est_align_size];
136 mmatrix_param = new int[mmatrix_param_size];
137 alignmentscores = new double[nr_paths]; //alignment score for each path/matrix
138
139
140 // printf("before memset...\n");
141 memset((char*)splice_align, -1, (dna_len-1)*nr_paths*sizeof(int)); // fills splice_align with zeros
142 memset((char*)est_align, -1, (est_len-1)*nr_paths*sizeof(int)); // fills est_align with zeros
143 memset((char*)mmatrix_param, 0, mmatrix_size*nr_paths*sizeof(int)); //fills mmatrix_param with zeros
144 memset(alignmentscores, -1, nr_paths*sizeof(double)); //fills alignmentscores with zeros
145 //printf("after memset...\n");
146
147 qualityFeaturesAllPaths= new penalty_struct*[nr_paths];
148
149 for (int z=0; z<nr_paths; z++) {
150 result_length = 0 ;
151
152 int* s_align = splice_align + (dna_len-1)*z; //pointer
153 int* e_align = est_align + (est_len-1)*z ; //pointer
154 int* mparam = mmatrix_param + mmatrix_size*z; //pointer
155
156 qualityFeaturesAllPaths[z] = new penalty_struct[numPlifs];
157
158 int qidx, pidx;
159 for(qidx=0;qidx<numPlifs;qidx++) {
160 penalty_struct p;
161 init_penalty_struct(p);
162 p.len = numQualSuppPoints;
163 p.limits = (double*) calloc(p.len,sizeof(double));
164
165 for(pidx=0;pidx<p.len;pidx++)
166 p.limits[pidx] = qualityScores[qidx].limits[pidx];
167
168 p.penalties = (double*) calloc(p.len,sizeof(double));
169 qualityFeaturesAllPaths[z][qidx] = p;
170 }
171
172 //penalty_struct* qparam = qualityScoresAllPaths + (numPlifs*z);
173
174 //printf("before call to result_align...\n");
175 bool no_more_path = result_align(matrices, z, est_len, dna_len, &result_length, est, dna, prb, chastity, s_align, e_align, mparam, alignmentscores, max_score_positions, qualityFeaturesAllPaths[z] , currentMode);
176 //printf("after call to result_align...\n");
177
178 //printf("z is %d\n",z);
179 //int len;
180 //for(qidx=0;qidx<numPlifs;qidx++) {
181 // penalty_struct p;
182 // p = qualityScoresAllPaths[z][qidx];
183 // printf("%d: ",qidx);
184 // for(pidx=0;pidx<p.len;pidx++)
185 // printf("%f ",p.limits[pidx]);
186 // printf("\n");
187
188 // for(pidx=0;pidx<p.len;pidx++)
189 // printf("%f ",p.penalties[pidx]);
190 // printf("\n");
191 //}
192
193 if(z==0) {
194
195 if(DNA_ARRAY != 0) {
196 delete[] DNA_ARRAY;
197 delete[] EST_ARRAY;
198 }
199
200 result_len = result_length;
201
202 DNA_ARRAY = new int[result_length];
203 EST_ARRAY = new int[result_length];
204
205 //backtracking
206 int i = max_score_positions[2*z] ; //i (est)
207 int j = max_score_positions[2*z +1] ; //j (dna)
208 int prev_i = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_i;
209 int prev_j = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_j;
210 int prev_m_no = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_matrix_no;
211
212 for (int ii=result_length; ii>0; ii--) {
213 if ((prev_i == (i-1)) && (prev_j == (j-1))) { // match or mismatch
214 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
215 EST_ARRAY[ii-1] = check_char(est[i-1]) ;
216 }
217 else if ((prev_i == (i)) && (prev_j == (j-1))) {// gap on EST_ARRAY
218 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
219 EST_ARRAY[ii-1] = 0 ; //gap
220 }
221 else if ((prev_i == (i-1)) && (prev_j == (j))) { // gap on DNA_ARRAY
222 DNA_ARRAY[ii-1] = 0 ; //gap
223 EST_ARRAY[ii-1] = check_char(est[i-1]) ;
224 }
225 else {// splice site
226 for (j; j > prev_j; j--) {
227 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
228 EST_ARRAY[ii-1] = 6 ; //intron
229 ii-- ;
230 }
231 ii++ ; // last ii-- too much (because done in for-loop)
232 }
233
234 i = prev_i;
235 j = prev_j;
236 prev_i = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_i;
237 prev_j = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_j;
238 prev_m_no = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_matrix_no;
239 }
240
241 } // end of "if z == 0"
242
243 } //end of z
244
245 for (int i=nr_paths-1;i>=0; i--)
246 delete[] matrices[i];
247 }
248
249 void Alignment::getAlignmentResults(int* s_align, int* e_align,
250 int* mmatrix_p, double* alignscores, double* qScores) {
251
252 int idx;
253 for(idx=0; idx<splice_align_size; idx++)
254 s_align[idx] = splice_align[idx];
255
256 for(idx=0; idx<est_align_size; idx++)
257 e_align[idx] = est_align[idx];
258
259 for(idx=0; idx<mmatrix_param_size; idx++)
260 mmatrix_p[idx] = mmatrix_param[idx];
261
262 for(idx=0; idx<alignmentscores_size; idx++)
263 alignscores[idx] = alignmentscores[idx];
264
265 if (use_quality_scores) {
266 penalty_struct currentPlif;
267 int ctr=0;
268 for (int z=0; z<nr_paths; z++) {
269 for(int estChar=1;estChar<6;estChar++) {
270 for(int dnaChar=0;dnaChar<6;dnaChar++) {
271
272 int currentPos = (estChar-1)*6+dnaChar;
273 currentPlif = qualityFeaturesAllPaths[z][currentPos];
274
275 for(int pidx=0; pidx<currentPlif.len; pidx++) {
276 qScores[ctr] = currentPlif.penalties[pidx];
277 //printf("%f ",qScores[ctr]);
278 ctr++;
279 }
280 //printf("\n");
281 }}}
282
283 //printf("\nctr is %d\n",ctr);
284 }
285 //printf("Leaving getAlignmentResults...\n");
286 }
287
288 void Alignment::getAlignmentArrays(int* dna_align, int* est_align) {
289
290 int idx;
291 for(idx=0; idx<result_len; idx++) {
292 dna_align[idx] = DNA_ARRAY[idx];
293 est_align[idx] = EST_ARRAY[idx];
294 }
295 }