]> git.donarmstrong.com Git - mothur.git/blob - chimeracheckcommand.cpp
added pipeline commands which involved change to command factory and command class...
[mothur.git] / chimeracheckcommand.cpp
1 /*
2  *  chimeracheckcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 3/31/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chimeracheckcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> ChimeraCheckCommand::getValidParameters(){       
14         try {
15                 string AlignArray[] =  {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
16                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "ChimeraCheckCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 vector<string> ChimeraCheckCommand::getRequiredParameters(){    
26         try {
27                 string AlignArray[] =  {"template","fasta"};
28                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29                 return myArray;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "ChimeraCheckCommand", "getRequiredParameters");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> ChimeraCheckCommand::getRequiredFiles(){ 
38         try {
39                 vector<string> myArray;
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "ChimeraCheckCommand", "getRequiredFiles");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 ChimeraCheckCommand::ChimeraCheckCommand(){     
49         try {
50                 vector<string> tempOutNames;
51                 outputTypes["chimera"] = tempOutNames;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
55                 exit(1);
56         }
57 }
58 //***************************************************************************************************************
59 ChimeraCheckCommand::ChimeraCheckCommand(string option)  {
60         try {
61                 abort = false;
62                 
63                 //allow user to run help
64                 if(option == "help") { help(); abort = true; }
65                 
66                 else {
67                         //valid paramters for this command
68                         string Array[] =  {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
69                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
70                         
71                         OptionParser parser(option);
72                         map<string,string> parameters = parser.getParameters();
73                         
74                         ValidParameters validParameter("chimera.check");
75                         map<string,string>::iterator it;
76                         
77                         //check to make sure all parameters are valid for command
78                         for (it = parameters.begin(); it != parameters.end(); it++) { 
79                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
80                         }
81                         
82                         vector<string> tempOutNames;
83                         outputTypes["chimera"] = tempOutNames;
84                 
85                         //if the user changes the input directory command factory will send this info to us in the output parameter 
86                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
87                         if (inputDir == "not found"){   inputDir = "";          }
88                         else {
89                                 it = parameters.find("template");
90                                 //user has given a template file
91                                 if(it != parameters.end()){ 
92                                         string path = m->hasPath(it->second);
93                                         //if the user has not given a path then, add inputdir. else leave path alone.
94                                         if (path == "") {       parameters["template"] = inputDir + it->second;         }
95                                 }
96                         }
97                         
98                         //check for required parameters
99                         fastafile = validParameter.validFile(parameters, "fasta", false);
100                         if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true;  }
101                         else { 
102                                 m->splitAtDash(fastafile, fastaFileNames);
103                                 
104                                 //go through files and make sure they are good, if not, then disregard them
105                                 for (int i = 0; i < fastaFileNames.size(); i++) {
106                                         if (inputDir != "") {
107                                                 string path = m->hasPath(fastaFileNames[i]);
108                                                 //if the user has not given a path then, add inputdir. else leave path alone.
109                                                 if (path == "") {       fastaFileNames[i] = inputDir + fastaFileNames[i];               }
110                                         }
111         
112                                         int ableToOpen;
113                                         ifstream in;
114                                         
115                                         ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
116                                 
117                                         //if you can't open it, try default location
118                                         if (ableToOpen == 1) {
119                                                 if (m->getDefaultPath() != "") { //default path is set
120                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
121                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
122                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
123                                                         fastaFileNames[i] = tryPath;
124                                                 }
125                                         }
126                                         
127                                         //if you can't open it, try default location
128                                         if (ableToOpen == 1) {
129                                                 if (m->getOutputDir() != "") { //default path is set
130                                                         string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
131                                                         m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
132                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
133                                                         fastaFileNames[i] = tryPath;
134                                                 }
135                                         }
136                                         
137                                         in.close();
138                                         
139                                         if (ableToOpen == 1) { 
140                                                 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine(); 
141                                                 //erase from file list
142                                                 fastaFileNames.erase(fastaFileNames.begin()+i);
143                                                 i--;
144                                         }
145                                 }
146                                 
147                                 //make sure there is at least one valid file left
148                                 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
149                         }
150                         
151                         //if the user changes the output directory command factory will send this info to us in the output parameter 
152                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = ""; }
153
154                         templatefile = validParameter.validFile(parameters, "template", true);
155                         if (templatefile == "not open") { abort = true; }
156                         else if (templatefile == "not found") { templatefile = "";  m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true;  }    
157                         
158                         namefile = validParameter.validFile(parameters, "name", false);
159                         if (namefile == "not found") { namefile = ""; }
160                         else { 
161                                 m->splitAtDash(namefile, nameFileNames);
162                                 
163                                 //go through files and make sure they are good, if not, then disregard them
164                                 for (int i = 0; i < nameFileNames.size(); i++) {
165                                         if (inputDir != "") {
166                                                 string path = m->hasPath(nameFileNames[i]);
167                                                 //if the user has not given a path then, add inputdir. else leave path alone.
168                                                 if (path == "") {       nameFileNames[i] = inputDir + nameFileNames[i];         }
169                                         }
170         
171                                         int ableToOpen;
172                                         ifstream in;
173                                         
174                                         ableToOpen = m->openInputFile(nameFileNames[i], in, "noerror");
175                                 
176                                         //if you can't open it, try default location
177                                         if (ableToOpen == 1) {
178                                                 if (m->getDefaultPath() != "") { //default path is set
179                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(nameFileNames[i]);
180                                                         m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
181                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
182                                                         nameFileNames[i] = tryPath;
183                                                 }
184                                         }
185                                         
186                                         //if you can't open it, try default location
187                                         if (ableToOpen == 1) {
188                                                 if (m->getOutputDir() != "") { //default path is set
189                                                         string tryPath = m->getOutputDir() + m->getSimpleName(nameFileNames[i]);
190                                                         m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
191                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
192                                                         nameFileNames[i] = tryPath;
193                                                 }
194                                         }
195                                         
196                                         in.close();
197                                         
198                                         if (ableToOpen == 1) { 
199                                                 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine(); 
200                                                 //erase from file list
201                                                 nameFileNames.erase(nameFileNames.begin()+i);
202                                                 i--;
203                                         }
204                                         
205                                 }
206                                 
207                                 //make sure there is at least one valid file left
208                                 if (nameFileNames.size() != 0) {
209                                         if (nameFileNames.size() != fastaFileNames.size()) { 
210                                                  m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine(); 
211                                                  abort = true;
212                                         }
213                                 }
214                         }
215
216                         string temp = validParameter.validFile(parameters, "processors", false);                if (temp == "not found") { temp = "1"; }
217                         convert(temp, processors);
218                         
219                         temp = validParameter.validFile(parameters, "ksize", false);                    if (temp == "not found") { temp = "7"; }
220                         convert(temp, ksize);
221                         
222                         temp = validParameter.validFile(parameters, "svg", false);                              if (temp == "not found") { temp = "F"; }
223                         svg = m->isTrue(temp);
224                         if (nameFileNames.size() != 0) { svg = true; }
225                         
226                         temp = validParameter.validFile(parameters, "increment", false);                if (temp == "not found") { temp = "10"; }
227                         convert(temp, increment);                       
228                 }
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
232                 exit(1);
233         }
234 }
235 //**********************************************************************************************************************
236
237 void ChimeraCheckCommand::help(){
238         try {
239         
240                 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
241                 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
242                 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
243                 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
244                 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
245                 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
246                 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
247                 #ifdef USE_MPI
248                 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
249                 #endif
250                 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
251                 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
252                 m->mothurOut("The svg parameter allows you to specify whether or not you would like a svg file outputted for each query sequence, default is False.\n");
253                 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
254                 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
255                 m->mothurOut("The chimera.check command should be in the following format: \n");
256                 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
257                 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
258                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
259         }
260         catch(exception& e) {
261                 m->errorOut(e, "ChimeraCheckCommand", "help");
262                 exit(1);
263         }
264 }
265
266 //***************************************************************************************************************
267
268 ChimeraCheckCommand::~ChimeraCheckCommand(){    /*      do nothing      */      }
269
270 //***************************************************************************************************************
271
272 int ChimeraCheckCommand::execute(){
273         try{
274                 
275                 if (abort == true) { return 0; }
276                 
277                 for (int i = 0; i < fastaFileNames.size(); i++) {
278                                 
279                         m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
280                         
281                         int start = time(NULL); 
282                         
283                         string thisNameFile = "";
284                         if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
285                         
286                         chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);                 
287
288                         if (m->control_pressed) { delete chimera;       return 0;       }
289                         
290                         if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[i]);  }//if user entered a file with a path then preserve it
291                         string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[i]))  + "chimeracheck.chimeras";
292                         outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
293                         
294                 #ifdef USE_MPI
295                 
296                                 int pid, end, numSeqsPerProcessor; 
297                                 int tag = 2001;
298                                 vector<unsigned long int> MPIPos;
299                                 
300                                 MPI_Status status; 
301                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
302                                 MPI_Comm_size(MPI_COMM_WORLD, &processors); 
303
304                                 MPI_File inMPI;
305                                 MPI_File outMPI;
306                                                         
307                                 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
308                                 int inMode=MPI_MODE_RDONLY; 
309                                                         
310                                 char outFilename[1024];
311                                 strcpy(outFilename, outputFileName.c_str());
312                         
313                                 char inFileName[1024];
314                                 strcpy(inFileName, fastaFileNames[i].c_str());
315
316                                 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
317                                 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
318                                 
319                                 if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);  for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0;  }
320                                 
321                                 if (pid == 0) { //you are the root process 
322                                         MPIPos = m->setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
323                                         
324                                         //send file positions to all processes
325                                         for(int j = 1; j < processors; j++) { 
326                                                 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
327                                                 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
328                                         }       
329                                         
330                                         //figure out how many sequences you have to align
331                                         numSeqsPerProcessor = numSeqs / processors;
332                                         int startIndex =  pid * numSeqsPerProcessor;
333                                         if(pid == (processors - 1)){    numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor;      }
334                                         
335                                 
336                                         //align your part
337                                         driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
338                                         
339                                         if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);  for (int j = 0; j < outputNames.size(); j++) {    remove(outputNames[j].c_str()); }   outputTypes.clear(); delete chimera; return 0;  }
340                                         
341                                         //wait on chidren
342                                         for(int j = 1; j < processors; j++) { 
343                                                 char buf[5];
344                                                 MPI_Recv(buf, 5, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status); 
345                                         }
346                                 }else{ //you are a child process
347                                         MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
348                                         MPIPos.resize(numSeqs+1);
349                                         MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
350                                         
351                                         //figure out how many sequences you have to align
352                                         numSeqsPerProcessor = numSeqs / processors;
353                                         int startIndex =  pid * numSeqsPerProcessor;
354                                         if(pid == (processors - 1)){    numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor;      }
355                                         
356                                         //align your part
357                                         driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
358                                         
359                                         if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI);   for (int j = 0; j < outputNames.size(); j++) {   remove(outputNames[j].c_str()); }  outputTypes.clear(); delete chimera; return 0;  }
360                                         
361                                         //tell parent you are done.
362                                         char buf[5];
363                                         strcpy(buf, "done"); 
364                                         MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
365                                 }
366                                 
367                                 //close files 
368                                 MPI_File_close(&inMPI);
369                                 MPI_File_close(&outMPI);
370                                 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
371                 #else
372                         
373                         vector<unsigned long int> positions = m->divideFile(fastaFileNames[i], processors);
374                                 
375                         for (int s = 0; s < (positions.size()-1); s++) {
376                                 lines.push_back(new linePair(positions[s], positions[(s+1)]));
377                         }       
378                         
379                         //break up file
380                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
381                                 if(processors == 1){
382                                         numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
383                                         
384                                         if (m->control_pressed) { for (int j = 0; j < outputNames.size(); j++) {        remove(outputNames[j].c_str()); } for (int j = 0; j < lines.size(); j++) {  delete lines[j];  } outputTypes.clear();  lines.clear(); delete chimera; return 0; }
385                                                                         
386                                 }else{
387                                         processIDS.resize(0);
388                                         
389                                         numSeqs = createProcesses(outputFileName, fastaFileNames[i]); 
390                                 
391                                         rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
392                                                 
393                                         //append output files
394                                         for(int j=1;j<processors;j++){
395                                                 m->appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
396                                                 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
397                                         }
398                                         
399                                         if (m->control_pressed) { 
400                                                 for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } outputTypes.clear();
401                                                 for (int j = 0; j < lines.size(); j++) {  delete lines[j];  }  lines.clear();
402                                                 delete chimera;
403                                                 return 0;
404                                         }
405                                 }
406
407                         #else
408                                 numSeqs = driver(lines[0], outputFileName, fastaFileNames[i]);
409                                 
410                                 if (m->control_pressed) { for (int j = 0; j < lines.size(); j++) {  delete lines[j];  }  lines.clear(); for (int j = 0; j < outputNames.size(); j++) {  remove(outputNames[j].c_str()); } outputTypes.clear(); delete chimera; return 0; }
411                         #endif
412                 #endif          
413                         delete chimera;
414                         for (int j = 0; j < lines.size(); j++) {  delete lines[j];  }  lines.clear();
415                         
416                         m->mothurOutEndLine(); m->mothurOut("This method does not determine if a sequence is chimeric, but allows you to make that determination based on the IS values."); m->mothurOutEndLine(); 
417                         m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
418
419                 }
420                 
421                 m->mothurOutEndLine();
422                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
423                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
424                 m->mothurOutEndLine();
425         
426                 return 0;
427                 
428         }
429         catch(exception& e) {
430                 m->errorOut(e, "ChimeraCheckCommand", "execute");
431                 exit(1);
432         }
433 }
434 //**********************************************************************************************************************
435
436 int ChimeraCheckCommand::driver(linePair* filePos, string outputFName, string filename){
437         try {
438                 ofstream out;
439                 m->openOutputFile(outputFName, out);
440                 
441                 ofstream out2;
442                 
443                 ifstream inFASTA;
444                 m->openInputFile(filename, inFASTA);
445
446                 inFASTA.seekg(filePos->start);
447
448                 bool done = false;
449                 int count = 0;
450         
451                 while (!done) {
452
453                         if (m->control_pressed) {       return 1;       }
454                 
455                         Sequence* candidateSeq = new Sequence(inFASTA);  m->gobble(inFASTA);
456                                 
457                         if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
458                                 //find chimeras
459                                 chimera->getChimeras(candidateSeq);
460                                 
461                                 if (m->control_pressed) {       delete candidateSeq; return 1;  }
462         
463                                 //print results
464                                 chimera->print(out, out2);
465                         }
466                         delete candidateSeq;
467                         
468                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
469                                 unsigned long int pos = inFASTA.tellg();
470                                 if ((pos == -1) || (pos >= filePos->end)) { break; }
471                         #else
472                                 if (inFASTA.eof()) { break; }
473                         #endif
474                         
475                         //report progress
476                         if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine();         }
477                 }
478                 //report progress
479                 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine();         }
480                 
481                 out.close();
482                 inFASTA.close();
483                                 
484                 return count;
485         }
486         catch(exception& e) {
487                 m->errorOut(e, "ChimeraCheckCommand", "driver");
488                 exit(1);
489         }
490 }
491 //**********************************************************************************************************************
492 #ifdef USE_MPI
493 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos){
494         try {
495                 MPI_File outAccMPI;
496                 MPI_Status status; 
497                 int pid;
498                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
499                 
500                 for(int i=0;i<num;i++){
501                         
502                         if (m->control_pressed) { return 0; }
503                         
504                         //read next sequence
505                         int length = MPIPos[start+i+1] - MPIPos[start+i];
506         
507                         char* buf4 = new char[length];
508                         MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
509                         
510                         string tempBuf = buf4;
511                         if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length);  }
512                         istringstream iss (tempBuf,istringstream::in);
513                         delete buf4;
514
515                         Sequence* candidateSeq = new Sequence(iss);  m->gobble(iss);
516                                 
517                         if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
518                                 //find chimeras
519                                 chimera->getChimeras(candidateSeq);
520                                         
521                                 //print results
522                                 chimera->print(outMPI, outAccMPI);
523                         }
524                         delete candidateSeq;
525                         
526                         //report progress
527                         if((i+1) % 100 == 0){  cout << "Processing sequence: " << (i+1) << endl;        m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n");          }
528                 }
529                 //report progress
530                 if(num % 100 != 0){             cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n");  }
531                 
532                 return 0;
533         }
534         catch(exception& e) {
535                 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
536                 exit(1);
537         }
538 }
539 #endif
540
541 /**************************************************************************************************/
542
543 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
544         try {
545 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
546                 int process = 0;
547                 int num = 0;
548                 
549                 //loop through and create all the processes you want
550                 while (process != processors) {
551                         int pid = fork();
552                         
553                         if (pid > 0) {
554                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
555                                 process++;
556                         }else if (pid == 0){
557                                 num = driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
558                                 
559                                 //pass numSeqs to parent
560                                 ofstream out;
561                                 string tempFile = outputFileName + toString(getpid()) + ".num.temp";
562                                 m->openOutputFile(tempFile, out);
563                                 out << num << endl;
564                                 out.close();
565                                 
566                                 exit(0);
567                         }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
568                 }
569                 
570                 //force parent to wait until all the processes are done
571                 for (int i=0;i<processors;i++) { 
572                         int temp = processIDS[i];
573                         wait(&temp);
574                 }
575                 
576                 for (int i = 0; i < processIDS.size(); i++) {
577                         ifstream in;
578                         string tempFile =  outputFileName + toString(processIDS[i]) + ".num.temp";
579                         m->openInputFile(tempFile, in);
580                         if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
581                         in.close(); remove(tempFile.c_str());
582                 }
583                 
584                 return num;
585 #endif          
586         }
587         catch(exception& e) {
588                 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
589                 exit(1);
590         }
591 }
592 /**************************************************************************************************/
593
594