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