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