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