proteome_v2.h 46.4 KB
Newer Older
David Trudgian's avatar
David Trudgian committed
1 2 3 4 5 6
#ifndef PROTEOME_V2_H
#define PROTEOME_V2_H

#include <iostream>
#include <fstream>
#include <vector>
7
#include <algorithm>
David Trudgian's avatar
David Trudgian committed
8 9 10
#include <set>
#include <cstdlib>
#include <sstream>
11
#include <iomanip>
David Trudgian's avatar
David Trudgian committed
12 13 14 15 16 17 18 19 20 21 22 23
#include <cmath>

#include "protein.h"
#include "amino_acids.h"
#include "util.h"

const bool STOREPROTEINS=false;
const bool STOREKMERS=false;
const double KEEPERS_CUTOFF=0.6;
const bool SUMOFSQUARES=true;
const double CONSERVATION_CUTOFF=1.3;
const bool PROTEOME_DEBUG=false;
24

David Trudgian's avatar
David Trudgian committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
const int MAXORDDIGITS=5;

using namespace std;

struct kmer
{
  string s;
  int source_protein_index;
};

struct orthscorer
{
  int gnums;
  int gis;
  bool keeper; //keeper > deleter (if keeper=true and deleter=true, keep it)
  bool deleter;
  int internal_repeats; //This may as well be an integer and not a bool.
};

struct osv
{
  bool keeper;
  bool deleter;
  bool internal_repeats;
  vector<int> gnums;
  vector<int> gis;
};

class proteome_v2
{
 private:
  int size_real_tags;
  double discrepancy;
  int conserved;
  int tag_length;
  int chosen_column; //When filtering and choosing a column from pvv automatically.
  bool ref;
  int total_num_proteins; //Set to 0 in constructor. Keeps the value whether or not proteins vector still is storing the proteins. Useful in filter.h code.
  int filtering_steps; //  default [0,10] in which 4 means 40% representation in reference set proteins.
  util u;
  int sos_cutoff;

  vector<string> tags;
  vector<string> tags_scr;
  //vector<string> tags_rev;
  vector<protein> proteins;
  vector<orthscorer> pv;
  vector<osv> pvv;
  vector<int> sosv;

  void populate_proteins(string file);
  void populate_kmers();
  bool is_valid(string s);
  amino_acids aas;
  double get_ratio(int pindex);
  double get_ratio(int pindex,int index2);
  string scramble (string s);
  string scramble_v2 (string s); //this one uses a particular composition of singles, doubles, and triples.
  bool keeper_present(string s1,string s2);
  int get_pvv_index(double d);
  void final_pvv_update();

 public:
  proteome_v2(); 
  void set_tag_length(int tag_length);
  void set_size_real_tags(int i);
  void set_ref(bool b);
  void set_discrepancy(double d);
  void set_conserved(int c);
  void set_chosen_column(int i);
  void set_pv_size(int i);
  void set_pvv_size(int i);
  void set_pv_val(int index,int i1, int i2, int i3, int i4, int i5);
  void set_pvv_gnums_val(int index,vector<int> vals);
  void set_pvv_gis_val(int index,vector<int> vals);
  void set_keeper(int index,int i);
  void set_deleter(int index,int i);
  void set_internal_repeats(int index, int i);
  void set_sosv_size(int i);
  void set_sosv_vals(vector<int> vals);
  void set_filtering_steps(int fs);

  void set_total_num_proteins(); //Not exactly setting it here (no parameter): Having read all the proteins in, set the variable total_num_proteins according to the size of proteins vector which will shortly be empty again.

  int get_tags_size();
  vector<string> get_tags();
  vector<string> get_scr_tags();
  int get_total_num_proteins();

  void add_protein(string info_line, string aastr);
  void generate_tags(int ordinal,bool do_scrambled);
  void generate_scrambled_tags(int ordinal);
  void clear_tags();
  void clear_scr_tags();
  void mark_keepers(vector<string> keepers);
  bool check_composition(string s);
  void mark_gene_for_deletion(int index);
  void mark_gene_internal_repeats(int index);
  void write_out_gene(int index);
  void print_protein_info(int i);  
  void clear_proteins();
  void add_discr(int instances, double avg_rep);
  double get_discr();
  void count_conserved();
  int get_conserved();
  //void mark_gene(string s, int hit_genome_id);
  //int find_gene(string s);
  //void write_out_proteins();
  //void write_out_marked_proteins(string path_par);
  //void query_proteome_v2_goi(vector<string> sv, int tag_length);
  //int get_size();
  //string get_protein_s(int i);
  //string get_protein_il(int i);
  //int get_ordinal();
  //void write_genes_with_duplicates();
  void write_out_new_proteome_v2(string path,string directory,vector<string> files,int ordinal,vector<pair<int,int> > mscv,int index);
  //void write_out_new_proteome_v2_v2();
  void increment_ggs(int i, int gnum_i, int gene_id_i);
  void increment_ggs(int i, int gnum_i, int gene_id_i,double d);
  void count_filtered_proteomes(vector<int>& num_proteins);
  //void quicksort_v2(vector<protein>& pv, int left, int right);
  //void reset_deleters();
  void write_info_v1(ofstream& outstream);
  void wokl(int id, ofstream& outstream);
  void write_filt_set(ofstream& outstream, int filt_option, int ordinal, string path,vector<string> files);
  //void mark_keepers(string s,ofstream& outstream);
};

proteome_v2::proteome_v2()
{ 
  //int sosv_size = tag_length*tag_length+1;
  //  for(int i=0; i<401; i++)
  //for(int i=0; i<sosv_size; i++)
  //    {
  //      sosv.push_back(0);
  //    }
161

David Trudgian's avatar
David Trudgian committed
162 163 164
  discrepancy=0;
  conserved=0;
  filtering_steps=10.0; //Default.
165 166 167 168 169

  // DCT - Wasn't initialized. Causes error later in mdist reading org file.
    chosen_column = 0;
    ref = false;

David Trudgian's avatar
David Trudgian committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
}

int proteome_v2::get_tags_size()
{
  //return kmers.size();
  return tags.size();
}

void proteome_v2::mark_keepers(vector<string> keepers)
{
  cout<<"In mark_keepers. size of keepers = "<<keepers.size()<<endl;
  cout<<"proteins.size() = "<<proteins.size()<<endl;

  for(int i=0; i<proteins.size(); i++)
    {
      for(int j=0; j<proteins.at(i).get_sequence().length(); j++)
	{
	  for(int f=0; f<keepers.size(); f++)
	    {
	      //	      if(keepers.at(f)==proteins.at(i).get_sequence().substr(j,keepers.at(f).length()))
	      if(keeper_present(keepers.at(f),proteins.at(i).get_sequence().substr(j,keepers.at(f).length())))
		{
		  cout<<"Marking keeper: i="<<i<<endl<<keepers.at(f)<<endl<<proteins.at(i).get_info_line()<<endl<<proteins.at(i).get_sequence()<<endl;
		  proteins.at(i).mark_keeper();
		  pv.at(i).keeper=true;
		  cout<<"pv.at(i)="<<pv.at(i).keeper<<endl;
		  //cout<<"pv.at(i+1)="<<pv.at(i+1).keeper<<endl;
		  //cout<<"no"<<endl;
		  pvv.at(i).keeper=true;
		  cout<<"Marked keeper."<<endl;
		}
	    }
	}
    }
}

void proteome_v2::set_keeper(int index, int i)
{
  if(index>=0 && index<pvv.size())
    {
      pvv.at(index).keeper=i;
    }
  else
    {
      cout<<"proteome_v2::set_keeper(int,int)"<<endl;
      cout<<"index="<<index<<" i="<<i<<endl;
      cout<<"pvv.size() = "<<pvv.size()<<endl;
      exit(1);
    }
}

void proteome_v2::set_deleter(int index, int i)
{
  pvv.at(index).deleter=i;
}

void proteome_v2::set_internal_repeats(int index, int i)
{
  pvv.at(index).internal_repeats=i;
}

void proteome_v2::set_filtering_steps(int fs)
{
  filtering_steps = fs;
}

void proteome_v2::set_sosv_size(int i)
{
  if(sosv.size()==0)
    {
      for(int q=0; q<i; q++)
	{
	  sosv.push_back(0);
	}  
    }
  else if(sosv.size()==i)
    {
      //      cout<<"sosv already initialized"<<endl;
    }
  else
    {
      cout<<"proteome_v2::set_sosv_size(int)"<<endl;
      cout<<"i = "<<i<<endl;
253
      cout<<"Current sosv.size: " << sosv.size() << endl;
David Trudgian's avatar
David Trudgian committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
      cout<<"Error condition"<<endl;
      exit(1);
    }
}

void proteome_v2::set_sosv_vals(vector<int> vals)
{
  if(vals.size() != sosv.size())
    {
      cout<<"proteome_v2::set_sosv_vals(vector<int>) : Error condition"<<endl;
      cout<<"vals.size() = "<<vals.size()<<endl;
      cout<<"sosv.size() = "<<sosv.size()<<endl;
      exit(1);
    }
  else
    {
      for(int i=0; i<vals.size(); i++)
	{
	  sosv.at(i)=vals.at(i);
	}
    }
}

void proteome_v2::set_ref(bool b)
{
  ref=b;
}

void proteome_v2::set_discrepancy(double d)
{
  discrepancy=d;
}

void proteome_v2::set_conserved(int c)
{
  conserved=c;
}

void proteome_v2::set_pvv_size(int i)
{
  /*  cout<<"In proteome_v2::set_pvv_size(int i)"<<endl;
  cout<<"i = "<<i<<endl;
  cout<<"filtering_steps = "<<filtering_steps<<endl;
  exit(1);*/
  for(int q=0; q<i; q++)
    {
      osv osvtemp;
      osvtemp.keeper=osvtemp.deleter=osvtemp.internal_repeats=0;
      for(int k=0; k<=filtering_steps; k++)
	{
	  osvtemp.gnums.push_back(0);
	  osvtemp.gis.push_back(0);
	}
      pvv.push_back(osvtemp);
    }
}

void proteome_v2::set_pv_size(int i)
{
  for(int q=0; q<i; q++)
    {
      orthscorer ostemp;
      ostemp.gnums=ostemp.gis=ostemp.keeper=ostemp.deleter=ostemp.internal_repeats=0;
      pv.push_back(ostemp);
    }
}

void proteome_v2::set_pv_val(int index,int i1, int i2, int i3, int i4,int i5)
{
  pv.at(index).gnums=i1;
  pv.at(index).gis=i2;
  pv.at(index).keeper=i3;
  pv.at(index).deleter=i4;
  pv.at(index).internal_repeats=i5;
}

void proteome_v2::set_pvv_gis_val(int index,vector<int> vals)
{
  if(index>=0 && index<pvv.size() && vals.size()==pvv.at(index).gis.size())
    {
      for(int i=0; i<vals.size(); i++)
	{
	  pvv.at(index).gis.at(i)=vals.at(i);
	}
    }
  else
    {
      cout<<"proteome_v2::set_pvv_gis_val(int,vector<int>) : Error condition"<<endl;
      exit(1);
    }  
}

void proteome_v2::set_pvv_gnums_val(int index,vector<int> vals)
{
  if(index>=0 && index<pvv.size() && vals.size()==pvv.at(index).gnums.size())
    {
      for(int i=0; i<vals.size(); i++)
	{
	  pvv.at(index).gnums.at(i)=vals.at(i);
	}
    }
  else
    {
      cout<<"proteome_v2::set_pvv_gnums_val(int,vector<int>) : Error condition"<<endl;
      cout<<"index="<<index<<endl<<"pvv.size()="<<pvv.size()<<endl<<"vals.size()="<<vals.size()<<endl<<"pvv.at(index).gnums.size()="<<pvv.at(index).gnums.size()<<endl;
      exit(1);
    }
}

void proteome_v2::generate_tags(int ordinal,bool do_scrambled)
{
365 366 367 368

  stringstream sso;
    sso << setfill('0') << setw(MAXORDDIGITS) << ordinal;
  string so = sso.str();
David Trudgian's avatar
David Trudgian committed
369 370 371

  for(int i=0; i<proteins.size(); i++)
    {
372 373 374 375 376

        stringstream ss;
        ss<<i;
        string is = ss.str();

David Trudgian's avatar
David Trudgian committed
377 378 379
      for(int j=0; j<proteins.at(i).get_sequence().length(); j++)
	{
	  string s = proteins.at(i).get_sequence().substr(j,tag_length);
380

David Trudgian's avatar
David Trudgian committed
381 382
	  if(is_valid(s))
	    {
383 384 385 386 387

          if(s.length() < tag_length){
              s.append(tag_length - s.length(), '^');
          }

David Trudgian's avatar
David Trudgian committed
388 389
	      s += " "+so+" "+is;
	      tags.push_back(s);
390

David Trudgian's avatar
David Trudgian committed
391 392 393 394 395 396 397 398 399
	    }
	}

      orthscorer ostemp;
      ostemp.gnums=0;
      ostemp.gis=0;
      ostemp.keeper=false;
      ostemp.deleter=false;
      ostemp.internal_repeats=0;
400

David Trudgian's avatar
David Trudgian committed
401 402 403 404 405 406 407 408 409 410 411 412 413
      pv.push_back(ostemp);

      osv ostemp2;
      ostemp2.keeper=false;
      ostemp2.deleter=false;
      ostemp2.internal_repeats=0;

      //We may not have to store all of these; better to add only as needed. Change later?
      for(int oindex=0; oindex<=filtering_steps; oindex++)
	{
	  ostemp2.gnums.push_back(0);
	  ostemp2.gis.push_back(0);
	}
414

David Trudgian's avatar
David Trudgian committed
415 416 417 418 419 420 421 422 423 424
      pvv.push_back(ostemp2);

      if(do_scrambled)
	{
	  string scrambled = scramble_v2(proteins.at(i).get_sequence());
	  for(int j=0; j<scrambled.length(); j++)
	    {
	      string s = scrambled.substr(j,tag_length);
	      if(is_valid(s))
		{
425 426 427
            if(s.length() < tag_length){
                s.append(tag_length - s.length(), '^');
            }
David Trudgian's avatar
David Trudgian committed
428 429 430 431
		  stringstream ss;
		  ss<<i;
		  string is = ss.str();
		  s += " "+so+" "+is;
432

David Trudgian's avatar
David Trudgian committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		  tags_scr.push_back(s);
		}
	    }
	}
    }

  //proteins.clear(); //can't store it, uses too much memory.

  if(PROTEOME_DEBUG)
    {
      cout<<"in proteome_v2::generate_tags(int,bool)"<<endl;
      cout<<"Size of tags = "<<tags.size()<<endl;
      cout<<"tags_scr.size() = "<<tags_scr.size()<<endl;
    }

  srand(time(NULL));

  while(tags_scr.size()>tags.size())
    {
      int r = rand()%tags_scr.size();
      if(r>=0 && r<tags_scr.size())
	{
	  tags_scr.erase(tags_scr.begin()+r);
	}
    }

459 460 461
    std::sort(tags.begin(), tags.end());


David Trudgian's avatar
David Trudgian committed
462 463 464

  if(do_scrambled)
    {
465
        std:sort(tags_scr.begin(), tags_scr.end());
David Trudgian's avatar
David Trudgian committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    }
  else
    {
      cout<<"Did not generate scrambled tags. do_scrambled="<<do_scrambled<<endl;
    }

  size_real_tags=tags.size();
}

string proteome_v2::scramble(string s)
{
  //s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //for testing.
  
  srand(time(NULL));
  string result;

  vector<string> pieces;
  pieces.push_back(s);

  while(pieces.size()>0)
    {
      int pindex=rand()%pieces.size();
      int plength=rand()%5;
      plength++; //because plength==0 makes no sense.

      if(pindex>=0 && pindex<pieces.size())
	{
	  int sindex=rand()%pieces.at(pindex).length();
	  result+=pieces.at(pindex).substr(sindex,plength);

	  if(sindex==0 && plength>=pieces.at(pindex).length())
	    {
	      pieces.erase(pieces.begin()+pindex);
	    }
	  else if(sindex==0)
	    {
	      pieces.at(pindex)=pieces.at(pindex).substr(sindex+plength,pieces.at(pindex).length());
	    }
	  else if( (sindex+plength)>=pieces.at(pindex).length())
	    {
	      pieces.at(pindex).erase(sindex,pieces.at(pindex).length());
	    }
	  else
	    {
	      string npiece=pieces.at(pindex).substr(sindex+plength,pieces.at(pindex).length());
	      if(npiece.length()>0)
		{
		  pieces.push_back(npiece);
		}
	      pieces.at(pindex).erase(sindex,pieces.at(pindex).length());
	    }
	}
      else
	{
	  cout<<"pindex out of bounds in proteome_v2::scramble(string). s = "<<s<<" pindex="<<pindex<<" pieces.size() = "<<pieces.size()<<endl;
	  exit(1);
	}
    }

  if(result.length()!=s.length())
    {
      //      cout<<"in proteome_v2::scramble(string). result.length()!=s.length()."<<endl;
      exit(1);
    }
  else
    {
      //     cout<<"in proteome_v2::scramble(string). result and s have same length = "<<result.length()<<endl;
    }

  return result;
}

string proteome_v2::scramble_v2(string s)
{
  //s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //for testing.  
  srand(time(NULL));
  string result;

  vector<string> pieces;
  pieces.push_back(s);

  int pivot=4;

  while(pieces.size()>0)
    {
      int pindex=rand()%pieces.size();     
      //int plength=(rand()%10)+1; //plength = [1,10]
      int plength=(rand()%pivot)+1; //plength = [1,pivot]
      /*      if(plength<=pivot)
	{
	  plength=1;
	}
      else
	{
	  plength=1;
	}
      */
      plength=1; //while I debug the msc problem 12/01/2014

      if(pindex>=0 && pindex<pieces.size())
	{
	  int sindex=rand()%pieces.at(pindex).length(); //[0,length())
	  result+=pieces.at(pindex).substr(sindex,plength);

	  if(sindex==0 && plength>=pieces.at(pindex).length())
	    {
	      pieces.erase(pieces.begin()+pindex);
	    }
	  else if(sindex==0)
	    {
	      pieces.at(pindex)=pieces.at(pindex).substr(sindex+plength,pieces.at(pindex).length());
	    }
	  else if( (sindex+plength)>=pieces.at(pindex).length())
	    {
	      pieces.at(pindex).erase(sindex,pieces.at(pindex).length());
	    }
	  else
	    {
	      string npiece=pieces.at(pindex).substr(sindex+plength,pieces.at(pindex).length());
	      if(npiece.length()>0)
		{
		  pieces.push_back(npiece);
		}
	      pieces.at(pindex).erase(sindex,pieces.at(pindex).length());
	    }
	}
      else
	{
	  cout<<"pindex out of bounds in proteome_v2::scramble(string). s = "<<s<<" pindex="<<pindex<<" pieces.size() = "<<pieces.size()<<endl;
	  exit(1);
	}
    }

  if(result.length()!=s.length())
    {
      cout<<"in proteome_v2::scramble(string). result.length()!=s.length()."<<endl;
      exit(1);
    }
  else
    {
      //      cout<<"in proteome_v2::scramble(string). result and s have same length = "<<result.length()<<endl;
    }

  return result;
}


void proteome_v2::generate_scrambled_tags(int ordinal)
{
  tags.clear(); //just in case.
  stringstream ss1;
  ss1<<ordinal;
  string so=ss1.str();

  for(int i=0; i<proteins.size(); i++)
    {
622 623 624 625 626

        stringstream ss;
        ss<<i;
        string is = ss.str();

David Trudgian's avatar
David Trudgian committed
627 628 629 630 631 632 633 634
      string q = proteins.at(i).get_sequence();
      scramble_v2(q);

      for(int j=0; j<q.length(); j++)
	{
	  string s = q.substr(j,tag_length);
	  if(is_valid(s))
	    {
635 636 637 638
            if(s.length() < tag_length){
                s.append(tag_length - s.length() ,'^');
            }

David Trudgian's avatar
David Trudgian committed
639 640 641 642 643 644 645 646 647 648 649
	      s += " "+so+" "+is;
	      //cout<<"s = "<<s<<endl;
	      tags.push_back(s);
	    }
	}
    }
  proteins.clear(); //can't store it, uses too much memory.
  cout<<"Size of tags = "<<tags.size()<<endl;
  cout<<"Size of size_real_tags = "<<size_real_tags<<endl;
  exit(1);
  cout<<"Sorting tags: "<<endl;
650
    std::sort(tags.begin(), tags.end());
David Trudgian's avatar
David Trudgian committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  cout<<"Done sorting. First elements = "<<tags.at(0)<<endl<<tags.at(1)<<endl<<tags.at(2)<<endl;
}

void proteome_v2::clear_tags()
{
  tags.clear();
}

void proteome_v2::clear_scr_tags()
{
  tags_scr.clear();
}

vector<string> proteome_v2::get_tags()
{
  return tags;
}

vector<string> proteome_v2::get_scr_tags()
{
  return tags_scr;
}

/*
void proteome_v2::read_proteins(string infile)
{
677
  populate_proteins(infile);
David Trudgian's avatar
David Trudgian committed
678 679 680 681
  for(int i=0; i<proteins.size(); i++)
    {
      proteins.at(i).set_id(i);
    }
682

David Trudgian's avatar
David Trudgian committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
  populate_kmers();
  cout<<"size of kmers = "<<kmers.size()<<endl;
}

*/

void proteome_v2::add_protein(string info_line, string aaseq)
{
  protein newprotein;
  newprotein.set_sequence(aaseq);
  newprotein.set_info_line(info_line);
  proteins.push_back(newprotein);
}

/*
  void proteome_v2::mark_gene(string s, int hit_genome_id)
  {
  cout<<"In mark_gene s="<<s<<" hit_genome_id="<<hit_genome_id<<endl;
701

David Trudgian's avatar
David Trudgian committed
702 703 704 705
  int bottom=0;
  int top=kmers.size();
  int middle=top/2;
  int former_middle=-1;
706

David Trudgian's avatar
David Trudgian committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  bool found=false;
  while(!found && former_middle!=middle)
  {
  if(kmers.at(middle).s>s) //YYYAS>AACSD
  {
  top=middle;
  former_middle=middle;
  middle=(top-bottom)/2+bottom;
  }
  else if(kmers.at(middle).s<s)//AACSD<YYYAS
  {
  bottom=middle;
  former_middle=middle;
  middle=(top-bottom)/2+bottom;
  }
  else if(kmers.at(middle).s==s)
  {
724
  found=true;
David Trudgian's avatar
David Trudgian committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
  proteins.at(kmers.at(middle).source_protein_index).add_hit(hit_genome_id);
  cout<<"protein info: "<<proteins.at(kmers.at(middle).source_protein_index).get_genome_hits_size()<<endl;
  cout<<"The code is making it in here. kmer = "<<s<<endl;
	}
    }
  
  if(!found)
    {
      cout<<"Not found, but I'm okay with that now."<<endl;
      cout<<"The ghost string = "<<s<<endl;
      for(int i=0; i<kmers.size(); i++)
	{
	  if(s == kmers.at(i).s.substr(0,s.length()) )//kmers.at(i).s==s)
	    {
	      cout<<"But it is in there! i = "<<i<<" out of kmers.size() = "<<kmers.size()<<endl;
	      exit(1);
	    }
	}
      
    }
}

void proteome_v2::populate_proteins(string file)
{
  ifstream instream;
  string infile = path+"/"+dir+"/"+directory+"/"+file;
  cout<<"infile = "<<infile<<endl;
  
  instream.open(infile.c_str());
  if(instream.fail())
    {
      cout<<"Failed to open file "<<file<<" in proteome_v2::populate_proteins(string)"<<endl;
      exit(1);
    }
  
  string eater;
  instream>>eater;
  while(!instream.eof())
    {
      if(eater[0]=='>')
	{
	  char buffer[1000];
	  instream.getline(buffer,1000);
	  string info_line = eater+buffer;
	  
	  instream>>eater;
	  string ps;
	  while(eater[0]!='>' && !instream.eof())
	    {
	      ps = ps+eater;
	      instream>>eater;
	    }
	  
	  protein protemp;
	  protemp.set_sequence(ps);
	  protemp.set_info_line(info_line);
	  proteins.push_back(protemp);
	}
    }
  instream.close();
}

void proteome_v2::populate_kmers()
{
  for(int i=0; i<proteins.size(); i++)
    {
      string p = proteins.at(i).get_sequence();
      for(int j=0; j<p.length(); j++)
	{
	  string s = p.substr(j,20);
	  if(s.length()==20)
	    {
	      kmer ktemp;
	      ktemp.s=s;
	      ktemp.source_protein_index=i;
	      kmers.push_back(ktemp);
	    }
	}
    }

  quicksort(kmers,0,kmers.size()-1);

  for(int i=0; i<kmers.size()-1; i++)
    {
      if(kmers.at(i).s>kmers.at(i+1).s)
	{
	  cout<<"Problem with the kmers sort"<<endl;
	  exit(1);
	}
    }
}

void proteome_v2::write_out_proteins()
{
  for(int i=0; i<proteins.size(); i++)
    {
      if(proteins.at(i).get_genome_hits_size()>0)
	{
	  cout<<proteins.at(i).get_info_line()<<endl;
	  cout<<proteins.at(i).get_sequence()<<endl;
	  //	  proteins.at(i).write_genome_hits();
	}
    }
}

void proteome_v2::quicksort(vector<kmer>& kv, int left, int right)
{
  int i=left;
  int j=right;
  
  string pivot=kv.at((left+right)/2).s;

  while(i<=j)
    {
      while(pivot>kv.at(i).s)
	{
	  i++;
	}
      while(pivot<kv.at(j).s)
	{
	  j--;
	}
      if(i<=j)
	{
	  kmer temp = kv.at(i);
	  kv.at(i) = kv.at(j);
	  kv.at(j) = temp;
	  i++;
	  j--;
	}
    };

  if(left<j)
    quicksort(kv,left,j);
  if(i<right)
    quicksort(kv,i,right);      
}

void proteome_v2::write_out_marked_proteins(string path_par)
{
  ofstream outstream;
  outstream.open( (path_par+"/gene_list.txt").c_str(), ios::out | ios::app);
  if(outstream.fail())
    {
      cout<<"Failed to open outstream in write_out_marked_proteins()"<<endl;
      exit(1);
    }
  
  int count_total_proteins=0;
  outstream<<"ordinal = "<<ordinal<<endl<<"directory = "<<directory<<" "<<endl<<"org_name = "<<org_name<<endl;
  outstream<<"marked proteins:"<<endl;
  for(int i=0; i<proteins.size(); i++)
    {
      if(proteins.at(i).get_genome_hits_size()>0)
	{
	  outstream<<proteins.at(i).get_info_line()<<endl;
	  outstream<<proteins.at(i).get_sequence()<<endl;
	  outstream<<proteins.at(i).get_gnums_count()<<endl;
	  outstream<<proteins.at(i).get_geneids_count()<<endl;
	  proteins.at(i).write_genome_hits(outstream);
	  count_total_proteins++;
	}
    }
  cout<<"Number of proteins with get_genome_hits_size()>0 = "<<count_total_proteins<<endl<<endl;
}

void proteome_v2::query_proteome_v2_goi(vector<string> sv, int tag_length_par)
{
  for(int j=0; j<sv.size(); j++)
    {
      ofstream gstream;
      string genefile = path+"/"+bfn+"/LepA_first_attempt.txt";
      gstream.open( genefile.c_str());
      if(gstream.fail())
	{
	  cout<<"Failed to open genestream to "<<genefile<<endl;
	  exit(1);
	}
      for(int k=0;k<sv.at(j).length(); k++)
	{
	  string l = sv.at(j).substr(k,tag_length_par);
	  if(l.length()==tag_length)
	    {
	      int kmer_index=find_gene(l);
	      if(kmer_index>=0)
		{
		  int tentative=kmer_index-1;
		  while(tentative>=0 && kmers.at(tentative).s==l)
		    {
		      kmer_index-=1;
		      tentative=kmer_index-1;
		    }
		  
		  int count_matches=0;
		  int min=kmer_index;
		  int max=kmer_index;
		  tentative=kmer_index;
		  while( (tentative+1)<kmers.size() && kmers.at(tentative+1).s==l)
		    {
		      tentative++;
		      max=tentative;
		      count_matches++;
		    }
		  cout<<"count in query proteome_v2 for string s = "<<l<<" = "<<count_matches<<endl;
		  exit(1);
		} 
	    }
	}
    }
}

int proteome_v2::find_gene(string s)
{
  //cout<<"In mark_gene s="<<s<<" hit_genome_id="<<hit_genome_id<<endl;
  
  int bottom=0;
  int top=kmers.size();
  int middle=top/2;
  int former_middle=-1;
  
  bool found=false;
  while(!found && former_middle!=middle)
    {
      if(kmers.at(middle).s>s) //YYYAS>AACSD
	{
	  top=middle;
	  former_middle=middle;
	  middle=(top-bottom)/2+bottom;
	}
      else if(kmers.at(middle).s<s)//AACSD<YYYAS
	{
	  bottom=middle;
	  former_middle=middle;
	  middle=(top-bottom)/2+bottom;
	}
      else if(kmers.at(middle).s==s)
	{
	  found=true;	  
	  return middle;
	}
    }
  return -1;
}

int proteome_v2::get_size()
{
  return proteins.size();
}

string proteome_v2::get_protein_s(int i)
{
  return proteins.at(i).get_sequence();
}

string proteome_v2::get_protein_il(int i)
{
  return proteins.at(i).get_info_line();
}

int proteome_v2::get_ordinal()
{
  return id;
}

void proteome_v2::write_genes_with_duplicates()
{
  ofstream outstream;
  outstream.open( (path+"/sos_info.txt").c_str(), ios::out | ios::app);
  if(outstream.fail())
    {
      cout<<"Failed to open outstrema in write_genes_with_duplicates."<<endl;
      exit(1);
    }

  int count=0;
  set<int> ids;
  for(int i=0; i<kmers.size()-1; i++)
    {
      if(kmers.at(i).s==kmers.at(i+1).s)
	{
	  ids.insert(kmers.at(i).source_protein_index);
	}
    }
  cout<<"ids size = "<<ids.size()<<" out of "<<proteins.size()<<endl;
  for(set<int>::iterator it=ids.begin(); it!=ids.end(); it++)
    {
      cout<<proteins.at(*it).get_info_line()<<endl;
    }

  for(set<int>::reverse_iterator it=ids.rbegin(); it!=ids.rend(); ++it)
    {
      proteins.erase(proteins.begin()+*it);
      cout<<"erasing *it="<<*it<<endl;
    }

  outstream<<ids.size()<<" erased. "<<proteins.size()<<" kept."<<endl;
}

void proteome_v2::write_out_new_proteome_v2(string path)
{
  ofstream outstream;
  outstream.open( (path+"/FAA_new/"+directory+"/"+files.at(0)).c_str());
  if(outstream.fail())
    {
      cout<<"Failed to open outstream in write_out_new_proteome_v2()"<<endl;
      exit(1);
    }
  for(int i=0; i<proteins.size(); i++)
    {
      outstream<<proteins.at(i).get_info_line()<<endl<<proteins.at(i).get_sequence()<<endl;
    }
  outstream.close();
}
*/

  //e.g. Genes hit by a cluster with 40% total coverage in the input proteome set will only have been counted in the bin corresponding to 40, but by definition 10%, 20%, and 30% were also hit; update those smaller bins with the larger numbers.
void proteome_v2::final_pvv_update()
{
  for(int i=0; i<pvv.size(); i++) //For all proteins.
    {
      for(int j=0; j<filtering_steps; j++) //For all bins
	{
	  for(int k=j+1; k<=filtering_steps; k++)
	    {
	      pvv.at(i).gnums.at(j)+=pvv.at(i).gnums.at(k);
	      pvv.at(i).gis.at(j)+=pvv.at(i).gis.at(k);
	    }
	}
    }
}

//////////////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////////////
void proteome_v2::write_filt_set(ofstream& outstream, int filt_option, int ordinal, string path, vector<string> files)
{
  cout<<"In proteome_v2::write_filt_set(ofstream&, int, int)"<<endl;

  stringstream ss;
  ss<<filt_option;
  string col = ss.str();

  //1. Populate columns of pvv with the final numbers.
  final_pvv_update();

  //Write out 
  //Write out the relevant IDs
  //Write out proteome size, before and after filtering.
  int before=0;
  int after=0;
  ofstream bastream;
  string bafile = path+"/proteome_sizes.txt";
  u.open_ofile_app(bastream,bafile,"bafile");

  //Write out conserved files.
  ofstream pvstream,pvvstream;
  string pvpath = path+"/pv.txt";
  u.open_ofile_app(pvstream,pvpath,"pvpath");
  string pvvpath = path+"/pvvpath.txt";
  u.open_ofile_app(pvvstream,pvvpath,"pvvpath");

  //Because the code doesn't store the proteomes in memory, it is necessary to re-read each proteome in to match the values in pv and pvv with the right protein.
  int pindex=0;
  for(int i=0; i<files.size(); i++)
    {
      ifstream instream;
      string fipath=files.at(i);
      instream.open(fipath.c_str());
      if(instream.fail())
	{
	  cout<<"Failed to open fipath."<<endl;
	  exit(1);
	}
      else
	{
	  cout<<"Successfully opened fipath to "<<fipath<<endl;
	}

      string eater;
      instream>>eater;
      while(!instream.eof())
	{
	  if(eater[0]=='>')
	    {
	      before++;
	      char buffer[2000];
	      instream.getline(buffer,2000);
	      string info_line=eater+buffer;
	      instream>>eater;
	      string sp;
	      while(!instream.eof() && eater[0]!='>')
		{
		  sp+=eater;
		  instream>>eater;
		}

	      //we now have an info line and a protein, and pindex tells us which protein we're on in pv.
	      pvstream<<ordinal<<" "<<pindex<<" "<<pv.at(pindex).gnums<<" "<<pv.at(pindex).gis<<" "<<get_ratio(pindex)<<" "<<pv.at(pindex).keeper<<" "<<info_line<<endl;
	      pvvstream<<pvv.at(pindex).keeper<<" "<<info_line<<endl;

	      for(int pvindex=0; pvindex<=filtering_steps; pvindex++)
		{
		  pvvstream<<pvv.at(pindex).gnums[pvindex]<<" ";
		}
	      pvvstream<<endl;

	      for(int pvindex=0; pvindex<=filtering_steps; pvindex++)
		{
		  pvvstream<<pvv.at(pindex).gis[pvindex]<<" ";
		}
	      pvvstream<<endl;

	      //if(pv.at(pindex).gnums>0 && get_ratio(pindex)<CONSERVATION_CUTOFF && pv.at(pindex).gis>0 && !pv.at(pindex).deleter)
	      if(pvv.at(pindex).gnums.at(filt_option)>0 && get_ratio(pindex,filt_option)<CONSERVATION_CUTOFF && pvv.at(pindex).gis.at(filt_option)>0 && !pvv.at(pindex).deleter)
		{
		  after++;
		  outstream<<ordinal<<" "<<pindex<<endl;
		  //outstream<<info_line<<endl<<sp<<endl;
		}
	      else if(pvv.at(pindex).keeper)
		{
		  outstream<<ordinal<<" "<<pindex<<endl;
		  //outstream<<info_line<<endl<<sp<<endl;
		  after++;
		}
	      else //it's a mobile element
		{
		  //outstreamJ<<info_line<<endl<<sp<<endl;
		}
	      pindex++;
	    }
	}
      instream.close();
    }

  bastream<<filtering_steps<<" "<<filt_option<<" "<<ordinal<<" "<<before<<" "<<after<<endl;
  bastream.close();

 
  pvstream.close();
  pvvstream.close();
}

//index = designates the index of the pvv gnums and gis vectors to use.
//The use of mscv is really just to skip over proteins that didn't have any hits. Otherwise we need to check every single one of them, even if they're just populated with 0's.
void proteome_v2::write_out_new_proteome_v2(string path,string directory, vector<string> files,int ordinal,vector<pair<int,int> > mscv, int index)
{
  stringstream ss;
  ss<<index;
  string col = ss.str();

  cout<<"index = "<<index<<endl;
  for(int i=0; i<pvv.size(); i++)
    {
      for(int j=0; j<filtering_steps; j++)
	{
	  for(int k=j+1; k<=filtering_steps; k++)
	    {
	      pvv.at(i).gnums.at(j)+=pvv.at(i).gnums.at(k);
	      pvv.at(i).gis.at(j)+=pvv.at(i).gis.at(k);
	    }
	}
    }

  //quicksort(proteins,0,proteins.size()-1);
  ofstream bastream;
  bastream.open( (path+"/proteome_sizes.txt").c_str(), ios::out|ios::app);
  if(bastream.fail())
    {
      cout<<"Failed to open bastream from write_out_new_proteome_v2"<<endl;
    }

  int before=0;
  int after=0;

  cout<<"Size of mscv in write_out_new_proteome_v2 = "<<mscv.size()<<endl;
  if(mscv.size()>0)
    {
      for(int i=0; i<mscv.size(); i++)
	{
	  cout<<mscv.at(i).first<<" "<<mscv.at(i).second<<endl;
	}
    }

  if(PROTEOME_DEBUG)
    {
      for(int i=0; i<proteins.size(); i++)
	{
	  proteins.at(i).print_protein_info();
	}
    }

  //fix this to include the refs
  if(!ref)
    {
      string outdirectory = path+"/FAA_csv_col"+col+"/"+directory;
      int system_return = system ( ("rm -r "+outdirectory).c_str());
      system_return = system ( ("mkdir "+outdirectory).c_str());
      string outdirectory2 = path+"/FAA_mob_col"+col+"/"+directory;
      int system_return2 = system ( ("rm -r "+outdirectory2).c_str());
      system_return2 = system ( ("mkdir "+outdirectory2).c_str());
    }

  else
    {
      string outdirectory = path+"/FAA_ref_csv_col"+col+"/"+directory;
      int system_return = system ( ("rm -r "+outdirectory).c_str());
      system_return = system ( ("mkdir "+outdirectory).c_str());
      string outdirectory2 = path+"/FAA_ref_mob_col"+col+"/"+directory;
      int system_return2 = system ( ("rm -r "+outdirectory2).c_str());
      system_return2 = system ( ("mkdir "+outdirectory2).c_str());
    }

  string outpath=files.at(0); //Contains the entire path, including the file at the end.
  int cb=0;
  string file_name;
  string path_name;
  for(int i=outpath.length()-1; i>=0; i--)
    {
      if(outpath[i]=='/')
	{
	  cb++;
	}
      if(cb<2)
	{
	  file_name=outpath[i]+file_name;
	}
      else
	{
	  path_name=outpath[i]+path_name;
	}
    }

  cout<<"file_name = "<<file_name<<endl;
  cout<<"path_name = "<<path_name<<endl;

  path_name = path_name.substr(0,path_name.length()-1);
  string path_name_j = path_name;
  path_name = path_name+"_csv_col"+col;
  path_name_j = path_name_j+"_mob_col"+col;

  path_name_j = path_name_j+"/"+file_name;
  path_name = path_name+"/"+file_name;
  
  cout<<"path_name = "<<path_name<<endl;
  cout<<"path_name_j = "<<path_name_j<<endl;

  ofstream outstream;
  ofstream outstreamJ;
  //string outpath=path+"/FAA_new/"+directory+"/"+files.at(0);

  /*
  int q=0;
  for(int i=0; i<files.at(0).length(); i++)
    {
      if(files.at(0).substr(i,3)=="FAA")
	{
	  q = i;
	}
    }

  string outpathJ=outpath;
  outpath.insert(q+3,"_new");
  outpathJ.insert(q+3,"_junk");

  //  string outpath=files.at(0);
  //  int q=0;
  for(int i=0; i<files.at(0).length(); i++)
    {
      if(files.at(0).substr(i,3)=="_new")
	{
	  q = i;
	}
    }

  outpath=outpath+"_col"+col;
  outpathJ=outpathJ+"_col"+col;

  cout<<"outpath = "<<outpath<<endl;
  exit(1);
  */

  //This is the one that matters. **
  //  outstream.open(outpath.c_str());
  outstream.open(path_name.c_str());
  while(outstream.fail())
    {
      char c;
      cout<<"Failed to open outstream to "<<path_name<<endl<<"Do the directories in FAA_csv/FAA_ref_csv exist? If not, put them there now and then confirm. (y): ";
      cin>>c;
      outstream.open(path_name.c_str());
    }

  //  outstreamJ.open(outpathJ.c_str());
  outstreamJ.open(path_name_j.c_str());
  while(outstreamJ.fail())
    {
      char c;
      cout<<"Failed to open outstream to "<<path_name_j<<endl<<"Do the directories in FAA_mob/FAA_ref_mob exist? If not, put them there now and then confirm. (y): ";
      cin>>c;
      outstreamJ.open(path_name_j.c_str());
    }

  /*  string outfile = path+"/FAA_new/"+directory+"/"+files.at(0);
  cout<<"outfile = "<<outfile<<endl;
  outstream.open(outfile.c_str());
  if(outstream.fail())
    {
      cout<<"Failed to open outstream in write_out_new_proteome_v2()"<<endl;
      exit(1);
    }
  */

  ofstream outstream2;
  string cpath = path+"/conserved.txt";
  outstream2.open( (cpath).c_str(), ios::out | ios::app);
  if(outstream2.fail())
    {
      cout<<"Failed to open outstream2 in write_out_new_proteome_v2()"<<endl;
      exit(1);
    }

  ofstream outstream2_v2;
  string cpath_v2 = path+"/conserved_v2.txt";
  outstream2_v2.open( (cpath_v2).c_str(), ios::out | ios::app);
  if(outstream2_v2.fail())
    {
      cout<<"Failed to open outstream2_v2 in write_out_new_proteome_v2()"<<endl;
      exit(1);
    }

  ofstream igstream;
  string igpath=path+"/interesting_genes_msc.txt";
  igstream.open(igpath.c_str(), ios::out | ios::app);
  if(igstream.fail())
    {
      cout<<"Failed to open igstream to "<<igpath<<endl;
      exit(1);
    }

  ofstream igstream2;
  string igpath2=path+"/interesting_genes.txt";
  igstream2.open(igpath2.c_str(), ios::out | ios::app);
  if(igstream2.fail())
    {
      cout<<"Failed to open igstream2 to "<<igpath2<<endl;
      exit(1);
    }
  

  //Because the code doesn't store the proteomes in memory, it is necessary to re-read each proteome in to match the values in pv and pvv with the right protein.
  int pindex=0;
  int mscindex=0;
  for(int i=0; i<files.size(); i++)
    {
      ifstream instream;
      //string fipath=path+"/FAA/"+directory+"/"+files.at(i);
      string fipath=files.at(i);
      instream.open(fipath.c_str());
      if(instream.fail())
	{
	  cout<<"Failed to open fipath."<<endl;
	  exit(1);
	}
      else
	{
	  cout<<"Successfully opened fipath to "<<fipath<<endl;
	}

      string eater;
      instream>>eater;
      while(!instream.eof())
	{
	  if(eater[0]=='>')
	    {
	      before++;
	      char buffer[2000];
	      instream.getline(buffer,2000);
	      string info_line=eater+buffer;
	      instream>>eater;
	      string sp;
	      while(!instream.eof() && eater[0]!='>')
		{
		  sp+=eater;
		  instream>>eater;
		}

	      //we now have an info line and a protein, and pindex tells us which protein we're on in pv.
	      if(mscv.size()>0)
		{
		  while(mscindex<mscv.size() && mscv.at(mscindex).first<pindex)
		    {
		      mscindex++;
		    }
		}

	      double msc_score=-1;
	      if(mscv.size()>0 && mscindex<mscv.size() && mscv.at(mscindex).first==pindex)
		{
		  cout<<"first = "<<mscv.at(mscindex).first<<endl<<"second = "<<mscv.at(mscindex).second<<"gnums = "<<pv.at(pindex).gnums<<endl<<"get_ratio = "<<get_ratio(pindex)-0.9<<endl;
		  if(pv.at(pindex).gnums==0)
		    {
		      cout<<"den == 0"<<endl;
		      cout<<"info_line = "<<info_line<<endl;
		      cout<<"protein = "<<sp<<endl;
		      msc_score=-1;
		    }
		  else
		    {
		      msc_score = ((double)mscv.at(mscindex).second/(double)pv.at(pindex).gnums)*(get_ratio(pindex)-.9);
		    }
		
		  igstream<<msc_score<<endl;
		  igstream2<<msc_score<<" "<<mscv.at(mscindex).second<<" "<<pv.at(pindex).gnums<<" "<<get_ratio(pindex)<<" "<<info_line<<" "<<sp<<endl;
		  cout<<"msc_score = "<<msc_score<<endl;
		}

	      outstream2<<ordinal<<" "<<pindex<<" "<<pv.at(pindex).gnums<<" "<<pv.at(pindex).gis<<" "<<get_ratio(pindex)<<" "<<pv.at(pindex).keeper<<" "<<info_line<<endl;
	      outstream2_v2<<pvv.at(pindex).keeper<<" "<<info_line<<endl;

	      //	      for(int pvindex=0; pvindex<11; pvindex++)
	      for(int pvindex=0; pvindex<=filtering_steps; pvindex++)
		{
		  outstream2_v2<<pvv.at(pindex).gnums[pvindex]<<" ";
		}
	      outstream2_v2<<endl;

	      for(int pvindex=0; pvindex<=filtering_steps; pvindex++)
		{
		  outstream2_v2<<pvv.at(pindex).gis[pvindex]<<" ";
		}
	      outstream2_v2<<endl;

	      //if(pv.at(pindex).gnums>0 && get_ratio(pindex)<CONSERVATION_CUTOFF && pv.at(pindex).gis>0 && !pv.at(pindex).deleter)
	      if(pvv.at(pindex).gnums[index]>0 && get_ratio(pindex,index)<CONSERVATION_CUTOFF && pvv.at(pindex).gis[index]>0 && !pvv.at(pindex).deleter)
		{
		  after++;
		  outstream<<info_line<<endl<<sp<<endl;
		}
	      else if(pvv.at(pindex).keeper)
		{
		  outstream<<info_line<<endl<<sp<<endl;
		  after++;
		}
	      else //it's a mobile element
		{
		  outstreamJ<<info_line<<endl<<sp<<endl;
		}
	      pindex++;
	    }
	}
      instream.close();
    }

  bastream<<before<<" "<<after<<endl;
  bastream.close();

  outstream.close();
  outstream2.close();
  outstream2_v2.close();
  outstreamJ.close();
  igstream.close();
}

void proteome_v2::increment_ggs(int i, int gnum_i, int gene_id_i)
{
  if(i>=0 && i<pv.size())
    {
      pv.at(i).gnums+=gnum_i;
      pv.at(i).gis+=gene_id_i;
    }
  else
    {
      cout<<"index issue in increment_ggs in proteome_v2."<<endl;
      exit(1);
    }
}

/*
0<=d<=1.0
Can d = 0?
 */
int proteome_v2::get_pvv_index(double d)
{
  int index = filtering_steps*d;
}

//d = the fraction (e.g. .35) of the number of reference genomes (or total genomes, if there is no reference) that had a hit for the source cluster. If a reference set of 30, and d = .5, then 15 were represented at least once.
void proteome_v2::increment_ggs(int i, int gnum_i, int gene_id_i,double d)
{
  if(i>=0 && i<pvv.size())
    {
      //      d*=10.0; //Put it in the range of integers, for indexing purposes. So .35 --> 3.5 --> index of 3.
      d*=(double)filtering_steps; 
      //      if(d>=0 && d<=10)
      if(d>=0 && d<=filtering_steps)
	{
	  pvv.at(i).gnums[(int)d]+=gnum_i; //gnum_i [1,refsetsize or input set size if there is no refset]
	  pvv.at(i).gis[(int)d]+=gene_id_i; //gene_id_i [1,arbitrarily large]
	}
      //      else if(d>10.0)
      else if(d>filtering_steps)
	{
	  cout<<"proteome_v2::increment_ggs(int,int,int,double)"<<endl;
	  cout<<"d>filtering_steps : d="<<d<<" filtering_steps="<<filtering_steps<<endl;
	  cout<<"i = "<<i<<endl;
	  cout<<"gnum_i = "<<gnum_i<<endl;
	  cout<<"gene_id_i = "<<gene_id_i<<endl;
	  cout<<"d = "<<d<<endl;
	  exit(1);
	}
    }
  else
    {
      cout<<"proteome_v2::increment_ggs(int,int,int,double)."<<endl;
      cout<<"index=i out of bounds for pvv vector"<<endl;
      cout<<"i = "<<i<<endl;
      cout<<"gnum_i = "<<gnum_i<<endl;
      cout<<"gene_id_i = "<<gene_id_i<<endl;
      cout<<"d = "<<d<<endl;
      cout<<"pvv.size() = "<<pvv.size()<<endl;
      exit(1);
    }
}

  
void proteome_v2::mark_gene_for_deletion(int index)
{
  if(index>=0 && index<pv.size())
    {
      pv.at(index).deleter=true;
    }
  if(index>=0 && index<pvv.size())
    {
      pvv.at(index).deleter=true;
    }
}

void proteome_v2::mark_gene_internal_repeats(int index)
{
  if(index>=0 && index<pv.size())
    {
      pv.at(index).internal_repeats=true;
    }
  if(index>=0 && index<pvv.size())
    {
      pvv.at(index).internal_repeats=true;
    }
}

void proteome_v2::write_out_gene(int index)
{
  if(index>=0 && index<proteins.size())
    {
      proteins.at(index).print_protein_info();
    }
}



/*string proteome_v2::scramble(string s)
{
  int s_original_length = s.length();
  string result;
  while(s.length()>0)
    {
      int index = rand()%s.length();
      if(index>=0 && index<s.length())
	{
	  result = result+s[index];
	  s = s.erase(index,1);
	}
      else
	{
	  cout<<"index out of bounds in info_file::scramble(...)"<<endl;
	}
    }

  if(s_original_length!=result.length())
    {
      cout<<"Length mismatch in info_file::scramble(string)"<<endl;
      exit(1);
    }

  return result;
}
*/

bool proteome_v2::is_valid(string s)
{
1616 1617 1618 1619
    std::size_t found = s.find_first_not_of(aas.valid_aas);

    if(found != std::string::npos ){
        return false;
David Trudgian's avatar
David Trudgian committed
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    }

  if(SUMOFSQUARES)
    {
      bool result = check_composition(s);
      return(true & result);
    }
  return true;
}

bool proteome_v2::check_composition(string s)
{
  vector<int> array;

1634
  for(int i=0; i<20; i++) 
David Trudgian's avatar
David Trudgian committed
1635
    {
1636 1637 1638
      char aa = aas.get_aa(i);
      int aa_count = std::count(s.begin(),s.end(),aa);
      array.push_back( aa_count );
David Trudgian's avatar
David Trudgian committed
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
    }

  int result=0;
  //  for(int i=0; i<s.length(); i++)
  for(int i=0; i<array.size(); i++)
    {
      result+=array[i]*array[i];
    }

  if(result>=0 && result<sosv.size())
    {
      sosv.at(result)++;
      if(result>sos_cutoff)
	{
	  // cout<<s<<" "<<result<<endl;
	}
    }
  else
    {
      cout<<"proteome_v2::check_composition(string)"<<endl;
      cout<<"sosv.size() = "<<sosv.size()<<endl;
      cout<<"Index out of range. s = "<<s<<" result = "<<result<<endl;
      exit(1);
    }

  if(result>sos_cutoff)
    {
      return false;
    }
  
  return true;
}

/*
void proteome_v2::quicksort_v2(vector<protein>& pv, int left, int right)
{
  int i=left;
  int j=right;
  
  int pivot=pv.at((left+right)/2).get_gnums_count();

  while(i<=j)
    {
      while(pivot>pv.at(i).get_gnums_count() )
	{
	  i++;
	}
      while(pivot<pv.at(j).get_gnums_count() )
	{
	  j--;
	}
      if(i<=j)
	{
	  protein temp = pv.at(i);
	  pv.at(i) = pv.at(j);
	  pv.at(j) = temp;
	  i++;
	  j--;
	}
    };

  if(left<j)
    quicksort_v2(pv,left,j);
  if(i<right)
    quicksort_v2(pv,i,right);      
}

void proteome_v2::reset_deleters()
{
  for(int i=0; i<proteins.size(); i++)
    {
      proteins.at(i).reset_deleters();
    }
}

*/

void proteome_v2::print_protein_info(int i)
{
  /*  if(i>=0 && i<proteins.size())
    {
      proteins.at(i).print_protein_info();
      }*/

  if(i>=0 && i<pv.size())
    {
      cout<<pv.at(i).gnums<<" "<<pv.at(i).gis<<" "<<(double)pv.at(i).gis/(double)pv.at(i).gnums<<endl;
    }
  else
    {
      cout<<"i="<<i<<" Invalid index for proteins.size()="<<proteins.size()<<endl;
    }

}

void proteome_v2::clear_proteins()
{
  proteins.clear();
}

double proteome_v2::get_ratio(int pindex)
{
  if(pv.at(pindex).gnums==0)
    {
      return 0.0;
    }
  else
    {
      double result=(double)pv.at(pindex).gis/(double)pv.at(pindex).gnums;
      return result;
    }
  
  cout<<"How did we get here? Bottom of proteome_v2::get_ratio."<<endl;
  exit(1);
}

double proteome_v2::get_ratio(int pindex, int index2)
{
  if(pvv.at(pindex).gnums.at(index2)==0)
    {
      return 0.0;
    }
  else
    {
      double result=(double)pvv.at(pindex).gis.at(index2)/(double)pvv.at(pindex).gnums.at(index2);
      return result;
    }
  
  cout<<"How did we get here? Bottom of proteome_v2::get_ratio."<<endl;
  exit(1);
}

void proteome_v2::set_total_num_proteins()
{
  total_num_proteins=proteins.size();
}

void proteome_v2::set_tag_length(int tag_length_par)
{
  tag_length=tag_length_par;
  int sosv_size = tag_length*tag_length+1;
  for(int i=0; i<sosv_size; i++)
    {
      sosv.push_back(0);
    }
  sos_cutoff=6.5*tag_length;
}

void proteome_v2::add_discr(int instances, double avg_rep)
{
  double diff = abs(instances-avg_rep);
  discrepancy+=diff;
}

double proteome_v2::get_discr()
{
  return discrepancy;
}

void proteome_v2::count_conserved()
{
  conserved++;
}

int proteome_v2::get_conserved()
{
  return conserved;
}

/*

void proteome_v2::mark_keepers(string s,ofstream& outstream)
{
  for(int i=0; i<proteins.size(); i++)
    {
      for(int j=0; j<proteins.at(i).get_sequence().length(); j++)
	{
	  string f = proteins.at(i).get_sequence().substr(j,s.length());
	  int matches=0;
	  for(int k=0; k<f.length(); k++)
	    {
	      if(s[k]==f[k])
		{
		  matches++;
		}
	    }
	  
	  if(matches>(.6*(double)s.length()))
	    {
	      proteins.at(i).print_protein_info();
	      proteins.at(i).minclude();
	      outstream<<proteins.at(i).get_info_line()<<endl;
	    }
	}
    }
}
*/

void proteome_v2::count_filtered_proteomes(vector<int>& num_proteins)
{
  //For all proteins in the current proteome.
  for(int i=0; i<pvv.size(); i++)
    {
      //For the 10 possible cutoffs.
      //for(int j=0; j<11; j++) //[0,10]
      for(int j=0; j<num_proteins.size(); j++) //[0,10]
	{
	  double ratio;
	  double gnums_sum=0.0;
	  double gis_sum=0.0;

	  //For the 10 possible cutoffs (indexing gnums)
	  //for(int k=j; k<11; k++)
	  for(int k=j; k<num_proteins.size(); k++)
	    {
	      gnums_sum+=pvv.at(i).gnums.at(k);
	      gis_sum+=pvv.at(i).gis.at(k);
	    }

	  if(gnums_sum==0)
	    {
	      ratio = 0.0;
	    }
	  else
	    {
	      ratio=(double)gis_sum/(double)gnums_sum;
	    }

	  if(gnums_sum>0 && ratio<CONSERVATION_CUTOFF && gis_sum>0 && !pvv.at(i).deleter)      
	    {
	      num_proteins.at(j)++;
	    }
	}
    }
}

bool proteome_v2::keeper_present(string s1,string s2)
{
  int count_mismatches=0;
  for(int i=0; i<s1.length(); i++)
    {
      if(s1[i]!=s2[i])
	{
	  count_mismatches++;
	}
    }

  double val = (double)count_mismatches/(double)s1.length();

  if(val>KEEPERS_CUTOFF)
    {
      return false;
    }
  else
    {
      return true;
    }
}

void proteome_v2::wokl(int id, ofstream& outstream)
{
  for(int i=0; i<pvv.size(); i++)
    {
      outstream<<id<<" "<<i<<" "<<pvv.at(i).keeper<<endl;
    }
  outstream<<endl;
}

void proteome_v2::write_info_v1(ofstream& outstream)
{
  outstream<<"proteome data:"<<endl;
  outstream<<"size_real_tags "<<size_real_tags<<endl;
  outstream<<"disrepancy "<<discrepancy<<endl;
  outstream<<"conserved "<<conserved<<endl;
  outstream<<"chosen_column "<<chosen_column<<endl;
  outstream<<"ref "<<ref<<endl;
  outstream<<"proteins.size() "<<proteins.size()<<endl;
  outstream<<"total_num_proteins "<<total_num_proteins<<endl;

  for(int i=0; i<proteins.size(); i++)
    {
      outstream<<i<<" "<<" "<<proteins.at(i).get_id()<<" "<<proteins.at(i).get_gnums()<<" "<<proteins.at(i).get_geneids()<<" "<<proteins.at(i).get_deleter()<<" "<<proteins.at(i).get_mi()<<endl;
    }

  outstream<<"pv.size() "<<pv.size()<<endl;
  for(int i=0; i<pv.size(); i++)
    {
      //      outstream<<pv.at(i).gnums<<" "<<pv.at(i).gis<<" "<<pv.at(i).keeper<<" "<<pv.at(i).deleter<<" "<<pv.at(i).internal_repeats<<endl;
      outstream<<i<<" "<<pv.at(i).keeper<<endl;
    }

  outstream<<"filtering_steps "<<filtering_steps<<endl;
  outstream<<"pvv.size() "<<pvv.size()<<endl;
  for(int i=0; i<pvv.size(); i++)
    {
      outstream<<i<<" "<<pvv.at(i).keeper<<endl;//" "<<pvv.at(i).deleter<<" "<<pvv.at(i).internal_repeats<<endl;
      /*      for(int j=0; j<pvv.at(i).gnums.size(); j++)
	{
	  outstream<<j<<" "<<pvv.at(i).gnums.at(j)<<endl;
	}
      for(int j=0; j<pvv.at(i).gis.size(); j++)
	{
	  outstream<<j<<" "<<pvv.at(i).gis.at(j)<<endl;
	  }*/
    }
  
  outstream<<"sosv.size() "<<sosv.size()<<endl;
  for(int i=0; i<sosv.size(); i++)
    {
      outstream<<i<<" "<<sosv.at(i)<<endl;
    }
  outstream<<endl;
}

void proteome_v2::set_size_real_tags(int i)
{
  size_real_tags=i;
}

void proteome_v2::set_chosen_column(int i)
{
  chosen_column=i;
}

int proteome_v2::get_total_num_proteins()
{
  return proteins.size();
}

#endif