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