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