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