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