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