| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <math.h> |
|---|
| 5 | #include <ctype.h> |
|---|
| 6 | #include <assert.h> |
|---|
| 7 | |
|---|
| 8 | #include <arbdb.h> |
|---|
| 9 | #include <arbdbt.h> |
|---|
| 10 | #include <aw_awars.hxx> |
|---|
| 11 | #include <BI_helix.hxx> |
|---|
| 12 | |
|---|
| 13 | #include <aw_root.hxx> |
|---|
| 14 | #include <awt_tree.hxx> |
|---|
| 15 | #include <awt_csp.hxx> |
|---|
| 16 | #include "st_ml.hxx" |
|---|
| 17 | #include "st_window.hxx" |
|---|
| 18 | #include "st_quality.hxx" |
|---|
| 19 | |
|---|
| 20 | st_cq_stat::st_cq_stat(int isize){ |
|---|
| 21 | size = isize; |
|---|
| 22 | likelihoods = new double[size]; |
|---|
| 23 | square_liks = new double[size]; |
|---|
| 24 | n_elems = new int[size]; |
|---|
| 25 | int i;for (i=0;i<size;i++){ |
|---|
| 26 | n_elems[i] = 0; |
|---|
| 27 | square_liks[i] = 0; |
|---|
| 28 | likelihoods[i] = 0; |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | st_cq_stat::~st_cq_stat(){ |
|---|
| 33 | delete square_liks; |
|---|
| 34 | delete likelihoods; |
|---|
| 35 | delete n_elems; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void st_cq_stat::add(int pos,double lik){ |
|---|
| 39 | assert(pos>=0 && pos < size); |
|---|
| 40 | likelihoods[pos] += lik; |
|---|
| 41 | square_liks[pos] += lik * lik; |
|---|
| 42 | n_elems[pos] ++; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | char *st_cq_stat::generate_string(){ |
|---|
| 46 | int i; |
|---|
| 47 | double sum_lik = 0; |
|---|
| 48 | double square_sum_lik = 0; |
|---|
| 49 | int sum_elems = 0; |
|---|
| 50 | char *res = new char[size + 1]; |
|---|
| 51 | for (i=0;i<size;i++){ |
|---|
| 52 | sum_lik += likelihoods[i]; |
|---|
| 53 | square_sum_lik += square_liks[i]; |
|---|
| 54 | sum_elems += n_elems[i]; |
|---|
| 55 | res[i] = '.'; |
|---|
| 56 | } |
|---|
| 57 | res[size] = 0; |
|---|
| 58 | if (sum_elems == 0) { |
|---|
| 59 | return res; |
|---|
| 60 | } |
|---|
| 61 | double mean_lik = sum_lik / sum_elems; |
|---|
| 62 | double mean_sigma = sqrt(square_sum_lik/sum_elems - mean_lik * mean_lik ); |
|---|
| 63 | for (i=0;i<size;i++){ |
|---|
| 64 | if (n_elems[i] <=1) continue; |
|---|
| 65 | |
|---|
| 66 | double variance = mean_sigma/sqrt(n_elems[i]) ; |
|---|
| 67 | double diff = likelihoods[i]/n_elems[i] - mean_lik; |
|---|
| 68 | double val = .7 * diff / variance; |
|---|
| 69 | int ival = int(val + .5) + 5; |
|---|
| 70 | if (ival > 9 ) ival = 9; |
|---|
| 71 | if (ival < 0 ) ival = 0; |
|---|
| 72 | res[i] = '0' + ival; |
|---|
| 73 | if (res[i] == '5') res[i] = '-'; |
|---|
| 74 | } |
|---|
| 75 | return res; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | st_cq_info::st_cq_info(int seq_len,int bucket_size):ss2(2),ss5(5),ssu(seq_len/bucket_size+1), sscon(2) { |
|---|
| 79 | ; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | st_cq_info::~st_cq_info(){ |
|---|
| 83 | ; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void st_ml_add_sequence_part_to_stat(ST_ML *st_ml, AWT_csp */*awt_csp*/, |
|---|
| 87 | const char *species_name, |
|---|
| 88 | int seq_len,int bucket_size, |
|---|
| 89 | GB_HASH *species_to_info_hash, int start,int end){ |
|---|
| 90 | |
|---|
| 91 | AP_tree *node = st_ml_convert_species_name_to_node(st_ml,species_name); |
|---|
| 92 | if (!node) return; |
|---|
| 93 | ST_sequence_ml *sml = st_ml->get_ml_vectors(0,node,start,end); |
|---|
| 94 | if (!sml) return; // no statistic available |
|---|
| 95 | st_cq_info *info; |
|---|
| 96 | if (start > 0){ |
|---|
| 97 | info = (st_cq_info *)GBS_read_hash(species_to_info_hash,species_name); |
|---|
| 98 | }else{ |
|---|
| 99 | info = new st_cq_info(seq_len, bucket_size); |
|---|
| 100 | GBS_write_hash(species_to_info_hash,species_name,long(info)); |
|---|
| 101 | } |
|---|
| 102 | int pos; |
|---|
| 103 | char *source_sequence = 0; |
|---|
| 104 | int source_sequence_len = 0; |
|---|
| 105 | |
|---|
| 106 | if (sml->gb_data){ |
|---|
| 107 | source_sequence_len = GB_read_string_count(sml->gb_data); |
|---|
| 108 | source_sequence = GB_read_char_pntr(sml->gb_data); |
|---|
| 109 | } |
|---|
| 110 | if (end > source_sequence_len){ |
|---|
| 111 | end = source_sequence_len; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | ST_base_vector *vec = sml->tmp_out + start; |
|---|
| 115 | for (pos = start; pos < end;vec++,pos++){ |
|---|
| 116 | double max = 0; |
|---|
| 117 | register double v; |
|---|
| 118 | register int b; |
|---|
| 119 | for (b = ST_A; b < ST_MAX_BASE; b++) { |
|---|
| 120 | v = vec->b[b]; |
|---|
| 121 | if (v>max) max = v; |
|---|
| 122 | } |
|---|
| 123 | AWT_dna_base base = awt_dna_table.char_to_enum(source_sequence[pos]); |
|---|
| 124 | if (base != ST_UNKNOWN && base != ST_GAP){ // dont count gaps |
|---|
| 125 | double val = max / (0.0001 + vec->b[base]); |
|---|
| 126 | double log_val = log(val); |
|---|
| 127 | info->ss2.add(pos * 2/ seq_len, log_val); |
|---|
| 128 | info->ss5.add(pos * 5/ seq_len, log_val); |
|---|
| 129 | info->ssu.add(pos * info->ssu.size / seq_len, log_val); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | void st_ml_add_quality_string_to_species(GBDATA *gb_main, |
|---|
| 135 | const char *alignment_name, |
|---|
| 136 | const char *species_name, |
|---|
| 137 | int seq_len,int bucket_size, |
|---|
| 138 | GB_HASH *species_to_info_hash, |
|---|
| 139 | st_report_enum report, |
|---|
| 140 | const char *dest_field){ |
|---|
| 141 | GBDATA *gb_species = GBT_find_species(gb_main,species_name); |
|---|
| 142 | if (!gb_species) return; // invalid species |
|---|
| 143 | st_cq_info *info = (st_cq_info *)GBS_read_hash(species_to_info_hash,species_name); |
|---|
| 144 | if (!info) return; |
|---|
| 145 | GBDATA *gb_dest = GB_search(gb_species,dest_field,GB_STRING); |
|---|
| 146 | GB_ERROR error = 0; |
|---|
| 147 | if (!gb_dest){ |
|---|
| 148 | error = GB_get_error(); |
|---|
| 149 | }else{ |
|---|
| 150 | char buffer[256]; |
|---|
| 151 | char *s2 = info->ss2.generate_string(); |
|---|
| 152 | char *s5 = info->ss5.generate_string(); |
|---|
| 153 | char *su = info->ssu.generate_string(); |
|---|
| 154 | sprintf(buffer,"a%s b%s c%s",s2,s5,su); |
|---|
| 155 | delete s2; |
|---|
| 156 | delete s5; |
|---|
| 157 | error = GB_write_string(gb_dest,buffer); |
|---|
| 158 | |
|---|
| 159 | if (!error && report){ |
|---|
| 160 | GBDATA *gb_report = GBT_add_data(gb_species,alignment_name,"quality",GB_STRING); |
|---|
| 161 | if (!gb_report){ |
|---|
| 162 | error = GB_get_error(); |
|---|
| 163 | }else{ |
|---|
| 164 | char *rp = new char[seq_len+1]; rp[seq_len] = 0; |
|---|
| 165 | int i; for (i=0;i < seq_len; i++){ |
|---|
| 166 | rp[i] = su[i / bucket_size]; |
|---|
| 167 | } |
|---|
| 168 | error = GB_write_string(gb_report,rp); |
|---|
| 169 | if (report == ST_QUALITY_REPORT_TEMP){ |
|---|
| 170 | GB_set_temporary(gb_report); |
|---|
| 171 | } |
|---|
| 172 | delete rp; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | delete su; |
|---|
| 176 | } |
|---|
| 177 | if (error){ |
|---|
| 178 | aw_message(error); |
|---|
| 179 | } |
|---|
| 180 | delete info; |
|---|
| 181 | GBS_write_hash(species_to_info_hash,species_name,0); |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | GB_ERROR st_ml_check_sequence_quality(GBDATA *gb_main, |
|---|
| 186 | const char *tree_name, |
|---|
| 187 | const char *alignment_name, |
|---|
| 188 | AWT_csp *awt_csp, |
|---|
| 189 | int bucket_size, int marked_only, |
|---|
| 190 | st_report_enum report, |
|---|
| 191 | const char *filter_string, |
|---|
| 192 | const char *dest_field){ |
|---|
| 193 | AP_filter filter; |
|---|
| 194 | int seq_len = GBT_get_alignment_len(gb_main,alignment_name); |
|---|
| 195 | filter.init(filter_string,"0 ",seq_len); |
|---|
| 196 | ST_ML st_ml(gb_main); |
|---|
| 197 | GB_ERROR error = st_ml.init(tree_name, alignment_name, |
|---|
| 198 | 0, marked_only, |
|---|
| 199 | filter_string,awt_csp); |
|---|
| 200 | if (error) { |
|---|
| 201 | return error; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | GB_HASH *species_to_info_hash = GBS_create_hash(GBT_count_species(gb_main),1); |
|---|
| 205 | GB_CSTR *snames = GBT_get_species_names_of_tree((GBT_TREE *)st_ml.tree_root->tree); |
|---|
| 206 | |
|---|
| 207 | int pos; |
|---|
| 208 | aw_openstatus("Sequence Quality Check"); |
|---|
| 209 | for (pos = 0; pos < seq_len; pos += ST_MAX_SEQ_PART){ |
|---|
| 210 | int end = pos + ST_MAX_SEQ_PART; |
|---|
| 211 | if (end > seq_len) end = seq_len; |
|---|
| 212 | if (aw_status( pos/ double(seq_len))){ |
|---|
| 213 | return "aborted"; |
|---|
| 214 | } |
|---|
| 215 | const char **pspecies_name; |
|---|
| 216 | for (pspecies_name = snames; *pspecies_name; pspecies_name++){ |
|---|
| 217 | st_ml_add_sequence_part_to_stat(&st_ml, awt_csp, |
|---|
| 218 | *pspecies_name, |
|---|
| 219 | seq_len, bucket_size, |
|---|
| 220 | species_to_info_hash,pos,end); |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | aw_status("Generating Result String"); |
|---|
| 224 | const char **pspecies_name; |
|---|
| 225 | for (pspecies_name = snames; *pspecies_name; pspecies_name++){ |
|---|
| 226 | st_ml_add_quality_string_to_species(gb_main,alignment_name, |
|---|
| 227 | *pspecies_name, |
|---|
| 228 | seq_len, bucket_size, |
|---|
| 229 | species_to_info_hash, |
|---|
| 230 | report,dest_field); |
|---|
| 231 | } |
|---|
| 232 | aw_closestatus(); |
|---|
| 233 | delete snames; |
|---|
| 234 | GBS_free_hash(species_to_info_hash); |
|---|
| 235 | return NULL; |
|---|
| 236 | } |
|---|
| 237 | |
|---|