mctr.h 78.9 KB
Newer Older
David Trudgian's avatar
David Trudgian committed
1
/******************************************************************************
2
3
Takes sorted lists of kmers and counts matches from lengths 1-k.
Writes the histograms for each pair to DATA/
David Trudgian's avatar
David Trudgian committed
4
5
6
7
8
9
 *****************************************************************************/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
10
#include <algorithm>
David Trudgian's avatar
David Trudgian committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <map>
#include <sstream>
#include <math.h>
#include <set>
#include <dirent.h>
#include <sys/types.h>
#include <omp.h>

#include "Coptim1_3Dmat.h"
#include "bitter.h"
#include "amino_acids.h"
#include "util.h"

using namespace std;

26
const int TAGLENGTH=20;
David Trudgian's avatar
David Trudgian committed
27
28
29
30
31
32
33
34
const bool MCTR_DEBUG=false;
const bool GENEIDRUN=true;
const int CON_MAP_CUTOFF=3; //"cmc" variable.
                            //For a protein with a hit between an HGT-pair to be spared deletion (due to possible HGT), it has to have at least cmc hits among the reference set.  Therefore, a cmc=15 means at least 15 reference orgs have to have hits for the protein to be considered "conserved" (not delete-able). NOTE: Actually this is wrong! This is not what cmc is! It's the number of hits for a reference set organism, but over all the kmers. So this value can be very high.
const int CON_MAP_SET_CUTOFF=3;

const int HGT_MIN_LENGTH=12;
const int MBKL=1.0; //Default=1.0. If tag_length=20, and MBKL=.75, then private variable mbkl will be set to .75*20=15. 15 will then be the minimum length of a match for its score to be passed to the bitter object, which uses these scores to select a right-hand cutoff for the data in the mdist function.
35
36
const bool AM=false;
const bool AAM=false;
David Trudgian's avatar
David Trudgian committed
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

struct bits_holder
{
  double d[20];
};

struct krow
{
  vector<bool> r;
};

class mctr
{
 private:
  //Variables necessary for all 3 types of run:
  string bfn;                 //base file name: directory holding all the data (e.g. 4ecoli_run1)
  string path;                //full path until base file name
  int tag_length;             //default=20
  int refsetsize;             //0 means no reference genomes.
  string dir;                 //either "" empty string or "scr"
  int filtering_steps;        //=-1 when no filtering used (e.g. using MERGED_TAGS rather than MERGED_TAGS_15_8)
  int filtering_option;       //=-1 when no filtering was used (same as above)
  string merged_directory;    
  string data_directory;
  int sos_cutoff;              //default = 6.5*tag_length
  double max_single_bit_value;  
  int max_bit_sum;             //e.g. tag_length=20, highest bit store=4, 4*20 = 80
64
65
66
  int mbkl;                    //minimum kmer length for bitter to store it, default=tag_length
  vector<string> files;        
  int number_of_genomes;       
David Trudgian's avatar
David Trudgian committed
67
68
69
70
  int number_of_genomes_ref;
  int offset;
  int recursion_counter;
  ofstream logstream;
71
72
73
74
75

  char array_c[][TAGLENGTH]; //20 is the tag length.
  int *gnums_arr;
  int *gene_ids_arr;
  
David Trudgian's avatar
David Trudgian committed
76
77
78
79
80
81
82
83
84
  vector<string> array;
  vector<int> gnums;
  vector<int> gene_ids;
  Coptim1_3Dmat* corr_array;
  vector<bitter> bitterv;
  vector<bits_holder> bitsv;
  amino_acids aas;
  util u;
  int run_type; //0=normal run, 1=ref_set_run, 2=final pair run
Raquel Bromberg's avatar
Raquel Bromberg committed
85
86
  bool partial_run;
  char partial_run_lead;
87
88
  int get_aa_int[30]; 
  char get_aa_char[20];
David Trudgian's avatar
David Trudgian committed
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

  //Variables necessary for HGT correction on subsequent passes through mctr.
  int prob1;                             //ordinal of one of the 2 problematic proteomes in the whole run.
  int prob2;                             //ordinal of one of the 2 problematic proteomes in the whole run.
  vector<krow> rows; //provides transitive information on HGT matches; if the pair shares a long length match, but one member of the pair has matches with the reference set, this applies to the other, so neither protein will be deleted.
  int rows_cols;
  int rows_rows;

  bool pair_run; //true if run is for just a pair with HGT, no reference set (3rd iteration)
  bool msc; //match_score_cutoff; true if the file "match_score_cutoffs.txt" exists, false otherwise.
  bool msc2;
  int genes_found;
  double min_msc;                        //the minimum msc among all the org pairs.
  map<std::pair<int,int>,int> msc_map;   //delete this when subsequent maps are implemented.
  map<std::pair<int,int>,int> con_map;
  vector< set<int> > con_map_set0;
  vector< set<int> > con_map_set1;
  map<std::pair<int,int>,int> hgt_map;
  vector<int> msc_ref_gnums;
  bool distances;
  vector<string> problem_tags;           //vector for the tags from the 2 proteomes that have the HGT problem. merged into the refset later.
  vector<string> ref_tags;
  vector<int> problem_tags_gnums;
  vector<int> problem_tags_gis;
  bool check_composition(string s);
  void get_problem_tags(vector<string> files,string ordinal);
  void get_problem_tags(vector<string> files,string ordinal, set<int> hgt_genes);
  int ppv_index;                         //what line of the problem pair index vector are we on.
  int ref_index;                         //what line of the reference vector are we on.
  vector<std::pair<int, int> > keys;
  vector<string> info_lines0;
  vector<string> info_lines1;
  vector<string> proteins0;
  vector<string> proteins1;
  void setup_it1(ifstream& infostream);
  void setup_it2(ifstream& infostream);

  void read_in_probfiles(ifstream& infostream,vector<string>& files1, vector<string>& files2);

  int calculate_max_bit_sum();
  void open_info_file(ifstream& infostream);
  void create_directories();
131
  char get_aa_char_fn(int i);
David Trudgian's avatar
David Trudgian committed
132
  void set_bitsv();
133
  void set_get_aa_arrays();
David Trudgian's avatar
David Trudgian committed
134
135
136
137
138

  void set_array_it0(int i);
  void set_array_it1(int i);
  void set_array_it2(int i);

139
140
141
142
143
144
145
146
147
  int set_gh(bool gh[], vector<int>& gnums, int first_row, int last_row);
  void set_score_array(double * score_array,string seq,int count,int gen_ord);

  void find_matches_it0(int col, int first_row, int last_row, string seq);
  void find_matches_it1(int col, int first_row, int last_row, string seq);
  void find_matches_it2(int col, int first_row, int last_row, string seq);
  void increment_bits_it0(int first_row, int last_row,string seq);
  void increment_bits_it1(int first_row, int last_row,string seq);
  void increment_bits_it2(int first_row, int last_row,string seq);
David Trudgian's avatar
David Trudgian committed
148
149
150
151
152
153
154
155
156
157
  int get_aa(char aa);
  void write_out_data_it0();
  void write_out_data_it1();
  void write_out_data_it2();
  
  void read_in_files();
  double get_score(int i1,int i2, string seq);
  void populate_bitterv();
  int get_bitterv_index(int i1, int i2);
  int get_sos_cutoff();
Raquel Bromberg's avatar
Raquel Bromberg committed
158
  char get_pr_lead();
David Trudgian's avatar
David Trudgian committed
159
160
161

 public: 
  //constructors
Raquel Bromberg's avatar
Raquel Bromberg committed
162
  mctr(string full_path_par,string dir_par, int filtering_steps_par, int filtering_option_par,bool partial_run_par); //Use for the main run when doing entire set.
David Trudgian's avatar
David Trudgian committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_options_par, int prob1_par, int prob2_par,vector<string>& ref_tags_par); //Use when testing a pair against a reference set.
  mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par, int prob1_par, int prob2_par); //Use when running two proteomes, HGT-genes removed, against each other. Final step.

  //run the main algorithm
  void run_it0();    //main run on whole set.
  int  run_it1();    //run pair against each other and the reference set.
  void run_it2();    //run pair against each other with phage/hgt proteins removed.

  void set_problem_pair(int prob1_par,int prob2_par);
};

//Constructor 1. Main run.
//Count all matches between all pairs present in a MERGED_TAGS directory (and also the corresponding MERGED_TAGSscr directory, but has to be called on the data separately using a different object). 
//This could be for a set without filtering (e.g. MERGED_TAGS plain) or with filering (e.g. MERGED_TAGS_15_8). 
//Without filtering: filtering_steps_par=-1, filtering_option_par=-1
Raquel Bromberg's avatar
Raquel Bromberg committed
178
mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par,bool partial_run_par)
David Trudgian's avatar
David Trudgian committed
179
180
181
{
  cout<<"mctr::mctr(string,string,int,int)"<<endl;
  run_type=0;
Raquel Bromberg's avatar
Raquel Bromberg committed
182
  partial_run=partial_run_par;
183
  set_get_aa_arrays();
David Trudgian's avatar
David Trudgian committed
184
185
186

  //main variables.
  u.extract_paths(full_path_par,path,bfn);
Raquel Bromberg's avatar
Raquel Bromberg committed
187
188
189
190
  if(partial_run)
    {
      partial_run_lead=get_pr_lead();
    }
David Trudgian's avatar
David Trudgian committed
191
192
193
194
195
  u.get_rss(path+"/"+bfn+"/"+bfn+"_info.txt",refsetsize);
  u.get_tl(path+"/"+bfn+"/"+bfn+"_info.txt",tag_length);
  mbkl=int(MBKL*(double)tag_length);
  filtering_steps=filtering_steps_par;
  filtering_option=filtering_option_par;
196
  dir=dir_par;            //dir="" when running on the main set; dir="scr" when running on scrambled set.
David Trudgian's avatar
David Trudgian committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  sos_cutoff=get_sos_cutoff(); 
  u.open_ofile_app(logstream,path+"/"+bfn+"/logfile_cm1.txt","logfile_cm1.txt");

  //Set all pair run variables to false.
  ppv_index=ref_index=0;

  string addon="";
  if(filtering_steps!=-1 && filtering_option!=-1)
    {
      addon="_"+u.int_to_string(filtering_steps)+"_"+u.int_to_string(filtering_option);
    }

  merged_directory = "MERGED_TAGS"+dir+addon;
  data_directory = "DATA"+dir+addon;
211

David Trudgian's avatar
David Trudgian committed
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
  ifstream infostream;
  open_info_file(infostream); //sets tag length and number of genomes.
  infostream.close();

  create_directories();
  set_bitsv();  //should have an option: conserved seq bit scores or all bit scores
  max_bit_sum = calculate_max_bit_sum();
  cout<<"max_bit_sum = "<<max_bit_sum<<endl;

  if(max_bit_sum>0 && number_of_genomes>0)
    {
      corr_array = new Coptim1_3Dmat(number_of_genomes,max_bit_sum);
    }
  else
    {
      cout<<"mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par)"<<endl;
      cout<<"max_bit_sum="<<max_bit_sum<<endl;
      cout<<"number_of_genomes="<<number_of_genomes<<endl;
      exit(1);
    }

  populate_bitterv();
  recursion_counter=0;
  read_in_files();
}

//Constructor 2. Pair run against reference.
mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par, int prob1_par, int prob2_par,vector<string>& ref_tags_par)
{
241
  set_get_aa_arrays();
David Trudgian's avatar
David Trudgian committed
242
243
244
245
246
247
248
249
250
251
252
253
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
  run_type=1;
  u.extract_paths(full_path_par,path,bfn);
  u.get_rss(path+"/"+bfn+"/"+bfn+"_info.txt",refsetsize);
  u.get_tl(path+"/"+bfn+"/"+bfn+"_info.txt",tag_length);
  mbkl=int(MBKL*(double)tag_length);

  filtering_steps=filtering_steps_par;
  filtering_option=filtering_option_par;
  dir=dir_par; //do we need this?
  ref_tags=ref_tags_par;
  
  prob1=prob1_par;
  prob2=prob2_par;
  
  //Set all pair run variables to false.
  ppv_index=0;
  ref_index=0;
  sos_cutoff=get_sos_cutoff();

  u.open_ofile_app(logstream,path+"/"+bfn+"/logfile_cm2.txt","logfile_cm2.txt");

  string addon="";
  if(filtering_steps!=-1 && filtering_option!=-1)
    {
      addon="_"+u.int_to_string(filtering_steps)+"_"+u.int_to_string(filtering_option);
    }
  
  merged_directory = "MERGED_TAGS"+dir+addon;
  data_directory = "DATA"+dir+addon;

  ifstream infostream;
  open_info_file(infostream); //sets tag length and number of genomes.
  int bound=number_of_genomes+number_of_genomes_ref; //Need to set the bound variable here, because these two variables will be changed later to reflect that it's a run only against the reference set.
  setup_it1(infostream);        //Reads in the tags (and sorts them) for the 2 problem proteomes. 
  infostream.close();

  set_bitsv();
  max_bit_sum = calculate_max_bit_sum();

  if(max_bit_sum>0)
    {
      int total=refsetsize;
      if(prob1>=refsetsize)
	{
	  total++;
	}
      if(prob2>=refsetsize)
	{
	  total++;
	}

      number_of_genomes_ref=total;
      if(prob1>=bound || prob2>=bound)
	{
	  cout<<"Invalid genome id: prob1="<<prob1<<" prob2="<<prob2<<endl;
	  cout<<"Check HGT_pairs.txt file for genome id out of bounds"<<endl;
	  exit(1);
	}      

      corr_array = new Coptim1_3Dmat(number_of_genomes_ref,max_bit_sum);
    }
  else
    {
      cout<<"Problem in mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par, int prob1_par, int prob2_par,vector<string>& ref_tags)"<<endl;
      cout<<"max_bit_sum="<<max_bit_sum<<endl;
      exit(1);
    }

  populate_bitterv();
  recursion_counter=0;
}

//Constructor 3. Pair run against just itself.
mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par, int prob1_par, int prob2_par)
{
317
318
  set_get_aa_arrays();

David Trudgian's avatar
David Trudgian committed
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
365
366
367
368
369
370
371
372
373
374
375
376
  if(MCTR_DEBUG)
    {
      cout<<"mctr::mctr(string full_path_par, string dir_par, int filtering_steps_par, int filtering_option_par, int prob1_par, int prob2_par)"<<endl;
      cout<<"Constructor 3 for HGT pair fix"<<endl;
    }

  run_type = 2;

  u.extract_paths(full_path_par,path,bfn);
  u.get_rss(path+"/"+bfn+"/"+bfn+"_info.txt",refsetsize);
  u.get_tl(path+"/"+bfn+"/"+bfn+"_info.txt",tag_length);
  mbkl=int(MBKL*(double)tag_length);
  filtering_steps=filtering_steps_par;
  filtering_option=filtering_option_par;
  dir=dir_par;

  //Set all pair run variables to false.
  prob1=prob1_par;
  prob2=prob2_par;
  ppv_index=0;
  ref_index=0;
  sos_cutoff=get_sos_cutoff();

  u.open_ofile_app(logstream,path+"/"+bfn+"/logfile_cm3.txt","logfile_cm3.txt");

  string addon="";
  if(filtering_steps!=-1 && filtering_option!=-1)
    {
      addon="_"+u.int_to_string(filtering_steps)+"_"+u.int_to_string(filtering_option);
    }
  
  merged_directory = "MERGED_TAGS"+dir+addon;
  data_directory = "DATA"+dir+addon;

  ifstream infostream;
  open_info_file(infostream); //sets tag length and number of genomes.
  setup_it2(infostream);
  infostream.close();

  set_bitsv();
  max_bit_sum = calculate_max_bit_sum();

  number_of_genomes_ref=0;
  number_of_genomes=2;
  corr_array = new Coptim1_3Dmat(2,max_bit_sum);

  populate_bitterv();

  recursion_counter=0;
}

void mctr::run_it0()
{
  if(MCTR_DEBUG)
    {
      cout<<"mctr::run_it0()"<<endl;
    }

Raquel Bromberg's avatar
Raquel Bromberg committed
377
378
379
380
381
382
383
384
385
  if(!partial_run)
    {
      for(int i=0; i<20; i++)
	{
	  array.clear();
	  gnums.clear();
	  gene_ids.clear();
	  set_array_it0(i);
	  cout<<"First 3 rows of array: "<<endl;
386
387
388
389
390
391
	  //cout<<array.at(0)<<" "<<gnums.at(0)<<" "<<gene_ids.at(0)<<endl;
	  //cout<<array.at(1)<<" "<<gnums.at(1)<<" "<<gene_ids.at(1)<<endl;
	  //cout<<array.at(2)<<" "<<gnums.at(2)<<" "<<gene_ids.at(2)<<endl;
	  cout<<array[0]<<" "<<gnums[0]<<" "<<gene_ids[0]<<endl;
	  cout<<array[1]<<" "<<gnums[1]<<" "<<gene_ids[1]<<endl;
	  cout<<array[2]<<" "<<gnums[2]<<" "<<gene_ids[2]<<endl;
Raquel Bromberg's avatar
Raquel Bromberg committed
392
	  cout<<"Last row of array: "<<endl;
393
	  cout<<array[array.size()-1]<<" "<<gnums[gnums.size()-1]<<" "<<gene_ids[gene_ids.size()-1]<<endl;
Raquel Bromberg's avatar
Raquel Bromberg committed
394
	  cout<<"Second to last row of array: "<<endl;
395
	  cout<<array[array.size()-2]<<endl;
Raquel Bromberg's avatar
Raquel Bromberg committed
396
	  cout<<"Last row of array: "<<endl;
397
	  cout<<array[array.size()-1]<<endl;
Raquel Bromberg's avatar
Raquel Bromberg committed
398
	  cout<<"Entering recursive function find_matches_it0(...)..."<<endl;
399
	  find_matches_it0(0,0,array.size()-1,"");//, aastr, astr);
Raquel Bromberg's avatar
Raquel Bromberg committed
400
401
402
403
	  cout<<"Escaped recursive function find_matches(...)!"<<endl;
	}
    }
  else if(partial_run)
David Trudgian's avatar
David Trudgian committed
404
405
406
407
    {
      array.clear();
      gnums.clear();
      gene_ids.clear();
408
409
      //      set_array_it0(aas.get_aa(partial_run_lead));
      set_array_it0(get_aa_int[(int)partial_run_lead-65]);
David Trudgian's avatar
David Trudgian committed
410
      cout<<"First 3 rows of array: "<<endl;
411
412
413
      cout<<array[0]<<" "<<gnums[0]<<" "<<gene_ids[0]<<endl;
      cout<<array[1]<<" "<<gnums[1]<<" "<<gene_ids[1]<<endl;
      cout<<array[2]<<" "<<gnums[2]<<" "<<gene_ids[2]<<endl;
David Trudgian's avatar
David Trudgian committed
414
      cout<<"Last row of array: "<<endl;
415
      cout<<array[array.size()-1]<<" "<<gnums[gnums.size()-1]<<" "<<gene_ids[gene_ids.size()-1]<<endl;
David Trudgian's avatar
David Trudgian committed
416
      cout<<"Second to last row of array: "<<endl;
417
      cout<<array[array.size()-2]<<endl;
David Trudgian's avatar
David Trudgian committed
418
      cout<<"Last row of array: "<<endl;
419
      cout<<array[array.size()-1]<<endl;
David Trudgian's avatar
David Trudgian committed
420
      cout<<"Entering recursive function find_matches_it0(...)..."<<endl;
421
      find_matches_it0(0,0,array.size()-1,"");//, aastr, astr);
David Trudgian's avatar
David Trudgian committed
422
423
424
425
426
427
428
429
430
431
432
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
459
460
461
462
463
464
465
466
467
468
469
470
      cout<<"Escaped recursive function find_matches(...)!"<<endl;
    }
  
  cout<<"Done with mctr main algorithm."<<endl;

  write_out_data_it0();
}

/*******************
Code for counting matches between problematic (HGT) pairs and a reference set.
 ******************/
int mctr::run_it1()
{ 
  if(MCTR_DEBUG)
    {
      cout<<"bool mctr::run_it1()"<<endl;
    }

  int total=refsetsize;

  if(prob1>=refsetsize)
    {
      total++;
    }
  if(prob2>=refsetsize)
    {
      total++;
    }

  number_of_genomes=number_of_genomes_ref=total;

  if(MCTR_DEBUG)
    {
      cout<<"total="<<total<<endl;
      cout<<"number_of_genomes="<<number_of_genomes<<endl;
    }
  
  for(int i=0; i<20; i++)
    {
      if(MCTR_DEBUG)
	{
	  cout<<i<<"/"<<20<<endl;
	}

      array.clear();
      gnums.clear();
      gene_ids.clear();
      set_array_it1(i);
      
471
      find_matches_it1(0,0,array.size()-1,"");
David Trudgian's avatar
David Trudgian committed
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
    }

  cout<<"Finished match-counting for "<<prob1<<" "<<prob2<<endl;
  cout<<"con_map.size() = "<<con_map.size()<<endl;
  cout<<"con_map_set0.size() = "<<con_map_set0.size()<<endl;
  cout<<"con_map_set1.size() = "<<con_map_set1.size()<<endl;
  cout<<"hgt_map.size() = "<<hgt_map.size()<<endl;
  
#pragma omp critical
  write_out_data_it1();
  
  return genes_found;
}

void mctr::run_it2()
{
  if(MCTR_DEBUG)
    {
      cout<<"void mctr::run_it2()"<<endl;
#pragma omp critical
      logstream<<"//In void mctr::run_it2() for prob1="<<prob1<<" prob2="<<prob2<<endl;
    }
  
  for(int i=0; i<20; i++)
    {
      array.clear();
      gnums.clear();
      gene_ids.clear();

      set_array_it2(i);

      if(MCTR_DEBUG)
	{
	  cout<<prob1<<" "<<prob2<<" Last row of array: "<<endl;
506
	  cout<<array[array.size()-1]<<" "<<gnums[gnums.size()-1]<<" "<<gene_ids[gene_ids.size()-1]<<endl;
David Trudgian's avatar
David Trudgian committed
507
	  cout<<"Second to last row of array: "<<endl;
508
	  cout<<array[array.size()-2]<<" "<<gnums[gnums.size()-2]<<" "<<gene_ids[gene_ids.size()-2]<<endl;
David Trudgian's avatar
David Trudgian committed
509
510
511
	  cout<<prob1<<" "<<prob2<<" "<<msc<<endl;
	}

512
      find_matches_it2(0,0,array.size()-1,"");
David Trudgian's avatar
David Trudgian committed
513
514
515
516
517
518
519
520

      if(MCTR_DEBUG)
	{
	  cout<<prob1<<" "<<prob2<<" Done in find_matches"<<endl;
	}
     }  

  cout<<"Finished match-counting (it2) for "<<prob1<<" "<<prob2<<endl;
521

David Trudgian's avatar
David Trudgian committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  write_out_data_it2();
}



/*****************************************************
set_bitsv()
*****************************************************/
void mctr::set_bitsv()
{
  logstream<<"void mctr::set_bitsv()"<<endl;
  logstream<<"Setting the bitsv vector..."<<endl;

  if(run_type==0) //the normal case.
    {
Raquel Bromberg's avatar
Raquel Bromberg committed
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
      double max=-1;	  
	  
      for(int numg=0; numg<number_of_genomes; numg++)
	{
	  ifstream histstream;
	  u.open_ifile(histstream,path+"/"+bfn+"/HIST/"+bfn+"_"+u.int_to_string(numg)+"_hist.txt","histstream");
	  
	  bits_holder bh_temp;
	  int gnum;
	  histstream>>gnum;
	  if(gnum!=numg)
	    {
	      cout<<"Mismatch in set_bitsv case 0."<<endl;
	      exit(1);
	    }

	  int total_residues;
	  histstream>>total_residues;

	  for(int j=0; j<20; j++)
	    {
	      int count;
	      double frequency;
	      histstream>>count>>frequency;
	      bh_temp.d[j]=(double)count/(double)total_residues;
	      bh_temp.d[j]=( (log(bh_temp.d[j])) * (-1.0)) / 2.0; //NOTE: using log, which in C++ uses base e.
	      //NOTE: Divide by 2 here to speed up calculating scores in get_score(...)
	      if(max==-1 || max<bh_temp.d[j])
		{
		  max = bh_temp.d[j];
		}
	    }
	    bitsv.push_back(bh_temp);
	    histstream.close();
	}
David Trudgian's avatar
David Trudgian committed
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
      
      max_single_bit_value=max;
    } //end of normal case run_type==0

  else if(run_type==1) //Running prospective HGT pair against a reference set.
    {
      ofstream outstream;
      u.open_ofile(outstream,path+"/"+bfn+"/"+bfn+"_msc_ref_keys.txt","outstream");

      //Original ordinal, then new ordinal
      outstream<<prob1<<" "<<0<<endl;
      outstream<<prob2<<" "<<1<<endl;
      keys.push_back(std::make_pair(prob1,0) );
      keys.push_back(std::make_pair(prob2,1) );

      for(int i=0; i<refsetsize; i++)
	{
	  if(i!=prob1 && i!=prob2)
	    {
	      int p1,p2;
	      if(i>prob1 && i>prob2)
		{
		  outstream<<i<<" "<<i<<endl;
		  p1=i;
		  p2=i;
		}
	      else if(i>prob1)
		{
		  outstream<<i<<" "<<i+1<<endl;
		  p1=i;
		  p2=i+1;
		}
	      else
		{
		  outstream<<i<<" "<<i+2<<endl;
		  p1=i;
		  p2=i+2;
		}
	      keys.push_back(std::make_pair(p1,p2) );
	    }
	}

      outstream.close();
      
      //get the bits for the 2 problem proteomes.
      double max=-1;
      for(int i=0; i<number_of_genomes; i++) //number_of_genomes, NOT number_of_genomes_ref, because the code has to go through the entire set to find a) all the reference genomes and b) the two problem genomes.
	{
Raquel Bromberg's avatar
Raquel Bromberg committed
620
621
622
623
624
625
626
627
628
629
	  ifstream probstream;
	  string probfile=path+"/"+bfn+"/HIST/"+bfn+"_"+u.int_to_string(i)+"_hist.txt";
	  probstream.open(probfile.c_str());
	  if(probstream.fail())
	    {
	      cout<<"Failed to open probstream to probfile="<<probfile<<endl;
	      cout<<"run_type=="<<run_type<<endl;
	      exit(1);
	    }
	  
David Trudgian's avatar
David Trudgian committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
	  int gnum;
	  probstream>>gnum;
	  int total_residues;
	  probstream>>total_residues;

	  bits_holder bh_temp;
	  for(int j=0; j<20; j++)
	    {
	      int count;
	      double frequency;
	      probstream>>count>>frequency;
	      bh_temp.d[j]=(double)count/(double)total_residues;
	      bh_temp.d[j]=( (log(bh_temp.d[j])) * (-1.0)) / 2.0; //divide by 2 here to speed up calculating scores in get_score(...)
	      if(max==-1 || max<bh_temp.d[j])
		{
		  max = bh_temp.d[j];
		}
	    }

	  if(gnum==prob1)
	    {
	      bitsv.insert(bitsv.begin(),bh_temp);
	    }
	  else if(gnum==prob2)
	    {
	      vector<bits_holder>::iterator it;
	      it=bitsv.begin();
	      it++;
	      bitsv.insert(it,bh_temp);
	    }
	  else if(gnum<refsetsize)
	    {
	      bitsv.push_back(bh_temp);
	    }
Raquel Bromberg's avatar
Raquel Bromberg committed
664
	  probstream.close();      
David Trudgian's avatar
David Trudgian committed
665
666
667
	}

      max_single_bit_value=max;
Raquel Bromberg's avatar
Raquel Bromberg committed
668
    } //run_type==1
David Trudgian's avatar
David Trudgian committed
669
670
671
672
673
674
675

  else if(run_type==2) //Running a pair against itself, with the suspicious genes already flagged by the previous run against a reference set.
    {
      //get the bits for the 2 problem proteomes.
      double max=-1;
      for(int i=0; i<number_of_genomes; i++) //number_of_genomes, NOT number_of_genomes_ref, because the code has to go through the entire set to find a) all the reference genomes and b) the two problem genomes.
	{
Raquel Bromberg's avatar
Raquel Bromberg committed
676
677
678
679
680
681
682
683
684
685
	  ifstream probstream;
	  string probfile=path+"/"+bfn+"/HIST/"+bfn+"_"+u.int_to_string(i)+"_hist.txt";
	  probstream.open(probfile.c_str());
	  if(probstream.fail())
	    {
	      cout<<"Failed to open probstream to probfile="<<probfile<<endl;
	      cout<<"run_type=="<<run_type<<endl;
	      exit(1);
	    }

David Trudgian's avatar
David Trudgian committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
	  bits_holder bh_temp;
	  int gnum;
	  probstream>>gnum;
	  int total_residues;
	  probstream>>total_residues;

	  for(int j=0; j<20; j++)
	    {
	      int count;
	      double frequency;
	      probstream>>count>>frequency;
	      bh_temp.d[j]=(double)count/(double)total_residues;
	      bh_temp.d[j]=( (log(bh_temp.d[j])) * (-1.0)) / 2.0; //divide by 2 here to speed up calculating scores in get_score(...)
	      if(max==-1 || max<bh_temp.d[j])
		{
		  max = bh_temp.d[j];
		}
	    }
	  
	  if(gnum==prob1)
	    {
	      bitsv.insert(bitsv.begin(),bh_temp);
	    }
	  else if(gnum==prob2)
	    {
	      vector<bits_holder>::iterator it;
	      it=bitsv.begin();
	      it++;
	      bitsv.insert(it,bh_temp);
	    }
Raquel Bromberg's avatar
Raquel Bromberg committed
716
	  probstream.close();
David Trudgian's avatar
David Trudgian committed
717
	}
Raquel Bromberg's avatar
Raquel Bromberg committed
718
      
David Trudgian's avatar
David Trudgian committed
719
      max_single_bit_value=max;
Raquel Bromberg's avatar
Raquel Bromberg committed
720
      
David Trudgian's avatar
David Trudgian committed
721
722
723
724
725
726
727
728
      if(bitsv.size()!=2)
	{
	  cout<<"Problem in void mctr::set_bitsv()"<<endl;
	  cout<<"bitsv.size()="<<bitsv.size()<<endl;
	  cout<<"prob1 = "<<prob1<<endl;
	  cout<<"prob2 = "<<prob2<<endl;
	  exit(1);
	}
Raquel Bromberg's avatar
Raquel Bromberg committed
729
730
    } //run_type==2

David Trudgian's avatar
David Trudgian committed
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
} //end of set_bitsv()

/***********************************
set_array()
************************************/
void mctr::set_array_it0(int i)
{
  cout<<"In mctr_optim1::set_array(int i). int i = "<<i<<endl;
  logstream<<"In mctr_optim1::set_array(int i). int i = "<<i<<endl;
  //char current_amino_acid = aas.get_aa(i);
  stringstream ss;
  ss<<i;
  string s = ss.str();
  ifstream instream;
  string infile = path + "/"+bfn + "/"+merged_directory+"/TAGS"+s+".txt";
  cout<<"infile = "<<infile<<endl;
  logstream<<"infile = "<<infile<<endl;
  instream.open( infile.c_str());
  if(instream.fail())
    {
      cout<<"In mctr::set_array(int i)"<<endl;
      cout<<"Failed to open instream to infile = "<<infile<<endl;
      exit(1);
    }
  
  int total_size;
  instream>>total_size;
  cout<<"\tTotal tags in current file = "<<total_size<<endl;
  logstream<<"\tTotal tags in current file = "<<total_size<<endl;
  
  for(int k=0; k<total_size; k++)
    {
      string sequence;
      int gnumber;
      int gene_id;
      instream>>sequence>>gnumber;
      if(sequence.length()<tag_length)
	{
	  cout<<"Hit the empty string. Bye."<<endl;
	  exit(1);
	}
      instream>>gene_id;
      gene_ids.push_back(gene_id);	   
      array.push_back(sequence);
      gnums.push_back(gnumber);
    }
  
  instream.close();
  cout<<"Closed "<<infile<<endl;
  cout<<"Size of array = "<<array.size()<<endl;
  cout<<"Size of gnums = "<<gnums.size()<<endl;      
  cout<<"Size of gene_ids = "<<gene_ids.size()<<endl;
  
  if(array.size()!=gnums.size())
    {
      cout<<"array.size()!=gnums.size()"<<endl;
      exit(1);
    }
  
  else if(gene_ids.size()!=gnums.size())
    {
      cout<<"gene_ids.size()="<<gene_ids.size()<<" != gnums.size()="<<gnums.size()<<endl;
      exit(1);
    }
} // *****end of mctr::set_array_it0(int i)*****

/***********************************
set_array_it1()
************************************/
void mctr::set_array_it1(int i)
{
  if(MCTR_DEBUG)
    {
      cout<<"void mctr::set_array_it1(int i)"<<endl;
      cout<<"i="<<i<<endl;
      cout<<"ref_index="<<ref_index<<endl;
      cout<<"ppv_index="<<ppv_index<<endl;
      cout<<"ref_tags.size()="<<ref_tags.size()<<endl;
    }
  
  logstream<<"void mctr::set_array_it1(int i)"<<endl;
  logstream<<"i="<<i<<endl;

814
815
  //  char caa = aas.get_aa(i);
  char caa = get_aa_char[i];
David Trudgian's avatar
David Trudgian committed
816

817
818
  string rs=ref_tags[ref_index]; //set to 0 in constructor.
  string ps=problem_tags[ppv_index];
David Trudgian's avatar
David Trudgian committed
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
  
  if(MCTR_DEBUG)
    {
      cout<<"caa="<<caa<<endl;
      cout<<"rs="<<rs<<endl;
      cout<<"ps="<<ps<<endl;
    }
  
  while(rs[0]==caa && ps[0]==caa && ref_index<ref_tags.size() && ppv_index<problem_tags.size() )
    {
      if(rs<ps)
	{
	  string sequence;
	  int gnumber;
	  int gene_id;
	  istringstream iss(rs);
	  iss>>sequence>>gnumber>>gene_id;
	  //leave out tags from problem sequences since these problem proteomes have already had their tags generated in problem_tags.
	  int new_number = gnumber-2;
	  if( new_number>prob2)
	    {
	      //new_number=gnumber-2;
	    }
	  else if(new_number==prob2)
	    {
	      new_number=-1;
	    }
	  else if(new_number>prob1)
	    {
	      new_number=gnumber-1;
	    }
	  else if(new_number==prob1)
	    {
	      new_number=-1;
	    }
	  else if(new_number<prob1)
	    {
	      new_number=gnumber;
	    }
	  
	  if(new_number!=-1)
	    {
	      array.push_back(sequence);
	      gnums.push_back(new_number);
	      gene_ids.push_back(gene_id);
	    }
	  
	  ref_index++;
	  if(ref_index<ref_tags.size())
	    {
869
870
	      //	      rs=ref_tags.at(ref_index);
	      rs=ref_tags[ref_index];
David Trudgian's avatar
David Trudgian committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
	    }
	}
      else
	{
	  string sequence;
	  int gnumber;
	  int gene_id;
	  istringstream iss(ps);
	  iss>>sequence>>gnumber>>gene_id;
	  array.push_back(sequence);
	  gnums.push_back(gnumber);
	  gene_ids.push_back(gene_id);
	  ppv_index++;
	  if(ppv_index<problem_tags.size())
	    {
886
887
	      //	      ps=problem_tags.at(ppv_index);
	      ps=problem_tags[ppv_index];
David Trudgian's avatar
David Trudgian committed
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
	    }
	}
    }
  while(rs[0]==caa && ref_index<ref_tags.size() )
    {
      string sequence;
      int gnumber;
      int gene_id;
      istringstream iss(rs);
      iss>>sequence>>gnumber>>gene_id;
      int new_number = gnumber-2;
      if( new_number>prob2)
	{
	  //new_number=gnumber-2;
	}
      else if(new_number==prob2)
	{
	  new_number=-1;
	}
      else if(new_number>prob1)
	{
	  new_number=gnumber-1;
	}
      else if(new_number==prob1)
	{
	  new_number=-1;
	}
      else if(new_number<prob1)
	{
	  new_number=gnumber;
	}
      
      if(new_number!=-1)
	{
	  array.push_back(sequence);
	  gnums.push_back(new_number);
	  gene_ids.push_back(gene_id);
	}
      
      ref_index++;
      if(ref_index<ref_tags.size())
	{
930
931
	  //	  rs=ref_tags.at(ref_index);
	  rs=ref_tags[ref_index];
David Trudgian's avatar
David Trudgian committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
	}
    }
  while(ps[0]==caa && ppv_index<problem_tags.size())
    {
      string sequence;
      int gnumber;
      int gene_id;
      istringstream iss(ps);
      iss>>sequence>>gnumber>>gene_id;
      array.push_back(sequence);
      gnums.push_back(gnumber);
      gene_ids.push_back(gene_id);
      ppv_index++;
      if(ppv_index<problem_tags.size())
	{
947
948
	  //	  ps=problem_tags.at(ppv_index);
	  ps=problem_tags[ppv_index];
David Trudgian's avatar
David Trudgian committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
	}
    }
  
  cout<<prob1<<" "<<prob2<<": array set ("<<caa<<")."<<endl;

  if(array.size()!=gnums.size())
    {
      cout<<"array.size()!=gnums.size()"<<endl;
      exit(1);
    }
  
  else if(gene_ids.size()!=gnums.size())
    {
      cout<<"gene_ids.size()="<<gene_ids.size()<<" != gnums.size()="<<gnums.size()<<endl;
      exit(1);
    }
} // *****end of mctr::set_array_it1(int i)*****

/***********************************
set_array_2()
************************************/
void mctr::set_array_it2(int i)
{
  logstream<<"In mctr_optim1::set_array(int i). int i = "<<i<<endl;
973
974
975
976
977
978

  //  char caa = aas.get_aa(i);
  char caa=get_aa_char[i];

  //  string ps=problem_tags.at(ppv_index);
  string ps=problem_tags[ppv_index];
David Trudgian's avatar
David Trudgian committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  
  while(ps[0]==caa && ppv_index<problem_tags.size())
    {
      string sequence;
      int gnumber;
      int gene_id;
      istringstream iss(ps);
      iss>>sequence>>gnumber>>gene_id;
      array.push_back(sequence);
      gnums.push_back(gnumber);
      gene_ids.push_back(gene_id);
      ppv_index++;
      if(ppv_index<problem_tags.size())
	{
993
994
	  //	  ps=problem_tags.at(ppv_index);
	  ps=problem_tags[ppv_index];
David Trudgian's avatar
David Trudgian committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
	}
    }

  cout<<prob1<<" "<<prob2<<": array set(it2)"<<endl;

} // *****end of mctr::set_array_it2(int i)*****


/************************************************
find_matches_it0(...)
Count matches between all pairs. 
No correction for HGT at this stage: the original match-counting.
**************************************************/
1008
void mctr::find_matches_it0(int col, int first_row, int last_row, string seq)
David Trudgian's avatar
David Trudgian committed
1009
1010
1011
1012
{
  if(MCTR_DEBUG)
    {
      cout<<"find_matches_it0(...): col = "<<col<<" first_row = "<<first_row<<" last_row = "<<last_row<<" seq = "<<seq<<endl;
1013
1014
1015
1016
      //cout<<"array.at(first_row) = " <<array.at(first_row)<<endl;
      cout<<"array.at(first_row) = " <<array[first_row]<<endl;
      //      cout<<"array.at(last_row) = "<<array.at(last_row)<<endl;
      cout<<"array.at(last_row) = "<<array[last_row]<<endl;
David Trudgian's avatar
David Trudgian committed
1017
1018
1019
1020
1021
1022
    }

  recursion_counter++;
  if(recursion_counter%100000==0)
    {
      cout<<"first_row = "<<first_row<<endl;
1023
1024
      //      cout<<array.at(first_row)<<" "<<gnums.at(first_row)<<" "<<gene_ids.at(first_row)<<endl;
      cout<<array[first_row]<<" "<<gnums[first_row]<<" "<<gene_ids[first_row]<<endl;
David Trudgian's avatar
David Trudgian committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    }

  //IF WE'VE ONLY GOT ONE SEQUENCE
  if(first_row==last_row)
    {
      //do nothing
    }

  //MULTIPLE SEQUENCES, LAST COLUMN
  else if(col==tag_length)
    {
      //do nothing, past the end of the sequence (e.g. previous recursion hit 20)
    }

  //IF WE'VE GOT MULTIPLE SEQUENCES (I.E. MORE MATCHES), NOT MAX LENGTH
  else
    {
      //SHORTCUT: IF THE ENTIRE BLOCK HAS THE SAME LEADING CHARACTER
1043
1044
      //      if(array.at(last_row)[col]==array.at(first_row)[col])
      if(array[last_row][col]==array[first_row][col])
David Trudgian's avatar
David Trudgian committed
1045
	{
1046
1047
1048
	  //	  int aa_index=get_aa(array.at(first_row)[col]);
	  //	  int aa_index=get_aa_int[(int)(array.at(first_row)[col])-65];
	  int aa_index=get_aa_int[(int)(array[first_row][col])-65];
David Trudgian's avatar
David Trudgian committed
1049
1050
1051
1052
	  
	  //IF THE SINGLE-CHARACTER COL IS NOT A ^ COLUMN
	  if(aa_index>=0 && aa_index<20)
	    {
1053
1054
	      find_matches_it0(col+1,first_row,last_row,seq+array[first_row][col]);
	      increment_bits_it0(first_row,last_row,seq+array[first_row][col]);
David Trudgian's avatar
David Trudgian committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
	    }
	}

      //IF THERE ARE DIFFERENT LEADING CHARACTERS, NEED TO PARTITION ARRAY FURTHER
      else
	{
	  int index_start=first_row;
	  int index_end=first_row;
	  
	  while(index_start<last_row)
	    {
1066
1067
	      //	      char leader=array.at(index_start)[col];
	      char leader=array[index_start][col];
David Trudgian's avatar
David Trudgian committed
1068
	      
1069
	      while(index_end<=last_row && array[index_end][col]==leader)
David Trudgian's avatar
David Trudgian committed
1070
1071
1072
1073
		{
		  index_end++;
		}
	      
1074
1075
	      //	      int aa_index = get_aa(leader);
	      int aa_index = get_aa_int[(int)leader-65];
David Trudgian's avatar
David Trudgian committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
	      
	      //IF THEY'RE THE SAME VALUE (SHOULD NEVER HAPPEN)
	      if(index_end==index_start)
		{
		}
	      //IF THEY'RE JUST 1 OFF: NO MATCHES AT THIS DEPTH
	      else if(index_end==index_start+1)
		{
		}
	      //IF THERE ARE MATCHES
	      else if(aa_index<20 && aa_index>=0)
		{
1088
1089
		  find_matches_it0(col+1, index_start, index_end-1,seq+leader);
		  increment_bits_it0(index_start,index_end-1,seq+leader);
David Trudgian's avatar
David Trudgian committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
		}
	      index_start=index_end;	      
	    } //end of while(index_start<last_row)
	}
    }
  if(MCTR_DEBUG)
    {
      cout<<"End of find_matches_it0(...)"<<endl;
    }
}

/************************************************
find_matches_it1(...)
Run between problematic (HGT) pair and reference set.
Match-counting not really used.
Just quickly assessing and marking long kmer matches in the set.
**************************************************/
1107
void mctr::find_matches_it1(int col, int first_row, int last_row, string seq)
David Trudgian's avatar
David Trudgian committed
1108
1109
1110
1111
{
  if(MCTR_DEBUG)
    {
      cout<<"col = "<<col<<" first_row = "<<first_row<<" last_row = "<<last_row<<" seq = "<<seq<<endl;
1112
1113
1114
1115
      //      cout<<"array.at(first_row) = " <<array.at(first_row)<<endl;
      //      cout<<"array.at(last_row) = "<<array.at(last_row)<<endl;
      cout<<"array.at(first_row) = " <<array[first_row]<<endl;
      cout<<"array.at(last_row) = "<<array[last_row]<<endl;
David Trudgian's avatar
David Trudgian committed
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
    }

  recursion_counter++;

  //IF WE'VE ONLY GOT ONE SEQUENCE
  if(first_row==last_row)
    {
      //do nothing
    }

  //MULTIPLE SEQUENCES, LAST COLUMN
  else if(col==tag_length)
    {
      //do nothing, past the end of the sequence (e.g. previous recursion hit 20)
    }

  //IF WE'VE GOT MULTIPLE SEQUENCES (I.E. MORE MATCHES), NOT MAX LENGTH
  else
    {
      //SHORTCUT: IF THE ENTIRE BLOCK HAS THE SAME LEADING CHARACTER
1136
1137
      //      if(array.at(last_row)[col]==array.at(first_row)[col])
      if(array[last_row][col]==array[first_row][col])
David Trudgian's avatar
David Trudgian committed
1138
	{
1139
1140
1141
	  //	  int aa_index=get_aa(array.at(first_row)[col]);
	  //	  int aa_index=get_aa_int[(int)(array.at(first_row)[col])-65];
	  int aa_index=get_aa_int[(int)(array[first_row][col])-65];
David Trudgian's avatar
David Trudgian committed
1142
1143
1144
1145
	  
	  //IF THE SINGLE-CHARACTER COL IS NOT A ^ COLUMN
	  if(aa_index>=0 && aa_index<20)
	    {
1146
1147
1148
1149
	      //	      find_matches_it1(col+1,first_row,last_row,seq+array.at(first_row)[col]);
	      //	      increment_bits_it1(first_row,last_row,seq+array.at(first_row)[col]);
	      find_matches_it1(col+1,first_row,last_row,seq+array[first_row][col]);
	      increment_bits_it1(first_row,last_row,seq+array[first_row][col]);
David Trudgian's avatar
David Trudgian committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
	    }
	}//end of shortcut
      
      //IF THERE ARE DIFFERENT LEADING CHARACTERS, NEED TO PARTITION ARRAY FURTHER
      else
	{
	  int index_start=first_row;
	  int index_end=first_row;
	  
	  while(index_start<last_row)
	    {
1161
1162
	      //	      char leader=array.at(index_start)[col];
	      char leader=array[index_start][col];
David Trudgian's avatar
David Trudgian committed
1163
	      
1164
	      while(index_end<=last_row && array[index_end][col]==leader)
David Trudgian's avatar
David Trudgian committed
1165
1166
1167
1168
		{
		  index_end++;
		}
	      	      
1169
1170
	      //	      int aa_index = get_aa(leader);
	      int aa_index = get_aa_int[(int)leader-65];
David Trudgian's avatar
David Trudgian committed
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182

	      //IF THEY'RE THE SAME VALUE (SHOULD NEVER HAPPEN)
	      if(index_end==index_start)
		{
		}
	      //IF THEY'RE JUST 1 OFF: NO MATCHES AT THIS DEPTH
	      else if(index_end==index_start+1)
		{
		}
	      //IF THERE ARE MATCHES
	      else if(aa_index<20 && aa_index>=0)
		{
1183
1184
		  find_matches_it1(col+1, index_start, index_end-1,seq+leader);
		  increment_bits_it1(index_start,index_end-1,seq+leader);
David Trudgian's avatar
David Trudgian committed
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
		}
	      index_start=index_end;	      
	    } //end of while(index_start<last_row)
	}
    }
} //end of find_matches_it1(...)

/************************************************
find_matches_it2(...)
**************************************************/
1195
void mctr::find_matches_it2(int col, int first_row, int last_row, string seq)//, ofstream& alalstream, ofstream& alstream)
David Trudgian's avatar
David Trudgian committed
1196
1197
1198
1199
{
  if(MCTR_DEBUG)
    {
      cout<<"col = "<<col<<" first_row = "<<first_row<<" last_row = "<<last_row<<" seq = "<<seq<<endl;
1200
1201
1202
1203
      //      cout<<"array.at(first_row) = " <<array.at(first_row)<<endl;
      //      cout<<"array.at(last_row) = "<<array.at(last_row)<<endl;
      cout<<"array.at(first_row) = " <<array[first_row]<<endl;
      cout<<"array.at(last_row) = "<<array[last_row]<<endl;
David Trudgian's avatar
David Trudgian committed
1204
1205
1206
1207
1208
1209
    }

  recursion_counter++;
  if(recursion_counter%100000==0)
    {
      cout<<"first_row = "<<first_row<<" for prob1="<<prob1<<" prob2="<<prob2<<endl;
1210
1211
      //      cout<<array.at(first_row)<<" "<<gnums.at(first_row)<<" "<<gene_ids.at(first_row)<<endl;
      cout<<array[first_row]<<" "<<gnums[first_row]<<" "<<gene_ids[first_row]<<endl;
David Trudgian's avatar
David Trudgian committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
    }

  //IF WE'VE ONLY GOT ONE SEQUENCE
  if(first_row==last_row)
    {
      //do nothing
    }

  //MULTIPLE SEQUENCES, LAST COLUMN
  else if(col==tag_length)
    {
      //do nothing, past the end of the sequence (e.g. previous recursion hit 20)
    }

  //IF WE'VE GOT MULTIPLE SEQUENCES (I.E. MORE MATCHES), NOT MAX LENGTH
  else
    {
      //SHORTCUT: IF THE ENTIRE BLOCK HAS THE SAME LEADING CHARACTER
1230
1231
      //      if(array.at(last_row)[col]==array.at(first_row)[col])
      if(array[last_row][col]==array[first_row][col])
David Trudgian's avatar
David Trudgian committed
1232
	{
1233
1234
1235
	  //	  int aa_index=get_aa(array.at(first_row)[col]);
	  //	  int aa_index=get_aa_int[(int)array.at(first_row)[col]-65];
	  int aa_index=get_aa_int[(int)array[first_row][col]-65];
David Trudgian's avatar
David Trudgian committed
1236
1237
1238
1239
	  
	  //IF THE SINGLE-CHARACTER COL IS NOT A ^ COLUMN
	  if(aa_index>=0 && aa_index<20)
	    {
1240
1241
1242
1243
	      //	      find_matches_it2(col+1,first_row,last_row,seq+array.at(first_row)[col]);
	      //	      increment_bits_it2(first_row,last_row,seq+array.at(first_row)[col]);
	      find_matches_it2(col+1,first_row,last_row,seq+array[first_row][col]);
	      increment_bits_it2(first_row,last_row,seq+array[first_row][col]);
David Trudgian's avatar
David Trudgian committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
	    }
	}//end of shortcut

      //IF THERE ARE DIFFERENT LEADING CHARACTERS, NEED TO PARTITION ARRAY FURTHER
      else
	{
	  int index_start=first_row;
	  int index_end=first_row;

	  while(index_start<last_row)
	    {
1255
	      char leader=array[index_start][col];
David Trudgian's avatar
David Trudgian committed
1256
	      
1257
	      while(index_end<=last_row && array[index_end][col]==leader)
David Trudgian's avatar
David Trudgian committed
1258
1259
1260
1261
		{
		  index_end++;
		}
	      	      
1262
1263
	      //	      int aa_index = get_aa(leader);
	      int aa_index = get_aa_int[(int)leader-65];
David Trudgian's avatar
David Trudgian committed
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275

	      //IF THEY'RE THE SAME VALUE (SHOULD NEVER HAPPEN)
	      if(index_end==index_start)
		{
		}
	      //IF THEY'RE JUST 1 OFF: NO MATCHES AT THIS DEPTH
	      else if(index_end==index_start+1)
		{
		}
	      //IF THERE ARE MATCHES
	      else if(aa_index<20 && aa_index>=0)
		{
1276
1277
		  find_matches_it2(col+1, index_start, index_end-1,seq+leader);
		  increment_bits_it2(index_start,index_end-1,seq+leader);
David Trudgian's avatar
David Trudgian committed
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
		}
	      index_start=index_end;	      
	    } //end of while(index_start<last_row)
	}
    }
}

/*******************************************
increment_bits_it0(...)
-used for run_it0; the main run, counting all matches between all inputs.
-separate from hgt/phage fix.
*********************************************/
1290
void mctr::increment_bits_it0(int first_row, int last_row,string seq)//,ofstream& alalstream, ofstream& alstream)
David Trudgian's avatar
David Trudgian committed
1291
1292
1293
1294
1295
1296
1297
{
  if(MCTR_DEBUG)
    {
      cout<<"increment_bits_it0(...)"<<endl;
      cout<<"first_row="<<first_row<<" last_row="<<last_row<<" seq="<<seq<<endl;
    }

1298
1299
1300
1301
1302
1303
1304
1305
  bool * gh; //replacing gnum_holder.
  gh = new bool[number_of_genomes];
  //  cout<<"number of genomes="<<number_of_genomes<<endl;
  
  for(int i=0; i<number_of_genomes; i++)
    {
      gh[i]=false;
    }
David Trudgian's avatar
David Trudgian committed
1306

1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  int num_uniq_genomes = set_gh(gh,gnums,first_row,last_row);

  //  cout<<"Top of increment_bits_it0..."<<endl;
  //  cout<<"size of gh = number_of_genomes = "<<number_of_genomes<<endl;
  //  cout<<"num_uniq_genomes = "<<num_uniq_genomes<<endl;
  //  cout<<"Contents of gh:"<<endl;
  //  for(int i=0; i<number_of_genomes; i++)
  //   {
  ///      cout<<i<<" "<<gh[i]<<endl;
  //    }
  
  //  set<int> gnum_holder; //using a set to deal with repeats within the same organism (sets are non-redundant)
  //bool store_gene_ids=false; //Used for storing gene numbers in bitterv because their score >= match_score_cutoff.

  //  bool full=false;
  //set<int> gnum_diag_hit;
  //pair<set<int>::iterator,bool> ret;
David Trudgian's avatar
David Trudgian committed
1324
1325

  //Store all ordinal IDs in a set.
1326
  /*  for(int i=first_row; i<=last_row && !full; i++)
David Trudgian's avatar
David Trudgian committed
1327
    {
1328
1329
      //      int gnum=gnums.at(i);
      int gnum=gnums[i];
David Trudgian's avatar
David Trudgian committed
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
      ret = gnum_holder.insert(gnum);
      if(gnum_holder.size()>=number_of_genomes && gnum_diag_hit.size()>=number_of_genomes)
	{
	  full=true;
	}
      if(ret.second==false) //This means gnum was already in the set. Since the sequence is present twice in the same organism, that means the organism has a match within itself; therefore add the number to the diagonal set.
	{
	  gnum_diag_hit.insert(gnum);
	}
    }
1340
  */
David Trudgian's avatar
David Trudgian committed
1341
1342
1343
1344
1345

  if(MCTR_DEBUG)
    {
      cout<<"In increment_bits_it0(...)"<<endl;
      cout<<"first_row = "<<first_row<<" last_row = "<<last_row<<" seq = "<<seq<<endl;
1346
1347
1348
      //     cout<<"ret = ";
      //set<int>::iterator it;
      /*      for(it=gnum_holder.begin(); it!=gnum_holder.end(); it++)
David Trudgian's avatar
David Trudgian committed
1349
1350
1351
	{
	  cout<<*it<<" ";
	}
1352
1353
      */
      //cout<<endl;
David Trudgian's avatar
David Trudgian committed
1354
1355
1356
    }

  //Case 1: No matches between organisms or within organisms.
1357
1358
  //  if(gnum_holder.size()<=1 && gnum_diag_hit.size()==0)
  if(num_uniq_genomes<=1)
David Trudgian's avatar
David Trudgian committed
1359
    {
1360
      //      cout<<"Doing nothing"<<endl;
David Trudgian's avatar
David Trudgian committed
1361
1362
1363
      //do nothing
    }
  //Case 2: A match within an organism and not between organisms
1364
  /*  else if(gnum_holder.size()<=1 && gnum_diag_hit.size()>=1)
David Trudgian's avatar
David Trudgian committed
1365
1366
1367
1368
1369
    {
      set<int>::iterator it3;
      for(it3=gnum_diag_hit.begin(); it3!=gnum_diag_hit.end(); it3++)
	{
	  int it3_int = *it3;
1370
	  double score = get_score(it3_int,it3_int,seq);
David Trudgian's avatar
David Trudgian committed
1371
1372
	  corr_array->increment_element(it3_int,it3_int,(int)(score+.5));
	}
1373
1374
	}*/ 
  
David Trudgian's avatar
David Trudgian committed
1375
  //Case 3: A match between ALL organisms
1376
1377
  //  else if(gnum_holder.size()>=number_of_genomes) //shortcut
  else if(num_uniq_genomes==number_of_genomes)
David Trudgian's avatar
David Trudgian committed
1378
    {
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
      //      cout<<"full sized"<<endl;
      double * score_array;
      score_array = new double[seq.length()*num_uniq_genomes];
      
      //set a vector of scores
      int count=0;	  
      for(int i=0; i<number_of_genomes; i++)
	{
	  if(gh[i])
	    {
	      set_score_array(score_array,seq,count,i);
	      count++;
	    }
	}
      
David Trudgian's avatar
David Trudgian committed
1394
1395
1396
1397
      for(int i=0; i<number_of_genomes-1; i++)
	{
	  for(int j=i+1; j<number_of_genomes; j++)
	    {
1398
1399
1400
1401
1402
1403
1404
	      double score=0;
	      
	      for(int k=0; k<seq.length(); k++)
		{
		  score+=score_array[(i*seq.length())+k]+score_array[(j*seq.length())+k];
		}
	      
David Trudgian's avatar
David Trudgian committed
1405
1406
1407
	      corr_array->increment_element(i,j,(int)(score+.5));
	      if(seq.length()>=mbkl)
		{
1408
		  bitterv[get_bitterv_index(i,j)].add_new_score(score);
David Trudgian's avatar
David Trudgian committed
1409
1410
1411
1412
		}
	    }
	}
      
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
      /*      for(int i=0; i<number_of_genomes-1; i++)
	{	  
	  for(int j=i+1; j<number_of_genomes; j++)
	    {
	      double score = get_score(i,j,seq);
	      corr_array->increment_element(i,j,(int)(score+.5));
	      if(seq.length()>=mbkl)
		{
//		  bitterv.at(get_bitterv_index(i,j)).add_new_score(score);
		  bitterv[get_bitterv_index(i,j)].add_new_score(score);
		}
	    }
	}
      */
	/*      if(gnum_diag_hit.size()>0)
David Trudgian's avatar
David Trudgian committed
1428
1429
1430
1431
1432
	{
	  set<int>::iterator it1;
	  for(it1=gnum_diag_hit.begin(); it1!=gnum_diag_hit.end(); it1++)
	    {
	      int it1_int = *it1;
1433
	      double score = get_score(it1_int,it1_int,seq);
David Trudgian's avatar
David Trudgian committed
1434
1435
	      corr_array->increment_element(it1_int,it1_int,(int)(score+.5));
	    }
1436
1437
	    }*/
      delete[] score_array;
David Trudgian's avatar
David Trudgian committed
1438
1439
1440
1441
1442
    }

  //Case 4: A match between SOME non-identical organisms
  else
    {
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
      /*      cout<<"Case 4: Some but not all have matches."<<endl;
      cout<<"num_uniq_genomes = "<<num_uniq_genomes<<endl;
      cout<<"number_of_genomes = "<<number_of_genomes<<endl;
      cout<<"seq="<<seq<<endl;
      */
      
      //new code
      double * score_array;
      score_array = new double[seq.length()*num_uniq_genomes];
      //      cout<<"Size of score_array = "<<seq.length()*num_uniq_genomes<<endl;
      
      //set a vector of scores
      //      for(int i=0; i<number_of_genomes; i++)
      //    cout<<"In Case 4: going through set_score_array loop..."<<endl;
      int count=0;
      for(int i=0; i<number_of_genomes; i++)
	{
	  //	  cout<<"i="<<i<<endl;
	  if(gh[i])
	    {
	      set_score_array(score_array,seq,count,i);
	      count++;
	    }
	}
      //      cout<<"Done in loop"<<endl;
      
      int gh_index1=0;
      //      cout<<"new loop..."<<endl;

      for(int i=0; i<number_of_genomes-1; i++)
	{
	  //	  cout<<"i="<<i<<" out of "<<number_of_genomes-1<<endl;
	  if(gh[i])
	    {
	      int gh_index2=gh_index1+1;
	      for(int j=i+1; j<number_of_genomes; j++)
		{
		  //		  cout<<"j="<<j<<" out of "<<number_of_genomes<<endl;
		  if(gh[j])
		    {
		      double score=0;
		      for(int k=0; k<seq.length(); k++)
			{
			  score+=score_array[(gh_index1*seq.length())+k]+score_array[(gh_index2*seq.length())+k];
			}
		      //		      cout<<"score = "<<score<<endl;

		      int score2=score+.5;
		      //		      cout<<"score2 = "<<score2<<endl;
		      //		      corr_array->increment_element(i,j,(int)(score+.5));
		      corr_array->increment_element(i,j,score2);
		      //		      cout<<"Incremented the corr_array..."<<endl;
		      
		      if(seq.length()>=mbkl)
			{
			  bitterv[get_bitterv_index(i,j)].add_new_score(score);
			}
		      //		      cout<<"got through the if block..."<<endl;
		      
		      gh_index2++;
		    }
		}
	      gh_index1++;
	    }
	  //	  cout<<"done in the next loop"<<endl;
	}//end of new code

      //      cout<<"out of one of the loops. Does it get here?"<<endl;

      
      /*
      for(int i=0; i<number_of_genomes; i++)
	{
	  if(gh[i])
	    {
	      for(int j=i+1; j<number_of_genomes; j++)
		{
		  if(gh[j])
		    {
		      double score = get_score(i,j,seq);
		      corr_array->increment_element(i,j,(int)(score+.5));
		      
		      if(seq.length()>=mbkl)
			{
			  bitterv[get_bitterv_index(i,j)].add_new_score(score);
			} 
		    }
		}
	    }
	}

      */
      /*      set<int>::iterator it1;
David Trudgian's avatar
David Trudgian committed
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
      set<int>::iterator it2;      
      
      for(it1=gnum_holder.begin(); it1!=gnum_holder.end(); it1++)
	{
	  for(it2=gnum_holder.begin(); it2!=gnum_holder.end(); it2++)
	    {
	      int ig=*it1; //ig is a gnum
	      int jg=*it2; //jg is a gnum
	      if(ig<jg)
		{
1546
		  double score = get_score(ig,jg,seq);
David Trudgian's avatar
David Trudgian committed
1547
1548
1549
1550
		  corr_array->increment_element(ig,jg,(int)(score+.5));

		  if(seq.length()>=mbkl)
		    {
1551
		      bitterv[get_bitterv_index(ig,jg)].add_new_score(score);
David Trudgian's avatar
David Trudgian committed
1552
1553
1554
1555
		    }
		}
	    }
	}
1556
      */
David Trudgian's avatar
David Trudgian committed
1557
      //fill in the diagonal.
1558
      /*      set<int>::iterator it3;
David Trudgian's avatar
David Trudgian committed
1559
1560
1561
      for(it3=gnum_diag_hit.begin(); it3!=gnum_diag_hit.end(); it3++)
	{
	  int it3_int = *it3;
1562
	  double score = get_score(it3_int, it3_int,seq);
David Trudgian's avatar
David Trudgian committed
1563
	  corr_array->increment_element(it3_int,it3_int,(int)(score+.5));
1564
1565
1566
1567

	  }*/

      delete[] score_array;
David Trudgian's avatar
David Trudgian committed
1568
1569
    }

1570
1571
  delete[] gh;

David Trudgian's avatar
David Trudgian committed
1572
1573
1574
1575
1576
1577
  if(MCTR_DEBUG)
    {
      cout<<"End of increment_bits_it0(...)"<<endl;
    }
}

1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
int mctr::set_gh(bool gh[], vector<int>& gnums, int first_row, int last_row)
{
  int count=0;
  for(int i=first_row; i<=last_row; i++)
    {
      if(!gh[gnums[i]])
	{
	  gh[gnums[i]]=true;
	  count++;
	}
    }
  return count;
}

void mctr::set_score_array(double * score_array,string seq,int count,int gen_ord)
{
  for(int j=0; j<seq.length() && seq[j]!='^'; j++)
    {
      score_array[count*seq.length()+j]=bitsv[gen_ord].d[get_aa_int[(int)seq[j]-65]];
    }
}

David Trudgian's avatar
David Trudgian committed
1600
1601
1602
1603
1604
1605
1606
/*******************************************
increment_bits_it1(...)
Use when running a problematic (HGT) pair against a reference set.
run_type==1
 - The 2 problem proteomes are in positions 0 and 1 (or -2,-1). 
 - Ignore any groups of sequences in which these two proteomes are both absent.
*********************************************/
1607
void mctr::increment_bits_it1(int first_row, int last_row,string seq)//,ofstream& alalstream, ofstream& alstream)
David Trudgian's avatar
David Trudgian committed
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
{
  set<int> gnum_holder; //using a set to deal with repeats within the same organism (sets are non-redundant)
  bool store_gene_ids=false; //Used for storing gene numbers in bitterv because their score >= match_score_cutoff. Even when msc is true,

  bool full=false;
  set<int> gnum_diag_hit;
  pair<set<int>::iterator,bool> ret;

  //Store all ordinal IDs in a set.
  for(int i=first_row; i<=last_row && !full; i++)
    {
1619
1620
      //      int gnum=gnums.at(i);
      int gnum=gnums[i];
David Trudgian's avatar
David Trudgian committed
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
      ret = gnum_holder.insert(gnum);
      if(gnum_holder.size()>=number_of_genomes && gnum_diag_hit.size()>=number_of_genomes)
	{
	  full=true;
	}
      if(ret.second==false) //This means gnum was already in the set. Since the sequence is present twice in the same organism, that means the organism has a match within itself; therefore add the number to the diagonal set.
	{
	  gnum_diag_hit.insert(gnum);
	}
    }

  //Case 1: No matches between organisms or within organisms.
  if(gnum_holder.size()<=1 && gnum_diag_hit.size()==0)
    {
      //do nothing
    }
1637

David Trudgian's avatar
David Trudgian committed
1638
1639
1640
1641
1642
1643
1644
  //Case 2: A match within an organism and not between organisms
  else if(gnum_holder.size()<=1 && gnum_diag_hit.size()>=1) 
    {
      set<int>::iterator it3;
      for(it3=gnum_diag_hit.begin(); it3!=gnum_diag_hit.end(); it3++)
	{
	  int it3_int = *it3;
1645
	  double score = get_score(it3_int,it3_int,seq);
David Trudgian's avatar
David Trudgian committed
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
	  corr_array->increment_element(it3_int,it3_int,(int)(score+.5));
	}
    }

  //Case 3: A match between ALL organisms
  else if(gnum_holder.size()>=number_of_genomes) //shortcut
    {
      for(int i=0; i<number_of_genomes-1; i++)
	{
	  for(int j=i+1; j<number_of_genomes; j++)
	    {
1657
	      double score = get_score(i,j,seq);
David Trudgian's avatar
David Trudgian committed
1658
1659
1660
	      corr_array->increment_element(i,j,(int)(score+.5));
	      if(seq.length()>=mbkl)
		{
1661
		  bitterv[get_bitterv_index(i,j)].add_new_score(score);
David Trudgian's avatar
David Trudgian committed
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
		}
	      
	      if( (i<=1 || j<=1) ) //if either problem org is represented.
		{
		  if(seq.length()>=HGT_MIN_LENGTH)
		    {
		      store_gene_ids=true;
		    }
		}
	    }
	}
      
      if(gnum_diag_hit.size()>0)
	{
	  set<int>::iterator it1;
	  for(it1=gnum_diag_hit.begin(); it1!=gnum_diag_hit.end(); it1++)
	    {
	      int it1_int = *it1;
1680
	      double score = get_score(it1_int,it1_int,seq);
David Trudgian's avatar
David Trudgian committed
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
	      corr_array->increment_element(it1_int,it1_int,(int)(score+.5));
	    }
	}
    }

  //Case 4: A match between SOME non-identical organisms
  else
    {
      set<int>::iterator it1;
      set<int>::iterator it2;      
      
      for(it1=gnum_holder.begin(); it1!=gnum_holder.end(); it1++)
	{
	  for(it2=gnum_holder.begin(); it2!=gnum_holder.end(); it2++)
	    {
	      int ig=*it1; //ig is a gnum
	      int jg=*it2; //jg is a gnum
	      if(ig<jg)
		{
1700
		  double score = get_score(ig,jg,seq);
David Trudgian's avatar
David Trudgian committed
1701
1702
1703
1704

		  corr_array->increment_element(ig,jg,(int)(score+.5));
		  if(seq.length()>=mbkl)
		    {
1705
		      bitterv[get_bitterv_index(ig,jg)].add_new_score(score);
David Trudgian's avatar
David Trudgian committed
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
		    }

		  if(ig<=1 || jg<=1)
		    {
		      if(seq.length()>=HGT_MIN_LENGTH)
			{
			  store_gene_ids=true;
			}
		    }
		}
	    }
	}

      set<int>::iterator it3;
      for(it3=gnum_diag_hit.begin(); it3!=gnum_diag_hit.end(); it3++)
	{
	  int it3_int = *it3;
1723
	  double score = get_score(it3_int, it3_int,seq);
David Trudgian's avatar
David Trudgian committed
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
	  corr_array->increment_element(it3_int,it3_int,(int)(score+.5));
	}
    }

  if(store_gene_ids)
    {
      set<int>::iterator it1;
      set<int>::iterator it2;
      int bottom=-1; //Don't let repeats get counted; once a gnum has been handled, ignore all the rest of the same gnum. Fortunately, the vector is sorted.
      bool hgt_match=true;

      for(int i=first_row; i<last_row; i++)
	{
	  for(int j=i+1; j<=last_row; j++)
	    {
1739
1740
1741
1742
	      int ig = gnums[i]; //ig is the NON-ORIGINAL genome ordinal.
	      int jg = gnums[j]; //jg is the next NON-ORIGNAL genome ordinal (equal or larger than ig)
	      int gig = gene_ids[i];
	      int gjg = gene_ids[j];
David Trudgian's avatar
David Trudgian committed
1743
1744
1745

	      if(ig!=jg) //If the matches are not from the same genome
		{
1746
		  double score = get_score(ig,jg,seq);
David Trudgian's avatar
David Trudgian committed
1747
1748
		  int index=get_bitterv_index(ig,jg);
		  
1749
		  if(score>=bitterv[index].get_msc() || hgt_match)
David Trudgian's avatar
David Trudgian committed
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
		    {
		      std::pair<int,int> hit1;
		      std::pair<int,int> hit2;
		      hit1=std::make_pair(ig,gig); //proteome 1 ids
		      hit2=std::make_pair(jg,gjg); //proteome 2 ids
		      if( (ig==0 && jg==1) || (ig==1 && jg==0) ) //An entry for the HGT map.
			{
			  if(hgt_map.find(std::pair<int,int>(ig,gig))!=hgt_map.end())
			    {
			      std::map<std::pair<int,int>,int>::iterator it;
			      it = hgt_map.find(std::pair<int,int>(ig,gig));
			      (it->second)++;
			    }
			  //entry doesn't exist.
			  else
			    {
			      std::pair<std::pair<int,int>,int> foo;
			      foo = std::make_pair(hit1,1); //The 1 stands for the starting score, i.e. 1 hit.
			      hgt_map.insert(foo);			      
			    }
			  //second entry exists in the hgt map
			  if(hgt_map.find(std::pair<int,int>(jg,gjg))!=hgt_map.end())
			    {
			      std::map<std::pair<int,int>,int>::iterator it;
			      it = hgt_map.find(std::pair<int,int>(jg,gjg));
			      (it->second)++;
			    }
			  else
			    {
			      std::pair<std::pair<int,int>,int> foo;
			      foo = std::make_pair(hit2,1);
			      hgt_map.insert(foo);
			    }
			} //end of ig==0 and jg==1
		      
		      //Not a match between the problems; a match with a ref.
		      else if(ig==0 || ig==1)
			{
			  if(con_map.find(std::pair<int,int>(ig,gig))!=con_map.end())
			    {
			      std::map<std::pair<int,int>,int>::iterator it;
			      it = con_map.find(std::pair<int,int>(ig,gig));
			      (it->second)++;
			    }
			  //entry doesn't exist.
			  else
			    {
			      std::pair<std::pair<int,int>,int> foo;
			      foo = std::make_pair(hit1,1); //The 1 stands for the starting score, i.e. 1 hit.
			      con_map.insert(foo);			      
			    }
			}
		      else if(jg==0 || jg==1)
			{
			  //second entry exists in the con map
			  if(con_map.find(std::pair<int,int>(jg,gjg))!=con_map.end())
			    {
			      std::map<std::pair<int,int>,int>::iterator it;
			      it = con_map.find(std::pair<int,int>(jg,gjg));
			      (it->second)++;
			    }
			  else
			    {
			      std::pair<std::pair<int,int>,int> foo;
			      foo = std::make_pair(hit2,1);
			      con_map.insert(foo);
			    } 
			} //end of jg==0 or jg==1
		    } //end of score > msc
		} //end of ig<jg and ig>bottom
	    } //end of first for
	} //end of second for
    } //end of if(msc)
}

/*******************************************
increment_bits_it2(...)
- When running just the HGT pair, HGT-genes removed.
*********************************************/
1829
void mctr::increment_bits_it2(int first_row, int last_row,string seq)//,ofstream& alalstream, ofstream& alstream)
David Trudgian's avatar
David Trudgian committed
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
{
  set<int> gnum_holder; //using a set to deal with repeats within the same organism (sets are non-redundant)
  bool store_gene_ids=false; //Used for storing gene numbers in bitterv because their score >= match_score_cutoff. Even when msc is true,

  bool full=false;
  set<int> gnum_diag_hit;
  pair<set<int>::iterator,bool> ret;

  //Store all ordinal IDs in a set.
  for(int i=first_row; i<=last_row && !full; i++)
    {
1841
      int gnum=gnums[i];
David Trudgian's avatar