+ started to extend fill_matrix in order to be able to use quality scores
[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() {
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 qualityMatrix = 0;
26 matching_plifs = 0;
27 }
28
29 void Alignment::setQualityMatrix(double* qMat, int length){
30 if(qualityMatrix == 0)
31 qualityMatrix = new double[length];
32
33 for(int i=0; i<length; i++)
34 qualityMatrix[i] = qMat[i];
35 }
36
37 void Alignment::setMatchPlifs(struct penalty_struct* match_plifs) {
38 matching_plifs = new struct penalty_struct[10];
39
40 for(int i=0; i<24; i++)
41 matching_plifs[i] = match_plifs[i];
42 }
43
44 void Alignment::getDNAEST(){}
45
46 void Alignment::myalign(int nr_paths_p, char* dna, int dna_len_p, char* est,
47 int est_len_p, struct penalty_struct h, double* matchmatrix, int mm_len,
48 double* donor, int d_len, double* acceptor, int a_len,
49 bool remove_duplicate_scores, bool print_matrix) {
50
51 // printf("Entering myalign...\n");
52 mlen = 6; // score matrix: length of 6 for "- A C G T N"
53 dna_len = dna_len_p + 1 ;
54 est_len = est_len_p + 1 ;
55 nr_paths = nr_paths_p;
56
57 /***************************************************************************/
58 // initialize alignment matrices and call fill_matrix()
59 /***************************************************************************/
60
61 //possible donor positions
62 int nr_donor_sites = 0 ;
63 for (int ii=0; ii<d_len; ii++) {
64 if(isfinite(donor[ii])) {
65 nr_donor_sites++ ;
66 }
67 }
68
69 int* donor_sites = new int[nr_donor_sites];
70 int donor_idx = 0 ;
71 for (int ii=0; ii<d_len; ii++) {
72 if(isfinite(donor[ii])) {
73 donor_sites[donor_idx] = ii+1 ;
74 donor_idx++ ;
75 }
76 }
77
78 int* max_score_positions = new int[nr_paths*2];
79
80 Pre_score** matrices = new Pre_score*[nr_paths];
81 for (int z=0; z<nr_paths; z++) {
82 matrices[z] = new Pre_score[dna_len * est_len];
83 }
84
85 // printf("calling fill_matrix...\n");
86
87 fill_matrix(nr_paths, matrices, est_len, dna_len, est, dna, &h, matchmatrix, donor, acceptor, remove_duplicate_scores, nr_donor_sites, donor_sites, max_score_positions);
88
89 // printf("after call to fill_matrix...\n");
90 /***************************************************************************/
91 // return arguments etc.
92 /***************************************************************************/
93 int result_length; //Eine Variable fuer alle Matrizen
94
95 // printf("before init of splice_align and rest...\n");
96
97 splice_align = new int[(dna_len-1)*nr_paths];
98 est_align = new int[(est_len-1)*nr_paths];
99 mmatrix_param = new int[(mlen*mlen)*nr_paths];
100 alignmentscores = new double[nr_paths]; //alignment score for each path/matrix
101
102 // printf("before memset...\n");
103
104 memset((char*)splice_align, -1, (dna_len-1)*nr_paths*sizeof(int)); // fills splice_align with zeros
105 memset((char*)est_align, -1, (est_len-1)*nr_paths*sizeof(int)); // fills est_align with zeros
106 memset((char*)mmatrix_param, 0, mlen*mlen*nr_paths*sizeof(int)); //fills mmatrix_param with zeros
107 memset(alignmentscores, -1, nr_paths*sizeof(double)); //fills alignmentscores with zeros
108
109 // printf("after memset...\n");
110 // dnaest
111 double *DNA_ARRAY = 0;
112 double *EST_ARRAY = 0;
113
114 for (int z=0; z<nr_paths; z++) {
115 result_length = 0 ;
116
117 int* s_align = splice_align + (dna_len-1)*z; //pointer
118 int* e_align = est_align + (est_len-1)*z ; //pointer
119 int* mparam = mmatrix_param + (mlen*mlen)*z; //pointer
120
121 bool no_more_path = result_align(matrices, z, est_len, dna_len, &result_length, est, dna, s_align, e_align, mparam, alignmentscores, max_score_positions);
122
123 if(DNA_ARRAY != 0) {
124 delete[] DNA_ARRAY;
125 delete[] EST_ARRAY;
126 }
127
128 DNA_ARRAY = new double[result_length];
129 EST_ARRAY = new double[result_length];
130
131 //backtracking
132 int i = max_score_positions[2*z] ; //i (est)
133 int j = max_score_positions[2*z +1] ; //j (dna)
134 int prev_i = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_i;
135 int prev_j = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_j;
136 int prev_m_no = ((Pre_score*)matrices[z] + i*dna_len +j)->prev_matrix_no;
137
138 for (int ii=result_length; ii>0; ii--) {
139 if ((prev_i == (i-1)) && (prev_j == (j-1))) { // match or mismatch
140 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
141 EST_ARRAY[ii-1] = check_char(est[i-1]) ;
142 }
143 else if ((prev_i == (i)) && (prev_j == (j-1))) {// gap on EST_ARRAY
144 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
145 EST_ARRAY[ii-1] = 0 ; //gap
146 }
147 else if ((prev_i == (i-1)) && (prev_j == (j))) { // gap on DNA_ARRAY
148 DNA_ARRAY[ii-1] = 0 ; //gap
149 EST_ARRAY[ii-1] = check_char(est[i-1]) ;
150 }
151 else {// splice site
152 for (j; j > prev_j; j--) {
153 DNA_ARRAY[ii-1] = check_char(dna[j-1]) ;
154 EST_ARRAY[ii-1] = 6 ; //intron
155 ii-- ;
156 }
157 ii++ ; // last ii-- too much (because done in for-loop)
158 }
159
160 i = prev_i;
161 j = prev_j;
162 prev_i = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_i;
163 prev_j = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_j;
164 prev_m_no = ((Pre_score*)matrices[prev_m_no] + i*dna_len + j)->prev_matrix_no;
165 }
166
167 } //end of z
168
169 if(DNA_ARRAY != 0) {
170 delete[] DNA_ARRAY;
171 delete[] EST_ARRAY;
172 }
173
174 for (int i=nr_paths-1;i>=0; i--)
175 delete[] matrices[i];
176
177 //printf("Leaving myalign...\n");
178 }
179
180 void Alignment::getAlignmentResults(int* s_align, int* e_align,
181 int* mmatrix_p, double* alignscores) {
182
183 // printf("Entering getAlignmentResults...\n");
184 uint splice_align_size = (dna_len-1)*nr_paths;
185 uint est_align_size = (est_len-1)*nr_paths;
186 uint mmatrix_param_size = (mlen*mlen)*nr_paths;
187 uint alignmentscores_size = nr_paths; //alignment score for each path/matrix
188
189 for(int idx=0; idx<splice_align_size; idx++)
190 s_align[idx] = splice_align[idx];
191
192 for(int idx=0; idx<est_align_size; idx++)
193 e_align[idx] = est_align[idx];
194
195 for(int idx=0; idx<mmatrix_param_size; idx++)
196 mmatrix_p[idx] = mmatrix_param[idx];
197
198 for(int idx=0; idx<alignmentscores_size; idx++)
199 alignscores[idx] = alignmentscores[idx];
200
201 // printf("Leaving getAlignmentResults...\n");
202 }
203