]> git.donarmstrong.com Git - mothur.git/blob - seqsummarycommand.cpp
added namefile to summary.seqs
[mothur.git] / seqsummarycommand.cpp
1 /*
2  *  seqcoordcommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 5/30/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "seqsummarycommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> SeqSummaryCommand::getValidParameters(){ 
15         try {
16                 string Array[] =  {"fasta","name","processors","outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "SeqSummaryCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 SeqSummaryCommand::SeqSummaryCommand(){ 
27         try {
28                 abort = true; calledHelp = true; 
29                 vector<string> tempOutNames;
30                 outputTypes["summary"] = tempOutNames;
31         }
32         catch(exception& e) {
33                 m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
34                 exit(1);
35         }
36 }
37 //**********************************************************************************************************************
38 vector<string> SeqSummaryCommand::getRequiredParameters(){      
39         try {
40                 string Array[] =  {"fasta"};
41                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42                 return myArray;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "SeqSummaryCommand", "getRequiredParameters");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 vector<string> SeqSummaryCommand::getRequiredFiles(){   
51         try {
52                 vector<string> myArray;
53                 return myArray;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "SeqSummaryCommand", "getRequiredFiles");
57                 exit(1);
58         }
59 }
60 //***************************************************************************************************************
61
62 SeqSummaryCommand::SeqSummaryCommand(string option)  {
63         try {
64                 abort = false; calledHelp = false;   
65                 
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 
69                 else {
70                         //valid paramters for this command
71                         string Array[] =  {"fasta","name","processors","outputdir","inputdir"};
72                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73                         
74                         OptionParser parser(option);
75                         map<string,string> parameters = parser.getParameters();
76                         
77                         ValidParameters validParameter("summary.seqs");
78                         map<string,string>::iterator it;
79                         
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //if the user changes the input directory command factory will send this info to us in the output parameter 
86                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
87                         if (inputDir == "not found"){   inputDir = "";          }
88                         else {
89                                 string path;
90                                 it = parameters.find("fasta");
91                                 //user has given a template file
92                                 if(it != parameters.end()){ 
93                                         path = m->hasPath(it->second);
94                                         //if the user has not given a path then, add inputdir. else leave path alone.
95                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
96                                 }
97                                 
98                                 it = parameters.find("name");
99                                 //user has given a template file
100                                 if(it != parameters.end()){ 
101                                         path = m->hasPath(it->second);
102                                         //if the user has not given a path then, add inputdir. else leave path alone.
103                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
104                                 }
105                         }
106                         
107                         //initialize outputTypes
108                         vector<string> tempOutNames;
109                         outputTypes["summary"] = tempOutNames;
110                         
111                         //check for required parameters
112                         fastafile = validParameter.validFile(parameters, "fasta", true);
113                         if (fastafile == "not open") { abort = true; }
114                         else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the summary.seqs command."); m->mothurOutEndLine(); abort = true;  }       
115                         
116                         namefile = validParameter.validFile(parameters, "name", true);
117                         if (namefile == "not open") { namefile = ""; abort = true; }
118                         else if (namefile == "not found") { namefile = "";  }   
119                         
120                         //if the user changes the output directory command factory will send this info to us in the output parameter 
121                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
122                                 outputDir = ""; 
123                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
124                         }
125                         
126                         string temp = validParameter.validFile(parameters, "processors", false);        if (temp == "not found"){       temp = "1";                             }
127                         convert(temp, processors); 
128
129
130                 }
131         }
132         catch(exception& e) {
133                 m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
134                 exit(1);
135         }
136 }
137 //**********************************************************************************************************************
138
139 void SeqSummaryCommand::help(){
140         try {
141                 m->mothurOut("The summary.seqs command reads a fastafile and summarizes the sequences.\n");
142                 m->mothurOut("The summary.seqs command parameters are fasta, name and processors, fasta is required.\n");
143                 m->mothurOut("The name parameter allows you to enter a name file associated with your fasta file. \n");
144                 m->mothurOut("The summary.seqs command should be in the following format: \n");
145                 m->mothurOut("summary.seqs(fasta=yourFastaFile, processors=2) \n");
146                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
147         }
148         catch(exception& e) {
149                 m->errorOut(e, "SeqSummaryCommand", "help");
150                 exit(1);
151         }
152 }
153
154 //***************************************************************************************************************
155
156 SeqSummaryCommand::~SeqSummaryCommand(){        /*      do nothing      */      }
157
158 //***************************************************************************************************************
159
160 int SeqSummaryCommand::execute(){
161         try{
162                 
163                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
164                 
165                 string summaryFile = outputDir + m->getSimpleName(fastafile) + ".summary";
166                                 
167                 int numSeqs = 0;
168                 
169                 vector<int> startPosition;
170                 vector<int> endPosition;
171                 vector<int> seqLength;
172                 vector<int> ambigBases;
173                 vector<int> longHomoPolymer;
174                 
175                 if (namefile != "") { readNames(); }
176                 
177                 if (m->control_pressed) { return 0; }
178                         
179 #ifdef USE_MPI  
180                                 int pid, numSeqsPerProcessor; 
181                                 int tag = 2001;
182                                 int startTag = 1; int endTag = 2; int lengthTag = 3; int baseTag = 4; int lhomoTag = 5;
183                                 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
184                                 vector<unsigned long int> MPIPos;
185                                 
186                                 MPI_Status status; 
187                                 MPI_Status statusOut;
188                                 MPI_File inMPI; 
189                                 MPI_File outMPI; 
190                                 MPI_Comm_size(MPI_COMM_WORLD, &processors);
191                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
192                                                         
193                                 char tempFileName[1024];
194                                 strcpy(tempFileName, fastafile.c_str());
195                                 
196                                 char sumFileName[1024];
197                                 strcpy(sumFileName, summaryFile.c_str());
198                 
199                                 MPI_File_open(MPI_COMM_WORLD, tempFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
200                                 MPI_File_open(MPI_COMM_WORLD, sumFileName, outMode, MPI_INFO_NULL, &outMPI);
201                                 
202                                 if (m->control_pressed) {  MPI_File_close(&inMPI);  MPI_File_close(&outMPI); return 0;  }
203                                 
204                                 if (pid == 0) { //you are the root process
205                                                 //print header
206                                                 string outputString = "seqname\tstart\tend\tnbases\tambigs\tpolymer\tnumSeqs\n";        
207                                                 int length = outputString.length();
208                                                 char* buf2 = new char[length];
209                                                 memcpy(buf2, outputString.c_str(), length);
210                                         
211                                                 MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &statusOut);
212                                                 delete buf2;
213                                                 
214                                                 MPIPos = m->setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs
215                                         
216                                                 for(int i = 1; i < processors; i++) { 
217                                                         MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
218                                                         MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
219                                                 }
220                                                 
221                                                 //figure out how many sequences you have to do
222                                                 numSeqsPerProcessor = numSeqs / processors;
223                                                 int startIndex =  pid * numSeqsPerProcessor;
224                                                 if(pid == (processors - 1)){    numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor;      }
225                                                 
226                                                 //do your part
227                                                 MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
228                                                 
229                                 }else { //i am the child process
230                         
231                                         MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
232                                         MPIPos.resize(numSeqs+1);
233                                         MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
234                                 
235                                         //figure out how many sequences you have to align
236                                         numSeqsPerProcessor = numSeqs / processors;
237                                         int startIndex =  pid * numSeqsPerProcessor;
238                                         if(pid == (processors - 1)){    numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor;      }
239                                 
240                                         //do your part
241                                         MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
242                                 }
243                                 
244                                 MPI_File_close(&inMPI);
245                                 MPI_File_close(&outMPI);
246                                 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
247                                 
248                                 if (pid == 0) {
249                                         //get the info from the child processes
250                                         for(int i = 1; i < processors; i++) { 
251                                                 int size;
252                                                 MPI_Recv(&size, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
253
254                                                 vector<int> temp; temp.resize(size+1);
255                                                 
256                                                 for(int j = 0; j < 5; j++) { 
257                                                 
258                                                         MPI_Recv(&temp[0], (size+1), MPI_INT, i, 2001, MPI_COMM_WORLD, &status); 
259                                                         int receiveTag = temp[temp.size()-1];  //child process added a int to the end to indicate what count this is for
260                                                         
261                                                         if (receiveTag == startTag) { 
262                                                                 for (int k = 0; k < size; k++) {                startPosition.push_back(temp[k]);       }
263                                                         }else if (receiveTag == endTag) { 
264                                                                 for (int k = 0; k < size; k++) {                endPosition.push_back(temp[k]); }
265                                                         }else if (receiveTag == lengthTag) { 
266                                                                 for (int k = 0; k < size; k++) {                seqLength.push_back(temp[k]);   }
267                                                         }else if (receiveTag == baseTag) { 
268                                                                 for (int k = 0; k < size; k++) {                ambigBases.push_back(temp[k]);  }
269                                                         }else if (receiveTag == lhomoTag) { 
270                                                                 for (int k = 0; k < size; k++) {                longHomoPolymer.push_back(temp[k]);     }
271                                                         }
272                                                 } 
273                                         }
274
275                                 }else{
276                                 
277                                         //send my counts
278                                         int size = startPosition.size();
279                                         MPI_Send(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
280                                         
281                                         startPosition.push_back(startTag);
282                                         int ierr = MPI_Send(&(startPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
283                                         endPosition.push_back(endTag);
284                                         ierr = MPI_Send (&(endPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
285                                         seqLength.push_back(lengthTag);
286                                         ierr = MPI_Send(&(seqLength[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
287                                         ambigBases.push_back(baseTag);
288                                         ierr = MPI_Send(&(ambigBases[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
289                                         longHomoPolymer.push_back(lhomoTag);
290                                         ierr = MPI_Send(&(longHomoPolymer[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
291                                 }
292                                 
293                                 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
294 #else
295                         vector<unsigned long int> positions = m->divideFile(fastafile, processors);
296                                 
297                         for (int i = 0; i < (positions.size()-1); i++) {
298                                 lines.push_back(new linePair(positions[i], positions[(i+1)]));
299                         }       
300
301                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
302                                 if(processors == 1){
303                                         numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]);
304                                 }else{
305                                         numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile); 
306                                         
307                                         rename((summaryFile + toString(processIDS[0]) + ".temp").c_str(), summaryFile.c_str());
308                                         //append files
309                                         for(int i=1;i<processors;i++){
310                                                 m->appendFiles((summaryFile + toString(processIDS[i]) + ".temp"), summaryFile);
311                                                 remove((summaryFile + toString(processIDS[i]) + ".temp").c_str());
312                                         }
313                                 }
314                                 
315                                 if (m->control_pressed) {  return 0; }
316                 #else
317                                 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]);
318                                 if (m->control_pressed) {  return 0; }
319                 #endif
320 #endif
321                         
322                 #ifdef USE_MPI
323                         if (pid == 0) { 
324                 #endif
325                 
326                 sort(startPosition.begin(), startPosition.end());
327                 sort(endPosition.begin(), endPosition.end());
328                 sort(seqLength.begin(), seqLength.end());
329                 sort(ambigBases.begin(), ambigBases.end());
330                 sort(longHomoPolymer.begin(), longHomoPolymer.end());
331                 
332                 int ptile0_25   = int(numSeqs * 0.025);
333                 int ptile25             = int(numSeqs * 0.250);
334                 int ptile50             = int(numSeqs * 0.500);
335                 int ptile75             = int(numSeqs * 0.750);
336                 int ptile97_5   = int(numSeqs * 0.975);
337                 int ptile100    = numSeqs - 1;
338                 
339                 //to compensate for blank sequences that would result in startPosition and endPostion equalling -1
340                 if (startPosition[0] == -1) {  startPosition[0] = 0;    }
341                 if (endPosition[0] == -1)       {  endPosition[0] = 0;          }
342                 
343                 if (m->control_pressed) {  remove(summaryFile.c_str()); return 0; }
344                 
345                 m->mothurOutEndLine();
346                 m->mothurOut("\t\tStart\tEnd\tNBases\tAmbigs\tPolymer"); m->mothurOutEndLine();
347                 m->mothurOut("Minimum:\t" + toString(startPosition[0]) + "\t" + toString(endPosition[0]) + "\t" + toString(seqLength[0]) + "\t" + toString(ambigBases[0]) + "\t" + toString(longHomoPolymer[0])); m->mothurOutEndLine();
348                 m->mothurOut("2.5%-tile:\t" + toString(startPosition[ptile0_25]) + "\t" + toString(endPosition[ptile0_25]) + "\t" + toString(seqLength[ptile0_25]) + "\t" + toString(ambigBases[ptile0_25]) + "\t"+ toString(longHomoPolymer[ptile0_25])); m->mothurOutEndLine();
349                 m->mothurOut("25%-tile:\t" + toString(startPosition[ptile25]) + "\t" + toString(endPosition[ptile25]) + "\t" + toString(seqLength[ptile25]) + "\t" + toString(ambigBases[ptile25]) + "\t" + toString(longHomoPolymer[ptile25])); m->mothurOutEndLine();
350                 m->mothurOut("Median: \t" + toString(startPosition[ptile50]) + "\t" + toString(endPosition[ptile50]) + "\t" + toString(seqLength[ptile50]) + "\t" + toString(ambigBases[ptile50]) + "\t" + toString(longHomoPolymer[ptile50])); m->mothurOutEndLine();
351                 m->mothurOut("75%-tile:\t" + toString(startPosition[ptile75]) + "\t" + toString(endPosition[ptile75]) + "\t" + toString(seqLength[ptile75]) + "\t" + toString(ambigBases[ptile75]) + "\t" + toString(longHomoPolymer[ptile75])); m->mothurOutEndLine();
352                 m->mothurOut("97.5%-tile:\t" + toString(startPosition[ptile97_5]) + "\t" + toString(endPosition[ptile97_5]) + "\t" + toString(seqLength[ptile97_5]) + "\t" + toString(ambigBases[ptile97_5]) + "\t" + toString(longHomoPolymer[ptile97_5])); m->mothurOutEndLine();
353                 m->mothurOut("Maximum:\t" + toString(startPosition[ptile100]) + "\t" + toString(endPosition[ptile100]) + "\t" + toString(seqLength[ptile100]) + "\t" + toString(ambigBases[ptile100]) + "\t" + toString(longHomoPolymer[ptile100])); m->mothurOutEndLine();
354                 if (namefile == "") {  m->mothurOut("# of Seqs:\t" + toString(numSeqs)); m->mothurOutEndLine(); }
355                 else { m->mothurOut("# of unique seqs:\t" + toString(numSeqs)); m->mothurOutEndLine(); m->mothurOut("total # of seqs:\t" + toString(startPosition.size())); m->mothurOutEndLine(); }
356                 
357                 if (m->control_pressed) {  remove(summaryFile.c_str()); return 0; }
358                 
359                 m->mothurOutEndLine();
360                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
361                 m->mothurOut(summaryFile); m->mothurOutEndLine();       outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
362                 m->mothurOutEndLine();
363                 
364                 #ifdef USE_MPI
365                         }
366                 #endif
367
368                 return 0;
369         }
370         catch(exception& e) {
371                 m->errorOut(e, "SeqSummaryCommand", "execute");
372                 exit(1);
373         }
374 }
375 /**************************************************************************************/
376 int SeqSummaryCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile, linePair* filePos) {   
377         try {
378                 
379                 ofstream outSummary;
380                 m->openOutputFile(sumFile, outSummary);
381                 
382                 //print header if you are process 0
383                 if (filePos->start == 0) {
384                         outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer\tnumSeqs" << endl;  
385                 }
386                                 
387                 ifstream in;
388                 m->openInputFile(filename, in);
389                                 
390                 in.seekg(filePos->start);
391
392                 bool done = false;
393                 int count = 0;
394         
395                 while (!done) {
396                                 
397                         if (m->control_pressed) { in.close(); outSummary.close(); return 1; }
398                                         
399                         Sequence current(in); m->gobble(in);
400         
401                         if (current.getName() != "") {
402                                 
403                                 int num = 1;
404                                 if (namefile != "") {
405                                         //make sure this sequence is in the namefile, else error 
406                                         map<string, int>::iterator it = nameMap.find(current.getName());
407                                         
408                                         if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
409                                         else { num = it->second; }
410                                 }
411                                 
412                                 //for each sequence this sequence represents
413                                 for (int i = 0; i < num; i++) {
414                                         startPosition.push_back(current.getStartPos());
415                                         endPosition.push_back(current.getEndPos());
416                                         seqLength.push_back(current.getNumBases());
417                                         ambigBases.push_back(current.getAmbigBases());
418                                         longHomoPolymer.push_back(current.getLongHomoPolymer());
419                                 }
420                                 
421                                 count++;
422                                 outSummary << current.getName() << '\t';
423                                 outSummary << current.getStartPos() << '\t' << current.getEndPos() << '\t';
424                                 outSummary << current.getNumBases() << '\t' << current.getAmbigBases() << '\t';
425                                 outSummary << current.getLongHomoPolymer() << '\t' << num << endl;
426                         }
427                         
428                         #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
429                                 unsigned long int pos = in.tellg();
430                                 if ((pos == -1) || (pos >= filePos->end)) { break; }
431                         #else
432                                 if (in.eof()) { break; }
433                         #endif
434                         
435                         //report progress
436                         //if((count) % 100 == 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
437                 }
438                 //report progress
439                 //if((count) % 100 != 0){       m->mothurOut(toString(count)); m->mothurOutEndLine();           }
440                 
441                 in.close();
442                 
443                 return count;
444         }
445         catch(exception& e) {
446                 m->errorOut(e, "SeqSummaryCommand", "driverCreateSummary");
447                 exit(1);
448         }
449 }
450 #ifdef USE_MPI
451 /**************************************************************************************/
452 int SeqSummaryCommand::MPICreateSummary(int start, int num, vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos) {        
453         try {
454                 
455                 int pid;
456                 MPI_Status status; 
457                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
458
459                 for(int i=0;i<num;i++){
460                         
461                         if (m->control_pressed) { return 0; }
462                         
463                         //read next sequence
464                         int length = MPIPos[start+i+1] - MPIPos[start+i];
465         
466                         char* buf4 = new char[length];
467                         MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
468                         
469                         string tempBuf = buf4;
470                         if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length);  }
471                         istringstream iss (tempBuf,istringstream::in);
472                         delete buf4;
473
474                         Sequence current(iss);  
475
476                         if (current.getName() != "") {
477                                 
478                                 int num = 1;
479                                 if (namefile != "") {
480                                         //make sure this sequence is in the namefile, else error 
481                                         map<string, int>::iterator it = nameMap.find(current.getName());
482                                         
483                                         if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
484                                         else { num = it->second; }
485                                 }
486                                 
487                                 //for each sequence this sequence represents
488                                 for (int i = 0; i < num; i++) {
489                                         startPosition.push_back(current.getStartPos());
490                                         endPosition.push_back(current.getEndPos());
491                                         seqLength.push_back(current.getNumBases());
492                                         ambigBases.push_back(current.getAmbigBases());
493                                         longHomoPolymer.push_back(current.getLongHomoPolymer());
494                                 }
495                                 
496                                 string outputString = current.getName() + "\t" + toString(current.getStartPos()) + "\t" + toString(current.getEndPos()) + "\t";
497                                 outputString += toString(current.getNumBases()) + "\t" + toString(current.getAmbigBases()) + "\t" + toString(current.getLongHomoPolymer()) + "\t" + toString(num) + "\n";
498                                 
499                                 //output to file
500                                 length = outputString.length();
501                                 char* buf3 = new char[length];
502                                 memcpy(buf3, outputString.c_str(), length);
503                                         
504                                 MPI_File_write_shared(outMPI, buf3, length, MPI_CHAR, &status);
505                                 delete buf3;
506                         }       
507                 }
508                 
509                 return 0;
510         }
511         catch(exception& e) {
512                 m->errorOut(e, "SeqSummaryCommand", "MPICreateSummary");
513                 exit(1);
514         }
515 }
516 #endif
517 /**************************************************************************************************/
518 int SeqSummaryCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile) {
519         try {
520 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
521                 int process = 0;
522                 int num = 0;
523                 processIDS.clear();
524                 
525                 //loop through and create all the processes you want
526                 while (process != processors) {
527                         int pid = fork();
528                         
529                         if (pid > 0) {
530                                 processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
531                                 process++;
532                         }else if (pid == 0){
533                                 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile + toString(getpid()) + ".temp", lines[process]);
534                                 
535                                 //pass numSeqs to parent
536                                 ofstream out;
537                                 string tempFile = fastafile + toString(getpid()) + ".num.temp";
538                                 m->openOutputFile(tempFile, out);
539                                 
540                                 out << num << endl;
541                                 out << startPosition.size() << endl;
542                                 for (int k = 0; k < startPosition.size(); k++)          {               out << startPosition[k] << '\t'; }  out << endl;
543                                 for (int k = 0; k < endPosition.size(); k++)            {               out << endPosition[k] << '\t'; }  out << endl;
544                                 for (int k = 0; k < seqLength.size(); k++)                      {               out << seqLength[k] << '\t'; }  out << endl;
545                                 for (int k = 0; k < ambigBases.size(); k++)                     {               out << ambigBases[k] << '\t'; }  out << endl;
546                                 for (int k = 0; k < longHomoPolymer.size(); k++)        {               out << longHomoPolymer[k] << '\t'; }  out << endl;
547                                 
548                                 out.close();
549                                 
550                                 exit(0);
551                         }else { 
552                                 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
553                                 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
554                                 exit(0);
555                         }
556                 }
557                 
558                 //force parent to wait until all the processes are done
559                 for (int i=0;i<processors;i++) { 
560                         int temp = processIDS[i];
561                         wait(&temp);
562                 }
563                 
564                 //parent reads in and combine Filter info
565                 for (int i = 0; i < processIDS.size(); i++) {
566                         string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
567                         ifstream in;
568                         m->openInputFile(tempFilename, in);
569                         
570                         int temp, tempNum;
571                         in >> tempNum; m->gobble(in); num += tempNum;
572                         in >> tempNum; m->gobble(in);
573                         for (int k = 0; k < tempNum; k++)                       {               in >> temp; startPosition.push_back(temp);              }               m->gobble(in);
574                         for (int k = 0; k < tempNum; k++)                       {               in >> temp; endPosition.push_back(temp);                }               m->gobble(in);
575                         for (int k = 0; k < tempNum; k++)                       {               in >> temp; seqLength.push_back(temp);                  }               m->gobble(in);
576                         for (int k = 0; k < tempNum; k++)                       {               in >> temp; ambigBases.push_back(temp);                 }               m->gobble(in);
577                         for (int k = 0; k < tempNum; k++)                       {               in >> temp; longHomoPolymer.push_back(temp);    }               m->gobble(in);
578                                 
579                         in.close();
580                         remove(tempFilename.c_str());
581                 }
582                 
583                 return num;
584 #endif          
585         }
586         catch(exception& e) {
587                 m->errorOut(e, "SeqSummaryCommand", "createProcessesCreateSummary");
588                 exit(1);
589         }
590 }
591 /**********************************************************************************************************************/
592 int SeqSummaryCommand::readNames() { 
593         try {
594                 //open input file
595                 ifstream in;
596                 m->openInputFile(namefile, in);
597                 
598                 while (!in.eof()) {
599                         if (m->control_pressed) { break; }
600                         
601                         string firstCol, secondCol;
602                         in >> firstCol >> secondCol; m->gobble(in);
603                         
604                         int num = m->getNumNames(secondCol);
605                         
606                         nameMap[firstCol] = num;
607                 }
608                 in.close();
609                 
610                 return 0;
611                 
612         }
613         catch(exception& e) {
614                 m->errorOut(e, "SeqSummaryCommand", "readNames");
615                 exit(1);
616         }
617 }
618
619 /**********************************************************************************************************************/
620
621
622