9 #include "allele_functions.h"    10 #include "call_consensus_pileup.h"    12 const std::string gap=
"N";
    14 void format_alleles(std::vector<allele> &ad){
    15   std::vector<allele>::iterator it = ad.begin();
    18     if(it->nuc[0] == 
'-'){  
    30 bool compare_allele_depth(
const allele &a, 
const allele &b){
    31   return b.depth < a.depth;
    34 ret_t get_consensus_allele(std::vector<allele> ad, uint8_t min_qual, 
double threshold){
    42     t.nuc = (ad.at(0).nuc.compare(
"*") == 0) ? 
"" : ad.at(0).nuc;
    43     t.q = (ad.at(0).nuc.compare(
"*") == 0) ? min_qual + 33 : ad.at(0).mean_qual + 33;
    46   std::string cnuc = 
"";
    47   std::vector<allele> nuc_pos;
    50   uint32_t max_l = 0, max_depth = 0, tmp_depth = 0, cur_depth = 0, total_max_depth = 0, gap_depth = 0, total_indel_depth = 0;
    51   uint8_t ambg_n = 1, ctr = 0;
    52   double q = 0, tq = 0, cur_threshold = 0;
    53   std::vector<allele>::iterator it;
    54   for(std::vector<allele>::iterator it = ad.begin(); it != ad.end(); ++it) {
    55     if(it->nuc.length() > max_l){
    56       max_l = it->nuc.length();
    58     if(it->nuc.length() == 1){
    59       total_max_depth += it->depth;
    60       total_indel_depth += it->depth;
    61       total_indel_depth = total_indel_depth - it->end; 
    66   for (uint16_t i = 0; i < max_l; ++i){
    79       if(!(i < it->nuc.length())){
    83       if(it->nuc[i] == 
'+' || it->nuc[i] == 
'-'){
    87       tmp_depth = it->depth;
    90       while(it!=ad.end()-1 && i < (it+1)->nuc.length() && it->nuc[i] == (it+1)->nuc[i]){ 
    91     tmp_depth += (it+1)->depth;
    92     tq = ((tq * (ctr)) + (it+1)->mean_qual)/(ctr + 1);
    96       cur_depth += tmp_depth;
    97       tmp_a.nuc = it->nuc[i];
   100       tmp_a.depth = tmp_depth;
   101       nuc_pos.push_back(tmp_a);
   115     std::sort(nuc_pos.begin(), nuc_pos.end(), compare_allele_depth);
   119     max_depth = it->depth;
   123       cur_threshold = threshold * (double) total_indel_depth;
   125       cur_threshold =  threshold * (double)total_max_depth;
   127     while(it!=nuc_pos.end() && (max_depth < cur_threshold || it->depth == (it -1)->depth)){ 
   128       n = gt2iupac(n, it->nuc[0]);
   129       q = ((q * ambg_n) + it->mean_qual)/(ambg_n+1);
   131       max_depth += it->depth;
   134     if(max_depth < cur_threshold) 
   137       gap_depth = (total_indel_depth > cur_depth) ? total_indel_depth - cur_depth : 0;
   140     if(n!=
'*' && max_depth >= gap_depth){ 
   143       t.q += (((uint8_t)q)+33);
   149 int call_consensus_from_plup(std::istream &cin, std::string out_file, uint8_t min_qual, 
double threshold){
   150   std::string line, cell;
   151   std::ofstream fout(out_file+
".fa");
   152   std::ofstream tmp_qout(out_file+
".qual.txt");
   153   fout << 
">Consensus" << 
"_" << threshold <<std::endl;
   154   int ctr = 0, pos = 0, mdepth = 0;
   155   std::stringstream lineStream;
   158   std::string qualities;
   159   std::vector<allele> ad;
   160   while (std::getline(cin, line)){
   163     while(std::getline(lineStream,cell,
'\t')){
   187     ad = update_allele_depth(ref, bases, qualities, min_qual);
   188     ret_t t = get_consensus_allele(ad, min_qual, threshold);