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