5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
15 GetSeqsCommand::GetSeqsCommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","name", "group", "align", "accnos" };
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //check for required parameters
38 accnosfile = validParameter.validFile(parameters, "accnos", true);
39 if (accnosfile == "not open") { abort = true; }
40 else if (accnosfile == "not found") { accnosfile = ""; mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; }
42 fastafile = validParameter.validFile(parameters, "fasta", true);
43 if (fastafile == "not open") { abort = true; }
44 else if (fastafile == "not found") { fastafile = ""; }
46 namefile = validParameter.validFile(parameters, "name", true);
47 if (namefile == "not open") { abort = true; }
48 else if (namefile == "not found") { namefile = ""; }
50 groupfile = validParameter.validFile(parameters, "group", true);
51 if (groupfile == "not open") { abort = true; }
52 else if (groupfile == "not found") { groupfile = ""; }
54 alignfile = validParameter.validFile(parameters, "align", true);
55 if (alignfile == "not open") { abort = true; }
56 else if (alignfile == "not found") { alignfile = ""; }
58 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true; }
60 if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, align."); mothurOutEndLine(); abort = true; }
65 errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
69 //**********************************************************************************************************************
71 void GetSeqsCommand::help(){
73 mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group or alignreport file.\n");
74 mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
75 mothurOut("The get.seqs command parameters are accnos, fasta, name, group and align. You must provide accnos and one of the other parameters.\n");
76 mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
77 mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
78 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
81 errorOut(e, "GetSeqsCommand", "help");
86 //**********************************************************************************************************************
88 int GetSeqsCommand::execute(){
91 if (abort == true) { return 0; }
93 //get names you want to keep
96 //read through the correct file and output lines you want to keep
97 if (fastafile != "") { readFasta(); }
98 else if (namefile != "") { readName(); }
99 else if (groupfile != "") { readGroup(); }
100 else if (alignfile != "") { readAlign(); }
105 catch(exception& e) {
106 errorOut(e, "GetSeqsCommand", "execute");
111 //**********************************************************************************************************************
112 void GetSeqsCommand::readFasta(){
114 string outputFileName = getRootName(fastafile) + "pick";
116 openOutputFile(outputFileName, out);
119 openInputFile(fastafile, in);
122 bool wroteSomething = false;
125 Sequence currSeq(in);
126 name = currSeq.getName();
128 //if this name is in the accnos file
129 if (names.count(name) == 1) {
130 wroteSomething = true;
132 currSeq.printSequence(out);
142 if (wroteSomething == false) {
143 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
144 remove(outputFileName.c_str());
148 catch(exception& e) {
149 errorOut(e, "GetSeqsCommand", "readFasta");
154 //**********************************************************************************************************************
155 void GetSeqsCommand::readName(){
158 string outputFileName = getRootName(namefile) + "pick";
160 openOutputFile(outputFileName, out);
163 openInputFile(namefile, in);
164 string name, firstCol, secondCol;
166 bool wroteSomething = false;
174 vector<string> parsedNames;
175 //parse second column saving each name
176 while (secondCol.find_first_of(',') != -1) {
177 name = secondCol.substr(0,secondCol.find_first_of(','));
178 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
179 parsedNames.push_back(name);
182 //get name after last ,
183 parsedNames.push_back(secondCol);
185 vector<string> validSecond;
186 for (int i = 0; i < parsedNames.size(); i++) {
187 if (names.count(parsedNames[i]) == 1) {
188 validSecond.push_back(parsedNames[i]);
189 names.erase(parsedNames[i]);
194 //if the name in the first column is in the set then print it and any other names in second column also in set
195 if (names.count(firstCol) == 1) {
197 wroteSomething = true;
199 out << firstCol << '\t';
201 //you know you have at least one valid second since first column is valid
202 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
203 out << validSecond[validSecond.size()-1] << endl;
205 names.erase(firstCol);
207 //make first name in set you come to first column and then add the remaining names to second column
209 //you want part of this row
210 if (validSecond.size() != 0) {
212 wroteSomething = true;
214 out << validSecond[0] << '\t';
216 //you know you have at least one valid second since first column is valid
217 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
218 out << validSecond[validSecond.size()-1] << endl;
227 if (wroteSomething == false) {
228 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
229 remove(outputFileName.c_str());
233 catch(exception& e) {
234 errorOut(e, "GetSeqsCommand", "readName");
239 //**********************************************************************************************************************
240 void GetSeqsCommand::readGroup(){
243 string outputFileName = getRootName(groupfile) + "pick";
245 openOutputFile(outputFileName, out);
248 openInputFile(groupfile, in);
251 bool wroteSomething = false;
255 in >> name; //read from first column
256 in >> group; //read from second column
258 //if this name is in the accnos file
259 if (names.count(name) == 1) {
260 wroteSomething = true;
262 out << name << '\t' << group << endl;
272 if (wroteSomething == false) {
273 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
274 remove(outputFileName.c_str());
278 catch(exception& e) {
279 errorOut(e, "GetSeqsCommand", "readGroup");
284 //**********************************************************************************************************************
285 //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
286 void GetSeqsCommand::readAlign(){
288 string outputFileName = getRootName(alignfile) + "pick";
290 openOutputFile(outputFileName, out);
293 openInputFile(alignfile, in);
296 bool wroteSomething = false;
298 //read column headers
299 for (int i = 0; i < 16; i++) {
300 if (!in.eof()) { in >> junk; out << junk << '\t'; }
307 in >> name; //read from first column
309 //if this name is in the accnos file
310 if (names.count(name) == 1) {
311 wroteSomething = true;
316 for (int i = 0; i < 15; i++) {
317 if (!in.eof()) { in >> junk; out << junk << '\t'; }
324 }else {//still read just don't do anything with it
326 for (int i = 0; i < 15; i++) {
327 if (!in.eof()) { in >> junk; }
337 if (wroteSomething == false) {
338 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
339 remove(outputFileName.c_str());
343 catch(exception& e) {
344 errorOut(e, "GetSeqsCommand", "readAlign");
348 //**********************************************************************************************************************
349 void GetSeqsCommand::readAccnos(){
353 openInputFile(accnosfile, in);
366 catch(exception& e) {
367 errorOut(e, "GetSeqsCommand", "readAccnos");
372 //**********************************************************************************************************************