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", "alignreport", "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, "alignreport", 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, alignreport."); mothurOutEndLine(); abort = true; }
60 if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport."); 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 alignreport. 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" + getExtension(fastafile);
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" + getExtension(namefile);
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]);
193 //if the name in the first column is in the set then print it and any other names in second column also in set
194 if (names.count(firstCol) == 1) {
196 wroteSomething = true;
198 out << firstCol << '\t';
200 //you know you have at least one valid second since first column is valid
201 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
202 out << validSecond[validSecond.size()-1] << endl;
205 //make first name in set you come to first column and then add the remaining names to second column
207 //you want part of this row
208 if (validSecond.size() != 0) {
210 wroteSomething = true;
212 out << validSecond[0] << '\t';
214 //you know you have at least one valid second since first column is valid
215 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
216 out << validSecond[validSecond.size()-1] << endl;
225 if (wroteSomething == false) {
226 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
227 remove(outputFileName.c_str());
231 catch(exception& e) {
232 errorOut(e, "GetSeqsCommand", "readName");
237 //**********************************************************************************************************************
238 void GetSeqsCommand::readGroup(){
241 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
243 openOutputFile(outputFileName, out);
246 openInputFile(groupfile, in);
249 bool wroteSomething = false;
253 in >> name; //read from first column
254 in >> group; //read from second column
256 //if this name is in the accnos file
257 if (names.count(name) == 1) {
258 wroteSomething = true;
260 out << name << '\t' << group << endl;
270 if (wroteSomething == false) {
271 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
272 remove(outputFileName.c_str());
276 catch(exception& e) {
277 errorOut(e, "GetSeqsCommand", "readGroup");
282 //**********************************************************************************************************************
283 //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
284 void GetSeqsCommand::readAlign(){
286 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
288 openOutputFile(outputFileName, out);
291 openInputFile(alignfile, in);
294 bool wroteSomething = false;
296 //read column headers
297 for (int i = 0; i < 16; i++) {
298 if (!in.eof()) { in >> junk; out << junk << '\t'; }
305 in >> name; //read from first column
307 //if this name is in the accnos file
308 if (names.count(name) == 1) {
309 wroteSomething = true;
314 for (int i = 0; i < 15; i++) {
315 if (!in.eof()) { in >> junk; out << junk << '\t'; }
322 }else {//still read just don't do anything with it
324 for (int i = 0; i < 15; i++) {
325 if (!in.eof()) { in >> junk; }
335 if (wroteSomething == false) {
336 mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
337 remove(outputFileName.c_str());
341 catch(exception& e) {
342 errorOut(e, "GetSeqsCommand", "readAlign");
346 //**********************************************************************************************************************
348 void GetSeqsCommand::readAccnos(){
352 openInputFile(accnosfile, in);
365 catch(exception& e) {
366 errorOut(e, "GetSeqsCommand", "readAccnos");
371 //**********************************************************************************************************************