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