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