From b9456b1087ade74251792e039f4e5021dbf32349 Mon Sep 17 00:00:00 2001 From: westcott Date: Wed, 8 Jul 2009 14:50:50 +0000 Subject: [PATCH] added get.seqs command --- Mothur.xcodeproj/project.pbxproj | 6 + commandfactory.cpp | 3 + getseqscommand.cpp | 326 +++++++++++++++++++++++++++++++ getseqscommand.h | 38 ++++ 4 files changed, 373 insertions(+) create mode 100644 getseqscommand.cpp create mode 100644 getseqscommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index f5bc8ad..af71efc 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -77,6 +77,7 @@ 37AFC71F0F445386005F492D /* sharedsobscollectsummary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37AFC71E0F445386005F492D /* sharedsobscollectsummary.cpp */; }; 37B28F680F27590100808A62 /* deconvolutecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B28F670F27590100808A62 /* deconvolutecommand.cpp */; }; 37B73C761004BEFD008C4B41 /* listseqscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73C751004BEFD008C4B41 /* listseqscommand.cpp */; }; + 37B73CA61004D89A008C4B41 /* getseqscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73CA51004D89A008C4B41 /* getseqscommand.cpp */; }; 37C1D9730F86506E0059E3F0 /* binsequencecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37C1D9720F86506E0059E3F0 /* binsequencecommand.cpp */; }; 37C753CE0FB3415200DBD02E /* distancecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37C753CD0FB3415200DBD02E /* distancecommand.cpp */; }; 37D928550F21331F001D4494 /* ace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D927B80F21331F001D4494 /* ace.cpp */; }; @@ -321,6 +322,8 @@ 37B28F670F27590100808A62 /* deconvolutecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = deconvolutecommand.cpp; sourceTree = SOURCE_ROOT; }; 37B73C741004BEFD008C4B41 /* listseqscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = listseqscommand.h; sourceTree = ""; }; 37B73C751004BEFD008C4B41 /* listseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = listseqscommand.cpp; sourceTree = ""; }; + 37B73CA41004D89A008C4B41 /* getseqscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getseqscommand.h; sourceTree = ""; }; + 37B73CA51004D89A008C4B41 /* getseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getseqscommand.cpp; sourceTree = ""; }; 37C1D9710F86506E0059E3F0 /* binsequencecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = binsequencecommand.h; sourceTree = SOURCE_ROOT; }; 37C1D9720F86506E0059E3F0 /* binsequencecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = binsequencecommand.cpp; sourceTree = SOURCE_ROOT; }; 37C753CC0FB3415200DBD02E /* distancecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = distancecommand.h; sourceTree = SOURCE_ROOT; }; @@ -783,6 +786,8 @@ 3749273E0FD5956B0031C06B /* getrabundcommand.cpp */, 3749271B0FD58C840031C06B /* getsabundcommand.h */, 3749271C0FD58C840031C06B /* getsabundcommand.cpp */, + 37B73CA41004D89A008C4B41 /* getseqscommand.h */, + 37B73CA51004D89A008C4B41 /* getseqscommand.cpp */, 375873F10F7D64800040F377 /* heatmapcommand.h */, 375873F00F7D64800040F377 /* heatmapcommand.cpp */, 378598640FDD497000EF9D03 /* heatmapsimcommand.h */, @@ -1117,6 +1122,7 @@ 7E5A17AF0FE57292003C6A03 /* mergefilecommand.cpp in Sources */, 379D3D510FF90E090068C1C0 /* chimeraseqscommand.cpp in Sources */, 37B73C761004BEFD008C4B41 /* listseqscommand.cpp in Sources */, + 37B73CA61004D89A008C4B41 /* getseqscommand.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/commandfactory.cpp b/commandfactory.cpp index 3d90269..2783aa3 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -51,6 +51,7 @@ #include "mergefilecommand.h" #include "chimeraseqscommand.h" #include "listseqscommand.h" +#include "getseqscommand.h" /***********************************************************/ @@ -100,6 +101,7 @@ CommandFactory::CommandFactory(){ commands["trim.seqs"] = "trim.seqs"; commands["chimera.seqs"] = "chimera.seqs"; commands["list.seqs"] = "list.seqs"; + commands["get.seqs"] = "get.seqs"; commands["quit"] = "quit"; } @@ -158,6 +160,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "trim.seqs") { command = new TrimSeqsCommand(optionString); } else if(commandName == "chimera.seqs") { command = new ChimeraSeqsCommand(optionString); } else if(commandName == "list.seqs") { command = new ListSeqsCommand(optionString); } + else if(commandName == "get.seqs") { command = new GetSeqsCommand(optionString); } else if(commandName == "merge.files") { command = new MergeFileCommand(optionString); } else { command = new NoCommand(optionString); } diff --git a/getseqscommand.cpp b/getseqscommand.cpp new file mode 100644 index 0000000..e58cc88 --- /dev/null +++ b/getseqscommand.cpp @@ -0,0 +1,326 @@ +/* + * getseqscommand.cpp + * Mothur + * + * Created by Sarah Westcott on 7/8/09. + * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. + * + */ + +#include "getseqscommand.h" +#include "sequence.hpp" + +//********************************************************************************************************************** + +GetSeqsCommand::GetSeqsCommand(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","name", "group", "align", "accnos" }; + 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 + accnosfile = validParameter.validFile(parameters, "accnos", true); + if (accnosfile == "not open") { abort = true; } + else if (accnosfile == "not found") { accnosfile = ""; mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; } + + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not open") { abort = true; } + else if (fastafile == "not found") { fastafile = ""; } + + namefile = validParameter.validFile(parameters, "name", true); + if (namefile == "not open") { abort = true; } + else if (namefile == "not found") { namefile = ""; } + + groupfile = validParameter.validFile(parameters, "group", true); + if (groupfile == "not open") { abort = true; } + else if (groupfile == "not found") { groupfile = ""; } + + alignfile = validParameter.validFile(parameters, "align", true); + if (alignfile == "not open") { abort = true; } + else if (alignfile == "not found") { alignfile = ""; } + + if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true; } + + if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "GetSeqsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void GetSeqsCommand::help(){ + try { + mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group or alignreport file.\n"); + mothurOut("It outputs a file containing only the sequences in the .accnos file.\n"); + mothurOut("The get.seqs command parameters are accnos, fasta, name, group and align. You must provide accnos and one of the other parameters.\n"); + mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n"); + mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n"); + mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +int GetSeqsCommand::execute(){ + try { + + if (abort == true) { return 0; } + + //get names you want to keep + readAccnos(); + + //read through the correct file and output lines you want to keep + if (fastafile != "") { readFasta(); } + else if (namefile != "") { readName(); } + else if (groupfile != "") { readGroup(); } + else if (alignfile != "") { readAlign(); } + + return 0; + } + + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "execute"); + exit(1); + } +} + +//********************************************************************************************************************** +void GetSeqsCommand::readFasta(){ + try { + string outputFileName = getRootName(fastafile) + "pick"; + ofstream out; + openOutputFile(outputFileName, out); + + ifstream in; + openInputFile(fastafile, in); + string name; + + while(!in.eof()){ + Sequence currSeq(in); + name = currSeq.getName(); + + //if this name is in the accnos file + if (names.count(name) == 1) { + currSeq.printSequence(out); + } + + gobble(in); + } + in.close(); + out.close(); + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "readFasta"); + exit(1); + } +} + +//********************************************************************************************************************** +void GetSeqsCommand::readName(){ + try { + + string outputFileName = getRootName(namefile) + "pick"; + ofstream out; + openOutputFile(outputFileName, out); + + ifstream in; + openInputFile(namefile, in); + string name, firstCol, secondCol; + + + while(!in.eof()){ + + in >> firstCol; + in >> secondCol; + + vector parsedNames; + //parse second column saving each name + while (secondCol.find_first_of(',') != -1) { + name = secondCol.substr(0,secondCol.find_first_of(',')); + secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + parsedNames.push_back(name); + } + + //get name after last , + parsedNames.push_back(secondCol); + + vector validSecond; + for (int i = 0; i < parsedNames.size(); i++) { + if (names.count(parsedNames[i]) == 1) { + validSecond.push_back(parsedNames[i]); + } + } + + + //if the name in the first column is in the set then print it and any other names in second column also in set + if (names.count(firstCol) == 1) { + out << firstCol << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + + //make first name in set you come to first column and then add the remaining names to second column + }else { + //you want part of this row + if (validSecond.size() != 0) { + out << validSecond[0] << '\t'; + + //you know you have at least one valid second since first column is valid + for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; } + out << validSecond[validSecond.size()-1] << endl; + } + } + + gobble(in); + } + in.close(); + out.close(); + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "readName"); + exit(1); + } +} + +//********************************************************************************************************************** +void GetSeqsCommand::readGroup(){ + try { + + string outputFileName = getRootName(groupfile) + "pick"; + ofstream out; + openOutputFile(outputFileName, out); + + ifstream in; + openInputFile(groupfile, in); + string name, group; + + while(!in.eof()){ + + in >> name; //read from first column + in >> group; //read from second column + + //if this name is in the accnos file + if (names.count(name) == 1) { + out << name << '\t' << group << endl; + } + + gobble(in); + } + in.close(); + out.close(); + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "readGroup"); + exit(1); + } +} + +//********************************************************************************************************************** +//alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name +void GetSeqsCommand::readAlign(){ + try { + string outputFileName = getRootName(alignfile) + "pick"; + ofstream out; + openOutputFile(outputFileName, out); + + ifstream in; + openInputFile(alignfile, in); + string name, junk; + + //read column headers + for (int i = 0; i < 16; i++) { + if (!in.eof()) { in >> junk; out << junk << '\t'; } + else { break; } + } + out << endl; + + while(!in.eof()){ + + in >> name; //read from first column + + //if this name is in the accnos file + if (names.count(name) == 1) { + + out << name << '\t'; + + //read rest + for (int i = 0; i < 15; i++) { + if (!in.eof()) { in >> junk; out << junk << '\t'; } + else { break; } + } + out << endl; + + }else {//still read just don't do anything with it + //read rest + for (int i = 0; i < 15; i++) { + if (!in.eof()) { in >> junk; } + else { break; } + } + } + + gobble(in); + } + in.close(); + out.close(); + + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "readAlign"); + exit(1); + } +} +//********************************************************************************************************************** +void GetSeqsCommand::readAccnos(){ + try { + + ifstream in; + openInputFile(accnosfile, in); + string name; + + while(!in.eof()){ + in >> name; + + names.insert(name); + + gobble(in); + } + in.close(); + + } + catch(exception& e) { + errorOut(e, "GetSeqsCommand", "readAccnos"); + exit(1); + } +} + +//********************************************************************************************************************** + diff --git a/getseqscommand.h b/getseqscommand.h new file mode 100644 index 0000000..47f56f7 --- /dev/null +++ b/getseqscommand.h @@ -0,0 +1,38 @@ +#ifndef GETSEQSCOMMAND_H +#define GETSEQSCOMMAND_H + +/* + * getseqscommand.h + * Mothur + * + * Created by Sarah Westcott on 7/8/09. + * Copyright 2009 __MyCompanyName__. All rights reserved. + * + */ + +#include "command.hpp" + +class GetSeqsCommand : public Command { + + public: + + GetSeqsCommand(string); + ~GetSeqsCommand(){}; + int execute(); + void help(); + + private: + set names; + string accnosfile, fastafile, namefile, groupfile, alignfile; + bool abort; + + void readFasta(); + void readName(); + void readGroup(); + void readAlign(); + void readAccnos(); + +}; + +#endif + -- 2.39.2