5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "listseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
15 ListSeqsCommand::ListSeqsCommand(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" };
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 fastafile = validParameter.validFile(parameters, "fasta", true);
39 if (fastafile == "not open") { abort = true; }
40 else if (fastafile == "not found") { fastafile = ""; }
42 namefile = validParameter.validFile(parameters, "name", true);
43 if (namefile == "not open") { abort = true; }
44 else if (namefile == "not found") { namefile = ""; }
46 groupfile = validParameter.validFile(parameters, "group", true);
47 if (groupfile == "not open") { abort = true; }
48 else if (groupfile == "not found") { groupfile = ""; }
50 alignfile = validParameter.validFile(parameters, "alignreport", true);
51 if (alignfile == "not open") { abort = true; }
52 else if (alignfile == "not found") { alignfile = ""; }
54 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide a file."); mothurOutEndLine(); abort = true; }
56 if (parameters.size() > 1) { mothurOut("You may only enter one file."); mothurOutEndLine(); abort = true; }
61 errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
65 //**********************************************************************************************************************
67 void ListSeqsCommand::help(){
69 mothurOut("The list.seqs command reads a fasta, name, group or alignreport file and outputs a .accnos file containing sequence names.\n");
70 mothurOut("The list.seqs command parameters are fasta, name, group and alignreport. You must provide one of these parameters.\n");
71 mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
72 mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
73 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
76 errorOut(e, "ListSeqsCommand", "help");
81 //**********************************************************************************************************************
83 int ListSeqsCommand::execute(){
86 if (abort == true) { return 0; }
88 //read functions fill names vector
89 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
90 else if (namefile != "") { inputFileName = namefile; readName(); }
91 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
92 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
94 //sort in alphabetical order
95 sort(names.begin(), names.end());
97 string outputFileName = getRootName(inputFileName) + "accnos";
99 openOutputFile(outputFileName, out);
101 //output to .accnos file
102 for (int i = 0; i < names.size(); i++) {
103 out << names[i] << endl;
110 catch(exception& e) {
111 errorOut(e, "ListSeqsCommand", "execute");
116 //**********************************************************************************************************************
117 void ListSeqsCommand::readFasta(){
121 openInputFile(fastafile, in);
125 Sequence currSeq(in);
126 name = currSeq.getName();
128 names.push_back(name);
135 catch(exception& e) {
136 errorOut(e, "ListSeqsCommand", "readFasta");
141 //**********************************************************************************************************************
142 void ListSeqsCommand::readName(){
146 openInputFile(namefile, in);
147 string name, firstCol, secondCol;
154 //parse second column saving each name
155 while (secondCol.find_first_of(',') != -1) {
156 name = secondCol.substr(0,secondCol.find_first_of(','));
157 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
158 names.push_back(name);
161 //get name after last ,
162 names.push_back(secondCol);
169 catch(exception& e) {
170 errorOut(e, "ListSeqsCommand", "readName");
175 //**********************************************************************************************************************
176 void ListSeqsCommand::readGroup(){
180 openInputFile(groupfile, in);
185 in >> name; //read from first column
186 in >> group; //read from second column
188 names.push_back(name);
195 catch(exception& e) {
196 errorOut(e, "ListSeqsCommand", "readGroup");
201 //**********************************************************************************************************************
202 //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
203 void ListSeqsCommand::readAlign(){
207 openInputFile(alignfile, in);
210 //read column headers
211 for (int i = 0; i < 16; i++) {
212 if (!in.eof()) { in >> junk; }
219 in >> name; //read from first column
222 for (int i = 0; i < 15; i++) {
223 if (!in.eof()) { in >> junk; }
227 names.push_back(name);
235 catch(exception& e) {
236 errorOut(e, "ListSeqsCommand", "readAlign");
240 //**********************************************************************************************************************