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