5 * Created by Pat Schloss on 5/30/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "seqsummarycommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> SeqSummaryCommand::getValidParameters(){
16 string Array[] = {"fasta","processors","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "SeqSummaryCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 SeqSummaryCommand::SeqSummaryCommand(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["summary"] = tempOutNames;
33 m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
37 //**********************************************************************************************************************
38 vector<string> SeqSummaryCommand::getRequiredParameters(){
40 string Array[] = {"fasta"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "SeqSummaryCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> SeqSummaryCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "SeqSummaryCommand", "getRequiredFiles");
60 //***************************************************************************************************************
62 SeqSummaryCommand::SeqSummaryCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta","processors","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter("summary.seqs");
78 map<string,string>::iterator it;
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; }
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 = ""; }
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; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["summary"] = tempOutNames;
103 //check for required parameters
104 fastafile = validParameter.validFile(parameters, "fasta", true);
105 if (fastafile == "not open") { abort = true; }
106 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the summary.seqs command."); m->mothurOutEndLine(); abort = true; }
108 //if the user changes the output directory command factory will send this info to us in the output parameter
109 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
111 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
114 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
115 convert(temp, processors);
120 catch(exception& e) {
121 m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand");
125 //**********************************************************************************************************************
127 void SeqSummaryCommand::help(){
129 m->mothurOut("The summary.seqs command reads a fastafile and summarizes the sequences.\n");
130 m->mothurOut("The summary.seqs command parameters are fasta and processors, fasta is required.\n");
131 m->mothurOut("The summary.seqs command should be in the following format: \n");
132 m->mothurOut("summary.seqs(fasta=yourFastaFile, processors=2) \n");
133 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
135 catch(exception& e) {
136 m->errorOut(e, "SeqSummaryCommand", "help");
141 //***************************************************************************************************************
143 SeqSummaryCommand::~SeqSummaryCommand(){ /* do nothing */ }
145 //***************************************************************************************************************
147 int SeqSummaryCommand::execute(){
150 if (abort == true) { if (calledHelp) { return 0; } return 2; }
152 string summaryFile = outputDir + m->getSimpleName(fastafile) + ".summary";
156 vector<int> startPosition;
157 vector<int> endPosition;
158 vector<int> seqLength;
159 vector<int> ambigBases;
160 vector<int> longHomoPolymer;
163 int pid, numSeqsPerProcessor;
165 int startTag = 1; int endTag = 2; int lengthTag = 3; int baseTag = 4; int lhomoTag = 5;
166 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
167 vector<unsigned long int> MPIPos;
170 MPI_Status statusOut;
173 MPI_Comm_size(MPI_COMM_WORLD, &processors);
174 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
176 char tempFileName[1024];
177 strcpy(tempFileName, fastafile.c_str());
179 char sumFileName[1024];
180 strcpy(sumFileName, summaryFile.c_str());
182 MPI_File_open(MPI_COMM_WORLD, tempFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
183 MPI_File_open(MPI_COMM_WORLD, sumFileName, outMode, MPI_INFO_NULL, &outMPI);
185 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); return 0; }
187 if (pid == 0) { //you are the root process
189 string outputString = "seqname\tstart\tend\tnbases\tambigs\tpolymer\n";
190 int length = outputString.length();
191 char* buf2 = new char[length];
192 memcpy(buf2, outputString.c_str(), length);
194 MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &statusOut);
197 MPIPos = m->setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs
199 for(int i = 1; i < processors; i++) {
200 MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
201 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
204 //figure out how many sequences you have to do
205 numSeqsPerProcessor = numSeqs / processors;
206 int startIndex = pid * numSeqsPerProcessor;
207 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
210 MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
212 }else { //i am the child process
214 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
215 MPIPos.resize(numSeqs+1);
216 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
218 //figure out how many sequences you have to align
219 numSeqsPerProcessor = numSeqs / processors;
220 int startIndex = pid * numSeqsPerProcessor;
221 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
224 MPICreateSummary(startIndex, numSeqsPerProcessor, startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, inMPI, outMPI, MPIPos);
227 MPI_File_close(&inMPI);
228 MPI_File_close(&outMPI);
229 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
232 //get the info from the child processes
233 for(int i = 1; i < processors; i++) {
235 MPI_Recv(&size, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
237 vector<int> temp; temp.resize(size+1);
239 for(int j = 0; j < 5; j++) {
241 MPI_Recv(&temp[0], (size+1), MPI_INT, i, 2001, MPI_COMM_WORLD, &status);
242 int receiveTag = temp[temp.size()-1]; //child process added a int to the end to indicate what count this is for
244 if (receiveTag == startTag) {
245 for (int k = 0; k < size; k++) { startPosition.push_back(temp[k]); }
246 }else if (receiveTag == endTag) {
247 for (int k = 0; k < size; k++) { endPosition.push_back(temp[k]); }
248 }else if (receiveTag == lengthTag) {
249 for (int k = 0; k < size; k++) { seqLength.push_back(temp[k]); }
250 }else if (receiveTag == baseTag) {
251 for (int k = 0; k < size; k++) { ambigBases.push_back(temp[k]); }
252 }else if (receiveTag == lhomoTag) {
253 for (int k = 0; k < size; k++) { longHomoPolymer.push_back(temp[k]); }
261 int size = startPosition.size();
262 MPI_Send(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
264 startPosition.push_back(startTag);
265 int ierr = MPI_Send(&(startPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
266 endPosition.push_back(endTag);
267 ierr = MPI_Send (&(endPosition[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
268 seqLength.push_back(lengthTag);
269 ierr = MPI_Send(&(seqLength[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
270 ambigBases.push_back(baseTag);
271 ierr = MPI_Send(&(ambigBases[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
272 longHomoPolymer.push_back(lhomoTag);
273 ierr = MPI_Send(&(longHomoPolymer[0]), (size+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
276 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
278 vector<unsigned long int> positions = m->divideFile(fastafile, processors);
280 for (int i = 0; i < (positions.size()-1); i++) {
281 lines.push_back(new linePair(positions[i], positions[(i+1)]));
284 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
286 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]);
288 numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile);
290 rename((summaryFile + toString(processIDS[0]) + ".temp").c_str(), summaryFile.c_str());
292 for(int i=1;i<processors;i++){
293 m->appendFiles((summaryFile + toString(processIDS[i]) + ".temp"), summaryFile);
294 remove((summaryFile + toString(processIDS[i]) + ".temp").c_str());
298 if (m->control_pressed) { return 0; }
300 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]);
301 if (m->control_pressed) { return 0; }
309 sort(startPosition.begin(), startPosition.end());
310 sort(endPosition.begin(), endPosition.end());
311 sort(seqLength.begin(), seqLength.end());
312 sort(ambigBases.begin(), ambigBases.end());
313 sort(longHomoPolymer.begin(), longHomoPolymer.end());
315 int ptile0_25 = int(numSeqs * 0.025);
316 int ptile25 = int(numSeqs * 0.250);
317 int ptile50 = int(numSeqs * 0.500);
318 int ptile75 = int(numSeqs * 0.750);
319 int ptile97_5 = int(numSeqs * 0.975);
320 int ptile100 = numSeqs - 1;
322 //to compensate for blank sequences that would result in startPosition and endPostion equalling -1
323 if (startPosition[0] == -1) { startPosition[0] = 0; }
324 if (endPosition[0] == -1) { endPosition[0] = 0; }
326 if (m->control_pressed) { remove(summaryFile.c_str()); return 0; }
328 m->mothurOutEndLine();
329 m->mothurOut("\t\tStart\tEnd\tNBases\tAmbigs\tPolymer"); m->mothurOutEndLine();
330 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();
331 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();
332 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();
333 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();
334 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();
335 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();
336 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();
337 m->mothurOut("# of Seqs:\t" + toString(numSeqs)); m->mothurOutEndLine();
339 if (m->control_pressed) { remove(summaryFile.c_str()); return 0; }
341 m->mothurOutEndLine();
342 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
343 m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
344 m->mothurOutEndLine();
352 catch(exception& e) {
353 m->errorOut(e, "SeqSummaryCommand", "execute");
357 /**************************************************************************************/
358 int SeqSummaryCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile, linePair* filePos) {
362 m->openOutputFile(sumFile, outSummary);
364 //print header if you are process 0
365 if (filePos->start == 0) {
366 outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer" << endl;
370 m->openInputFile(filename, in);
372 in.seekg(filePos->start);
379 if (m->control_pressed) { in.close(); outSummary.close(); return 1; }
381 Sequence current(in); m->gobble(in);
383 if (current.getName() != "") {
384 startPosition.push_back(current.getStartPos());
385 endPosition.push_back(current.getEndPos());
386 seqLength.push_back(current.getNumBases());
387 ambigBases.push_back(current.getAmbigBases());
388 longHomoPolymer.push_back(current.getLongHomoPolymer());
390 outSummary << current.getName() << '\t';
391 outSummary << current.getStartPos() << '\t' << current.getEndPos() << '\t';
392 outSummary << current.getNumBases() << '\t' << current.getAmbigBases() << '\t';
393 outSummary << current.getLongHomoPolymer() << endl;
397 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
398 unsigned long int pos = in.tellg();
399 if ((pos == -1) || (pos >= filePos->end)) { break; }
401 if (in.eof()) { break; }
405 //if((count) % 100 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
408 //if((count) % 100 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
414 catch(exception& e) {
415 m->errorOut(e, "SeqSummaryCommand", "driverCreateSummary");
420 /**************************************************************************************/
421 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) {
426 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
429 for(int i=0;i<num;i++){
431 if (m->control_pressed) { return 0; }
434 int length = MPIPos[start+i+1] - MPIPos[start+i];
436 char* buf4 = new char[length];
437 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
439 string tempBuf = buf4;
440 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
441 istringstream iss (tempBuf,istringstream::in);
444 Sequence current(iss);
446 if (current.getName() != "") {
447 startPosition.push_back(current.getStartPos());
448 endPosition.push_back(current.getEndPos());
449 seqLength.push_back(current.getNumBases());
450 ambigBases.push_back(current.getAmbigBases());
451 longHomoPolymer.push_back(current.getLongHomoPolymer());
453 string outputString = current.getName() + "\t" + toString(current.getStartPos()) + "\t" + toString(current.getEndPos()) + "\t";
454 outputString += toString(current.getNumBases()) + "\t" + toString(current.getAmbigBases()) + "\t" + toString(current.getLongHomoPolymer()) + "\n";
457 length = outputString.length();
458 char* buf3 = new char[length];
459 memcpy(buf3, outputString.c_str(), length);
461 MPI_File_write_shared(outMPI, buf3, length, MPI_CHAR, &status);
468 catch(exception& e) {
469 m->errorOut(e, "SeqSummaryCommand", "MPICreateSummary");
474 /**************************************************************************************************/
475 int SeqSummaryCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, string sumFile) {
477 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
482 //loop through and create all the processes you want
483 while (process != processors) {
487 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
490 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile + toString(getpid()) + ".temp", lines[process]);
492 //pass numSeqs to parent
494 string tempFile = fastafile + toString(getpid()) + ".num.temp";
495 m->openOutputFile(tempFile, out);
498 for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
499 for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
500 for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
501 for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
502 for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
508 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
509 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
514 //force parent to wait until all the processes are done
515 for (int i=0;i<processors;i++) {
516 int temp = processIDS[i];
520 //parent reads in and combine Filter info
521 for (int i = 0; i < processIDS.size(); i++) {
522 string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
524 m->openInputFile(tempFilename, in);
527 in >> tempNum; m->gobble(in); num += tempNum;
528 for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
529 for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
530 for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
531 for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
532 for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
535 remove(tempFilename.c_str());
541 catch(exception& e) {
542 m->errorOut(e, "SeqSummaryCommand", "createProcessesCreateSummary");
546 //***************************************************************************************************************