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