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