]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
pat's changes after generating wiki pages for 1.5
[mothur.git] / listseqscommand.cpp
1 /*
2  *  listseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "listseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14
15 ListSeqsCommand::ListSeqsCommand(string option){
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"fasta","name", "group", "alignreport" };
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         
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;  }
35                         }
36                         
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 = "";  }        
41                         
42                         namefile = validParameter.validFile(parameters, "name", true);
43                         if (namefile == "not open") { abort = true; }
44                         else if (namefile == "not found") {  namefile = "";  }  
45                         
46                         groupfile = validParameter.validFile(parameters, "group", true);
47                         if (groupfile == "not open") { abort = true; }
48                         else if (groupfile == "not found") {  groupfile = "";  }        
49                         
50                         alignfile = validParameter.validFile(parameters, "alignreport", true);
51                         if (alignfile == "not open") { abort = true; }
52                         else if (alignfile == "not found") {  alignfile = "";  }
53                         
54                         if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == ""))  { mothurOut("You must provide a file."); mothurOutEndLine(); abort = true; }
55                         
56                         if (parameters.size() > 1) { mothurOut("You may only enter one file."); mothurOutEndLine(); abort = true;  }
57                 }
58
59         }
60         catch(exception& e) {
61                 errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
62                 exit(1);
63         }
64 }
65 //**********************************************************************************************************************
66
67 void ListSeqsCommand::help(){
68         try {
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");
74         }
75         catch(exception& e) {
76                 errorOut(e, "ListSeqsCommand", "help");
77                 exit(1);
78         }
79 }
80
81 //**********************************************************************************************************************
82
83 int ListSeqsCommand::execute(){
84         try {
85                 
86                 if (abort == true) { return 0; }
87                 
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();    }
93                 
94                 //sort in alphabetical order
95                 sort(names.begin(), names.end());
96                 
97                 string outputFileName = getRootName(inputFileName) + "accnos";
98                 ofstream out;
99                 openOutputFile(outputFileName, out);
100                 
101                 //output to .accnos file
102                 for (int i = 0; i < names.size(); i++) {
103                         out << names[i] << endl;
104                 }
105                 out.close();
106                 
107                 return 0;               
108         }
109
110         catch(exception& e) {
111                 errorOut(e, "ListSeqsCommand", "execute");
112                 exit(1);
113         }
114 }
115
116 //**********************************************************************************************************************
117 void ListSeqsCommand::readFasta(){
118         try {
119                 
120                 ifstream in;
121                 openInputFile(fastafile, in);
122                 string name;
123                 
124                 while(!in.eof()){
125                         Sequence currSeq(in);
126                         name = currSeq.getName();
127                         
128                         names.push_back(name);
129                         
130                         gobble(in);
131                 }
132                 in.close();             
133
134         }
135         catch(exception& e) {
136                 errorOut(e, "ListSeqsCommand", "readFasta");
137                 exit(1);
138         }
139 }
140
141 //**********************************************************************************************************************
142 void ListSeqsCommand::readName(){
143         try {
144                 
145                 ifstream in;
146                 openInputFile(namefile, in);
147                 string name, firstCol, secondCol;
148                 
149                 while(!in.eof()){
150
151                         in >> firstCol;                         
152                         in >> secondCol;                        
153                         
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);
159                         }
160                         
161                         //get name after last ,
162                         names.push_back(secondCol);
163                         
164                         gobble(in);
165                 }
166                 in.close();
167                 
168         }
169         catch(exception& e) {
170                 errorOut(e, "ListSeqsCommand", "readName");
171                 exit(1);
172         }
173 }
174
175 //**********************************************************************************************************************
176 void ListSeqsCommand::readGroup(){
177         try {
178         
179                 ifstream in;
180                 openInputFile(groupfile, in);
181                 string name, group;
182                 
183                 while(!in.eof()){
184
185                         in >> name;                             //read from first column
186                         in >> group;                    //read from second column
187                         
188                         names.push_back(name);
189                                         
190                         gobble(in);
191                 }
192                 in.close();
193
194         }
195         catch(exception& e) {
196                 errorOut(e, "ListSeqsCommand", "readGroup");
197                 exit(1);
198         }
199 }
200
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(){
204         try {
205         
206                 ifstream in;
207                 openInputFile(alignfile, in);
208                 string name, junk;
209                 
210                 //read column headers
211                 for (int i = 0; i < 16; i++) {  
212                         if (!in.eof())  {       in >> junk;             }
213                         else                    {       break;                  }
214                 }
215                 
216                 
217                 while(!in.eof()){
218
219                         in >> name;                             //read from first column
220                         
221                         //read rest
222                         for (int i = 0; i < 15; i++) {  
223                                 if (!in.eof())  {       in >> junk;             }
224                                 else                    {       break;                  }
225                         }
226                         
227                         names.push_back(name);
228                                         
229                         gobble(in);
230                 }
231                 in.close();
232
233                 
234         }
235         catch(exception& e) {
236                 errorOut(e, "ListSeqsCommand", "readAlign");
237                 exit(1);
238         }
239 }
240 //**********************************************************************************************************************