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