tmerg.h 12.1 KB
Newer Older
David Trudgian's avatar
David Trudgian committed
1 2 3 4 5 6 7 8 9 10 11
#ifndef TMERG_H
#define TMERG_H

/*
tmerg:
Takes the list of kmers that were sorted in batches and merges them
into a final sorted list.
*/

#include <iostream>
#include <vector>
12
#include <algorithm>
David Trudgian's avatar
David Trudgian committed
13 14 15 16 17 18 19 20 21 22 23 24 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 161 162 163 164 165 166 167 168 169 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 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 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
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>

#include "file_info.h"
#include "amino_acids.h"
#include "util.h"

using namespace std;

const bool TMERG_DEBUG=true;

class tmerg
{
 private:
  string bfn;
  string path;
  int tag_length;
  int ref_set_size;
  string dir;           //specifies whether the tags are from TAGS or TAGSscr 
                        //and whether the merged tags go to TAGS_MERGED or TAGS_MERGEDscr
  ofstream logstream;

  amino_acids aas;
  util u;

  vector<file_info*> fiv;
  vector<string> files;
  void read_in_files();
  void open_tag_files();
  void read_in_tags(vector<string>& tags, char current_amino_acid, int& num_refs);
  void read_in_tags_um(vector<string>& tags, char current_amino_acid, int& num_refs);
  void write_out_tags(vector<string>& tags,int ordinal, int num_refs);
  void write_out_tags_um(vector<string>& tags,int ordinal, int num_refs);
  bool compare_tags(string s1, string s2);

 public:
  //constructors
  tmerg(string full_path_par, string dir_par);

  void merge();
};

tmerg::tmerg(string full_path_par, string dir_par)
{
  if(TMERG_DEBUG)
    {
      cout<<"tmerg::tmerg(string,string)"<<endl;
    }

  u.extract_paths(full_path_par,path,bfn);
  dir = dir_par;
  u.get_tl(path+"/"+bfn+"/"+bfn+"_info.txt", tag_length);
  u.get_rss(path+"/"+bfn+"/"+bfn+"_info.txt", ref_set_size);

  string logfile=path+"/"+bfn+"/logfile_tmerg.txt";
  u.open_ofile_app(logstream,logfile,"logfile");
  logstream<<"In constructor: tmerg::tmerg(string,string)"<<endl;
  logstream<<"path = "<<path<<endl;
  logstream<<"bfn = "<<bfn<<endl;
  logstream<<"tag_length = "<<tag_length<<endl;
  logstream<<"ref_set_size = "<<ref_set_size<<endl;
  logstream<<"dir_par = "<<dir_par<<endl;

  u.rm_mkdir(path+"/"+bfn, "MERGED_TAGS"+dir);
  u.rm_mkdir(path+"/"+bfn, "REF_MERGED_TAGS"+dir);
}

void tmerg::merge()
{
  if(TMERG_DEBUG)
    {
      cout<<"tmerg::merge() : Reading in files..."<<endl;
    }
  read_in_files();
  if(TMERG_DEBUG)
    {
      cout<<"tmerg:merge() : Opening new merged tag files..."<<endl;
    }
  open_tag_files();
}

void tmerg::read_in_files()
{
  struct dirent *de=NULL;
  DIR *d=NULL;
  string tags_directory_full_path = path + "/"+bfn+"/TAGS"+dir+"/";
  d=opendir(tags_directory_full_path.c_str());
  if(d==NULL)
    {
      cout<<"Couldn't open directory "<<tags_directory_full_path<<endl;
      exit(1);
    }
  else
    {
      cout<<"Successfully opened directory "<<tags_directory_full_path<<endl;
      logstream<<"Successfully opened directory "<<tags_directory_full_path<<endl;
    }

  while(de=readdir(d))
    {
      std::ostringstream sin;
      sin<<(de->d_name);
      std::string fn=sin.str();

      if( ((string)de->d_name).length() > 5 && ((string)de->d_name).substr(0,4)=="TAGS")
	{
	  files.push_back( (string)(de->d_name));
	}
    }

  if(TMERG_DEBUG)
    {
      cout<<"tmerg::read_in_files() : Size of files = "<<files.size()<<endl;
    }
  logstream<<"tmerg::read_in_files() : Size of files = "<<files.size()<<endl;

  closedir(d);
}

void tmerg::open_tag_files()
{
  if(TMERG_DEBUG)
    {
      cout<<"tmerg::open_tag_files()"<<endl;
    }

  //The files contain sorted lists of kmers starting from A through Y.
  //Therefore, they all, and not a subset of them,  have to be opened and merged
  //from the start.
  for(int i=0; i<files.size(); i++)
    {
      file_info* fitemp = new file_info();
      fitemp->open(path+"/"+bfn+"/TAGS"+dir+"/"+files.at(i));
      if(TMERG_DEBUG)
	{
	  cout<<"i="<<i<<" total_tags = "<<fitemp->total_tags<<endl; 
	}
      fiv.push_back(fitemp);
    }

  if(TMERG_DEBUG)
    {
      cout<<"Size of fiv = "<<fiv.size()<<endl;
    }

  vector<string> tags;
  for(int i=0; i<20; i++)
    {
      char caa = aas.get_aa(i);
      int num_refs=0; //Total number of kmers from the reference set.
                      //Necessary because this set is written out not only
                      //with the main set, but also to its own merged files
                      //for use during the phage filtering.
      //      read_in_tags_um(tags, caa, num_refs);
      read_in_tags(tags, caa, num_refs);

      if(TMERG_DEBUG)
	{
	  cout<<"i="<<i<<" tags.size() = "<<tags.size()<<endl;
	}

      if(tags.size()>0)
	{
	  if(TMERG_DEBUG)
	    {
	      cout<<"tags.at(0) = "<<tags.at(0)<<endl;
	      cout<<"tags last element = "<<tags.at(tags.size()-1)<<endl;
	    }
	}

      //      write_out_tags_um(tags,i,num_refs);
      write_out_tags(tags,i,num_refs);
    }

  for(int i=0; i<fiv.size(); i++)
    {
      delete fiv.at(i);
      fiv.erase(fiv.begin());
    }
}

void tmerg::read_in_tags(vector<string>& tags, char current_amino_acid, int& num_refs)
{
  num_refs=0; //Counting the number of tags belonging to the reference set.
              //Need this number because a set of merged tags for this set is also written out
              //and the total is always included at the top fo these merged files.

  for(int i=0; i<fiv.size(); i++)
    {
      cout<<"tmerg::read_in_tags(vector<string>, char, int&) : current_amino_acid = "<<current_amino_acid<<endl;
      if(TMERG_DEBUG)
	{
	  cout<<"i = "<<i<<" out of fiv.size() = "<<fiv.size()<<endl;
	  cout<<"fiv.at(i).strings_read_in = "<<fiv.at(i)->strings_read_in<<endl;
	  cout<<"total_tags = "<<fiv.at(i)->total_tags<<endl;
	  cout<<"s[0]="<<fiv.at(i)->s[0]<<endl;
	}

      int counter=0;
      while( (fiv.at(i)->strings_read_in<fiv.at(i)->total_tags) && fiv.at(i)->s[0]==current_amino_acid)
	{
	  string ctag = fiv.at(i)->get_current_line();
	  tags.push_back(ctag);
	  istringstream iss(ctag);
	  string e;
	  int gnum;
	  iss>>e>>gnum;
	  if(gnum<ref_set_size)
	    {
	      num_refs++;
	    }

	  fiv.at(i)->read_line();
	  counter++;
	  if(counter%10000==0)
	    {
	      cout<<"counter = "<<counter<<" ctag = "<<ctag<<endl;
	    }
	}
      cout<<"tmerg::read_in_tags(vector<string>, char, int&) : ";
      cout<<"Merged "<<i+1<<" out of "<<fiv.size()<<". tags.size() = "<<tags.size()<<endl;
      if(TMERG_DEBUG)
	{
	  cout<<"tags.at(0) = "<<tags.at(0)<<endl<<"tags.at(final index) = "<<tags.at(tags.size()-1)<<endl;
	}
    }
}

//Using merge rather than relying on final quicksort.
void tmerg::read_in_tags_um(vector<string>& tags, char current_amino_acid, int& num_refs)
{
  num_refs=0; //Counting the number of tags belonging to the reference set.
              //Need this number because a set of merged tags for this set is also written out
              //and the total is always included at the top fo these merged files.

  for(int i=0; i<fiv.size(); i++)
    {
      cout<<"tmerg::read_in_tags_um(vector<string>, char, int&) : current_amino_acid = "<<current_amino_acid<<endl;
      if(TMERG_DEBUG)
	{
	  cout<<"i = "<<i<<" out of fiv.size() = "<<fiv.size()<<endl;
	  cout<<"fiv.at(i).strings_read_in = "<<fiv.at(i)->strings_read_in<<endl;
	  cout<<"total_tags = "<<fiv.at(i)->total_tags<<endl;
	  cout<<"s[0]="<<fiv.at(i)->s[0]<<endl;
	}

      int counter=0;
      if(tags.size()==0) //if no tags have been pushed back yet, just push back the first sorted chunk.
	{
	  while( (fiv.at(i)->strings_read_in<fiv.at(i)->total_tags) && fiv.at(i)->s[0]==current_amino_acid)
	    {
	      string ctag = fiv.at(i)->get_current_line();
	      tags.push_back(ctag);
	      istringstream iss(ctag);
	      string e;
	      int gnum;
	      iss>>e>>gnum;
	      if(gnum<ref_set_size)
		{
		  num_refs++;
		}
	      
	      fiv.at(i)->read_line();
	      counter++;
	      if(counter%10000==0)
		{
		  cout<<"counter = "<<counter<<" ctag = "<<ctag<<endl;
		}
	    }

	  cout<<"tmerg::read_in_tags_um(vector<string>, char, int&) : ";
	  cout<<"Merged "<<i+1<<" out of "<<fiv.size()<<". tags.size() = "<<tags.size()<<endl;
	  if(TMERG_DEBUG)
	    {
	      cout<<"tags.at(0) = "<<tags.at(0)<<endl<<"tags.at(final index) = "<<tags.at(tags.size()-1)<<endl;
	    }
	}
      
      else //if at least one batch of tags has already been added.
	{
	  int tindex=0;
	  while( (fiv.at(i)->strings_read_in<fiv.at(i)->total_tags) && fiv.at(i)->s[0]==current_amino_acid)
	    {
	      string ctag = fiv.at(i)->get_current_line();
	      while(compare_tags(ctag,tags.at(tindex)) && tindex<tags.size() )
		{
		  tindex++;
		}
	      
	      tags.insert(tags.begin()+tindex,ctag);
	      istringstream iss(ctag);
	      string e;
	      int gnum;
	      iss>>e>>gnum;
	      if(gnum<ref_set_size)
		{
		  num_refs++;
		}
	      
	      fiv.at(i)->read_line();
	      counter++;
	      if(counter%1000==0)
		{
		  cout<<"counter = "<<counter<<" ctag = "<<ctag<<endl;
		}
	    }
	  cout<<"tmerg::read_in_tags_um(vector<string>, char, int&) : ";
	  cout<<"Merged "<<i+1<<" out of "<<fiv.size()<<". tags.size() = "<<tags.size()<<endl;
	  if(TMERG_DEBUG)
	    {
	      cout<<"tags.at(0) = "<<tags.at(0)<<endl<<"tags.at(final index) = "<<tags.at(tags.size()-1)<<endl;
	    }
	}
    }
}


void tmerg::write_out_tags(vector<string>& tags,int ordinal, int num_refs)
{
  if(TMERG_DEBUG)
    {
      cout<<"tmerg::write_out_tags(vector<string>&, int, int) : tags.size() = "<<tags.size()<<endl;
    }

  if(tags.size()<=1)
    {
      cout<<"tmerg::write_out_tags(vector<string>&, int, int) : invalid tags.size() = "<<tags.size()<<endl;
      exit(1);
    }

  if(tags.size()>0)
    {
      char buf[30];
      sprintf(buf,"%d",ordinal);
      string buf2=buf;
      string out_file_name = "TAGS"+buf2+".txt";
      FILE* oFile;
      FILE* oFile_ref;
      oFile = fopen( (path + "/"+bfn+"/MERGED_TAGS"+dir+"/"+out_file_name).c_str(), "w");
      if(num_refs>0)
	{
	  oFile_ref = fopen( (path + "/"+bfn+"/REF_MERGED_TAGS"+dir+"/"+out_file_name).c_str(), "w");
	}

362
      std::sort(tags.begin(), tags.end());
David Trudgian's avatar
David Trudgian committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 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 471 472 473 474 475 476
      if(TMERG_DEBUG)
	{
	  cout<<"tmerg::write_out_tags(vector<string>&, int, int) : Successfully completed quicksort..."<<endl;
	}

      fprintf(oFile,"%d\n",(int)tags.size());
      if(num_refs>0)
	{
	  fprintf(oFile_ref,"%d\n",(int)num_refs);
	}

      for(int i=0; (unsigned)i<tags.size(); i++)
	{
	  fprintf(oFile,"%s",tags.at(i).c_str());
	  istringstream iss(tags.at(i));
	  string e;
	  int gnum;
	  iss>>e>>gnum;
	  if(num_refs>0)
	    {
	      if(gnum<ref_set_size && dir=="")
		{
		  fprintf(oFile_ref,"%s",tags.at(i).c_str());
		}
	    }
	}
      fclose(oFile);

      if(num_refs>0)
	{
	  fclose(oFile_ref);
	}

      tags.clear();	        
    }
  else
    {
      cout<<"tmerg::write_out_tags(vector<string>&, int, int) : tags.size() = "<<tags.size()<<". Successfully wrote tags to file."<<endl;
    }
}

void tmerg::write_out_tags_um(vector<string>& tags,int ordinal, int num_refs)
{
  if(TMERG_DEBUG)
    {
      cout<<"tmerg::write_out_tags_um(vector<string>&, int, int) : tags.size() = "<<tags.size()<<endl;
    }

  if(tags.size()<=1)
    {
      cout<<"tmerg::write_out_tags_um(vector<string>&, int, int) : invalid tags.size() = "<<tags.size()<<endl;
      exit(1);
    }

  if(tags.size()>0)
    {
      char buf[30];
      sprintf(buf,"%d",ordinal);
      string buf2=buf;
      string out_file_name = "TAGS"+buf2+".txt";
      FILE* oFile;
      FILE* oFile_ref;
      oFile = fopen( (path + "/"+bfn+"/MERGED_TAGS"+dir+"/"+out_file_name).c_str(), "w");
      if(num_refs>0)
	{
	  oFile_ref = fopen( (path + "/"+bfn+"/REF_MERGED_TAGS"+dir+"/"+out_file_name).c_str(), "w");
	}

      fprintf(oFile,"%d\n",(int)tags.size());
      if(num_refs>0)
	{
	  fprintf(oFile_ref,"%d\n",(int)num_refs);
	}

      for(int i=0; (unsigned)i<tags.size(); i++)
	{
	  fprintf(oFile,"%s",tags.at(i).c_str());
	  istringstream iss(tags.at(i));
	  string e;
	  int gnum;
	  iss>>e>>gnum;
	  if(num_refs>0)
	    {
	      if(gnum<ref_set_size && dir=="")
		{
		  fprintf(oFile_ref,"%s",tags.at(i).c_str());
		}
	    }
	}
      fclose(oFile);

      if(num_refs>0)
	{
	  fclose(oFile_ref);
	}

      tags.clear();	        
    }
  else
    {
      cout<<"tmerg::write_out_tags_um(vector<string>&, int, int): tags.size = "<<tags.size()<<". Code  = 0. Successfully wrote tags to file."<<endl;
    }
}


bool tmerg::compare_tags(string s1, string s2)
{
  if(s1.compare(s2)<0)
    return true;
  else
    return false;
}

#endif