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