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