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