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