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