X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=filterseqscommand.cpp;h=68450fb15a7b140803aab2d54d8c5bc84e7ce688;hb=7e354c9abb09ea3cf5b500a16cc7f6dd79ccb6f5;hp=8864bb3c57d32052220013ebf88267f865ccdbd1;hpb=544469443afe44920bdf279aefd26d29534cabaf;p=mothur.git diff --git a/filterseqscommand.cpp b/filterseqscommand.cpp index 8864bb3..68450fb 100644 --- a/filterseqscommand.cpp +++ b/filterseqscommand.cpp @@ -8,181 +8,183 @@ */ #include "filterseqscommand.h" +#include "sequence.hpp" /**************************************************************************************/ -FilterSeqsCommand::FilterSeqsCommand(){ - globaldata = GlobalData::getInstance(); - - if(globaldata->getFastaFile() == "") { cout << "You must enter a fasta formatted file" << endl; } - trump = globaldata->getTrump()[0]; - vertical = -// readSeqs->read(); -// db = readSeqs->getDB(); -// numSeqs = db->size(); -// -// alignmentLength = db->get(0).getAlignLength(); -// -// filter = string(alignmentLength, '1'); -} - -/**************************************************************************************/ - -void FilterSeqsCommand::doHard() { - -// string hardName = globaldata->getHard(); -// string hardFilter = ""; -// -// ifstream fileHandle; -// openInputFile(hardName, fileHandle); -// -// fileHandle >> hardFilter; -// -// if(hardFilter.length() != filter.length()){ -// cout << "The hard filter is not the same length as the alignment: Hard filter will not be applied." << endl; -// } -// else{ -// filter = hardFilter; -// } - -} - -/**************************************************************************************/ - -void FilterSeqsCommand::doTrump() { - - - for(int i = 0; i < numSeqs; i++) { - string curAligned = db->get(i).getAligned();; - - for(int j = 0; j < alignmentLength; j++) { - if(curAligned[j] == trump){ - filter[j] = '0'; +FilterSeqsCommand::FilterSeqsCommand(string option){ + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta", "trump", "soft", "hard", "vertical"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + + //check for required parameters + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not found") { mothurOut("fasta is a required parameter for the filter.seqs command."); mothurOutEndLine(); abort = true; } + else if (fastafile == "not open") { abort = true; } + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + + string temp; + temp = validParameter.validFile(parameters, "trump", false); if (temp == "not found") { temp = "*"; } + trump = temp[0]; + + temp = validParameter.validFile(parameters, "soft", false); if (temp == "not found") { soft = 0; } + else { soft = (float)atoi(temp.c_str()) / 100.0; } + + hard = validParameter.validFile(parameters, "hard", true); if (hard == "not found") { hard = ""; } + else if (hard == "not open") { abort = true; } + + vertical = validParameter.validFile(parameters, "vertical", false); if (vertical == "not found") { vertical = "T"; } + + numSeqs = 0; + + if (abort == false) { + + if (soft != 0) { F.setSoft(soft); } + if (trump != '*') { F.setTrump(trump); } + + } + } + + } + catch(exception& e) { + errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand"); + exit(1); } - -} - -/**************************************************************************************/ - -void FilterSeqsCommand::doVertical() { - -// vector counts(alignmentLength, 0); -// -// for(int i = 0; i < numSeqs; i++) { -// string curAligned = db->get(i).getAligned();; -// -// for(int j = 0; j < alignmentLength; j++) { -// if(curAligned[j] == '-' || curAligned[j] == '.'){ -// counts[j]++; -// } -// } -// } -// for(int i=0;igetSoft().c_str()) / 100.0; -// -// vector a(alignmentLength, 0); -// vector t(alignmentLength, 0); -// vector g(alignmentLength, 0); -// vector c(alignmentLength, 0); -// vector x(alignmentLength, 0); -// -// for(int i=0;iget(i).getAligned();; -// -// for(int j=0;jinputFileName) + "filter"; - openOutputFile(filterFile, outfile); + filter = F.getFilter(); - outfile << filter << endl; - outfile.close(); + ofstream outFilter; + string filterFile = getRootName(fastafile) + "filter"; + openOutputFile(filterFile, outFilter); + outFilter << filter << endl; + outFilter.close(); - string filteredFasta = getRootName(globaldata->inputFileName) + "filter.fasta"; - openOutputFile(filteredFasta, outfile); + ifstream inFasta2; + openInputFile(fastafile, inFasta2); + string filteredFasta = getRootName(fastafile) + "filter.fasta"; + ofstream outFASTA; + openOutputFile(filteredFasta, outFASTA); - for(int i=0;iget(i).getAligned(); - outfile << '>' << db->get(i).getName() << endl; + numSeqs = 0; + while(!inFasta2.eof()){ + Sequence seq(inFasta2); + string align = seq.getAligned(); + string filterSeq = ""; + for(int j=0;j' << seq.getName() << endl << filterSeq << endl; + numSeqs++; + gobble(inFasta2); } - outfile.close(); + outFASTA.close(); + inFasta2.close(); + int filteredLength = 0; for(int i=0;iclear(); + mothurOutEndLine(); + mothurOut("Length of filtered alignment: " + toString(filteredLength)); mothurOutEndLine(); + mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); mothurOutEndLine(); + mothurOut("Length of the original alignment: " + toString(alignmentLength)); mothurOutEndLine(); + mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); mothurOutEndLine(); return 0; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FilterSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FilterSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "FilterSeqsCommand", "execute"); exit(1); } }