X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=seqsummarycommand.cpp;h=7edb717853461ba4cd23f9845b36880dd9ce38ae;hb=3e2465c16d187247ce3befd29811c2d5dfc15ee8;hp=5009ff5c2eeb87e1cbe49fdd0f240355bfcaaf74;hpb=b4f80c1d2be78a8743a408a2b6d462b07f9f71ff;p=mothur.git diff --git a/seqsummarycommand.cpp b/seqsummarycommand.cpp index 5009ff5..7edb717 100644 --- a/seqsummarycommand.cpp +++ b/seqsummarycommand.cpp @@ -10,6 +10,54 @@ #include "seqsummarycommand.h" #include "sequence.hpp" +//********************************************************************************************************************** +vector SeqSummaryCommand::getValidParameters(){ + try { + string Array[] = {"fasta","processors","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "SeqSummaryCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +SeqSummaryCommand::SeqSummaryCommand(){ + try { + abort = true; + //initialize outputTypes + vector tempOutNames; + outputTypes["summary"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "SeqSummaryCommand", "SeqSummaryCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector SeqSummaryCommand::getRequiredParameters(){ + try { + string Array[] = {"fasta"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "SeqSummaryCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector SeqSummaryCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "SeqSummaryCommand", "getRequiredFiles"); + exit(1); + } +} //*************************************************************************************************************** SeqSummaryCommand::SeqSummaryCommand(string option) { @@ -43,12 +91,16 @@ SeqSummaryCommand::SeqSummaryCommand(string option) { it = parameters.find("fasta"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->hasPath(it->second); //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["fasta"] = inputDir + it->second; } } } + //initialize outputTypes + vector tempOutNames; + outputTypes["summary"] = tempOutNames; + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } @@ -57,7 +109,7 @@ SeqSummaryCommand::SeqSummaryCommand(string option) { //if the user changes the output directory command factory will send this info to us in the output parameter outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; - outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it + outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it } string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; } @@ -98,7 +150,7 @@ int SeqSummaryCommand::execute(){ if (abort == true) { return 0; } - string summaryFile = outputDir + getSimpleName(fastafile) + ".summary"; + string summaryFile = outputDir + m->getSimpleName(fastafile) + ".summary"; int numSeqs = 0; @@ -143,7 +195,7 @@ int SeqSummaryCommand::execute(){ MPI_File_write_shared(outMPI, buf2, length, MPI_CHAR, &statusOut); delete buf2; - MPIPos = setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs + MPIPos = m->setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs for(int i = 1; i < processors; i++) { MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD); @@ -224,38 +276,29 @@ int SeqSummaryCommand::execute(){ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case #else + vector positions = m->divideFile(fastafile, processors); + + for (int i = 0; i < (positions.size()-1); i++) { + lines.push_back(new linePair(positions[i], positions[(i+1)])); + } + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) if(processors == 1){ - ifstream inFASTA; - openInputFile(fastafile, inFASTA); - getNumSeqs(inFASTA, numSeqs); - inFASTA.close(); - - lines.push_back(new linePair(0, numSeqs)); - - driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]); + numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]); }else{ - numSeqs = setLines(fastafile); - createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile); + numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile); rename((summaryFile + toString(processIDS[0]) + ".temp").c_str(), summaryFile.c_str()); //append files for(int i=1;iappendFiles((summaryFile + toString(processIDS[i]) + ".temp"), summaryFile); remove((summaryFile + toString(processIDS[i]) + ".temp").c_str()); } } if (m->control_pressed) { return 0; } #else - ifstream inFASTA; - openInputFile(fastafile, inFASTA); - getNumSeqs(inFASTA, numSeqs); - inFASTA.close(); - - lines.push_back(new linePair(0, numSeqs)); - - driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]); + numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, summaryFile, lines[0]); if (m->control_pressed) { return 0; } #endif #endif @@ -298,7 +341,7 @@ int SeqSummaryCommand::execute(){ m->mothurOutEndLine(); m->mothurOut("Output File Name: "); m->mothurOutEndLine(); - m->mothurOut(summaryFile); m->mothurOutEndLine(); + m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile); m->mothurOutEndLine(); #ifdef USE_MPI @@ -313,27 +356,30 @@ int SeqSummaryCommand::execute(){ } } /**************************************************************************************/ -int SeqSummaryCommand::driverCreateSummary(vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename, string sumFile, linePair* line) { +int SeqSummaryCommand::driverCreateSummary(vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename, string sumFile, linePair* filePos) { try { ofstream outSummary; - openOutputFile(sumFile, outSummary); + m->openOutputFile(sumFile, outSummary); //print header if you are process 0 - if (line->start == 0) { + if (filePos->start == 0) { outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer" << endl; } ifstream in; - openInputFile(filename, in); + m->openInputFile(filename, in); - in.seekg(line->start); - - for(int i=0;inum;i++){ + in.seekg(filePos->start); + + bool done = false; + int count = 0; + + while (!done) { if (m->control_pressed) { in.close(); outSummary.close(); return 1; } - Sequence current(in); + Sequence current(in); m->gobble(in); if (current.getName() != "") { startPosition.push_back(current.getStartPos()); @@ -346,12 +392,25 @@ int SeqSummaryCommand::driverCreateSummary(vector& startPosition, vector= filePos->end)) { break; } + #else + if (in.eof()) { break; } + #endif + + //report progress + //if((count) % 100 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } } + //report progress + //if((count) % 100 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); } + in.close(); - return 0; + return count; } catch(exception& e) { m->errorOut(e, "SeqSummaryCommand", "driverCreateSummary"); @@ -418,20 +477,39 @@ int SeqSummaryCommand::createProcessesCreateSummary(vector& startPosition, try { #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) int process = 0; - int exitCommand = 1; + int num = 0; processIDS.clear(); //loop through and create all the processes you want while (process != processors) { - int pid = vfork(); + int pid = fork(); if (pid > 0) { processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later process++; }else if (pid == 0){ - driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile + toString(getpid()) + ".temp", lines[process]); + num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, sumFile + toString(getpid()) + ".temp", lines[process]); + + //pass numSeqs to parent + ofstream out; + string tempFile = fastafile + toString(getpid()) + ".num.temp"; + m->openOutputFile(tempFile, out); + + out << num << endl; + for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl; + for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl; + for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl; + for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl; + for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl; + + out.close(); + exit(0); - }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); } + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } } //force parent to wait until all the processes are done @@ -440,65 +518,29 @@ int SeqSummaryCommand::createProcessesCreateSummary(vector& startPosition, wait(&temp); } - return exitCommand; -#endif - } - catch(exception& e) { - m->errorOut(e, "SeqSummaryCommand", "createProcessesCreateSummary"); - exit(1); - } -} -/**************************************************************************************************/ - -int SeqSummaryCommand::setLines(string filename) { - try { - - vector positions; - - ifstream inFASTA; - openInputFile(filename, inFASTA); + //parent reads in and combine Filter info + for (int i = 0; i < processIDS.size(); i++) { + string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp"; + ifstream in; + m->openInputFile(tempFilename, in); - string input; - while(!inFASTA.eof()){ - input = getline(inFASTA); - - if (input.length() != 0) { - if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); } - } - } - inFASTA.close(); - - int numFastaSeqs = positions.size(); - - FILE * pFile; - unsigned long int size; - - //get num bytes in file - pFile = fopen (filename.c_str(),"rb"); - if (pFile==NULL) perror ("Error opening file"); - else{ - fseek (pFile, 0, SEEK_END); - size=ftell (pFile); - fclose (pFile); - } - - int numSeqsPerProcessor = numFastaSeqs / processors; - - for (int i = 0; i < processors; i++) { - - unsigned long int startPos = positions[ i * numSeqsPerProcessor ]; - if(i == processors - 1){ - numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; - }else{ - unsigned long int myEnd = positions[ (i+1) * numSeqsPerProcessor ]; - } - lines.push_back(new linePair(startPos, numSeqsPerProcessor)); + int temp, tempNum; + in >> tempNum; m->gobble(in); num += tempNum; + for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in); + + in.close(); + remove(tempFilename.c_str()); } - return numFastaSeqs; + return num; +#endif } catch(exception& e) { - m->errorOut(e, "SeqSummaryCommand", "setLines"); + m->errorOut(e, "SeqSummaryCommand", "createProcessesCreateSummary"); exit(1); } }