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