]> git.donarmstrong.com Git - mothur.git/blob - screenseqscommand.cpp
modified sequence class to read fasta files with comments. this required modification...
[mothur.git] / screenseqscommand.cpp
1 /*
2  *  screenseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 6/3/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "screenseqscommand.h"
11 #include "sequence.hpp"
12
13 //***************************************************************************************************************
14
15 ScreenSeqsCommand::ScreenSeqsCommand(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 AlignArray[] =  {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength",
25                                                                         "name", "group", "alignreport"};
26                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //check for required parameters
39                         fastafile = validParameter.validFile(parameters, "fasta", true);
40                         if (fastafile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; }
41                         else if (fastafile == "not open") { abort = true; }     
42         
43                         groupfile = validParameter.validFile(parameters, "group", true);
44                         if (groupfile == "not open") { abort = true; }  
45                         else if (groupfile == "not found") { groupfile = ""; }
46                         
47                         namefile = validParameter.validFile(parameters, "name", true);
48                         if (namefile == "not open") { abort = true; }
49                         else if (namefile == "not found") { namefile = ""; }    
50
51                         alignreport = validParameter.validFile(parameters, "alignreport", true);
52                         if (alignreport == "not open") { abort = true; }
53                         else if (alignreport == "not found") { alignreport = ""; }      
54                         
55                         //check for optional parameter and set defaults
56                         // ...at some point should added some additional type checking...
57                         string temp;
58                         temp = validParameter.validFile(parameters, "start", false);            if (temp == "not found") { temp = "-1"; }
59                         convert(temp, startPos); 
60                 
61                         temp = validParameter.validFile(parameters, "end", false);                      if (temp == "not found") { temp = "-1"; }
62                         convert(temp, endPos);  
63
64                         temp = validParameter.validFile(parameters, "maxambig", false);         if (temp == "not found") { temp = "-1"; }
65                         convert(temp, maxAmbig);  
66
67                         temp = validParameter.validFile(parameters, "maxhomop", false);         if (temp == "not found") { temp = "-1"; }
68                         convert(temp, maxHomoP);  
69
70                         temp = validParameter.validFile(parameters, "minlength", false);        if (temp == "not found") { temp = "-1"; }
71                         convert(temp, minLength); 
72                         
73                         temp = validParameter.validFile(parameters, "maxlength", false);        if (temp == "not found") { temp = "-1"; }
74                         convert(temp, maxLength); 
75                 }
76
77         }
78         catch(exception& e) {
79                 errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 void ScreenSeqsCommand::help(){
86         try {
87                 mothurOut("The screen.seqs command reads a fastafile and creates .....\n");
88                 mothurOut("The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, and group.\n");
89                 mothurOut("The fasta parameter is required.\n");
90                 mothurOut("The start parameter .... The default is -1.\n");
91                 mothurOut("The end parameter .... The default is -1.\n");
92                 mothurOut("The maxambig parameter .... The default is -1.\n");
93                 mothurOut("The maxhomop parameter .... The default is -1.\n");
94                 mothurOut("The minlength parameter .... The default is -1.\n");
95                 mothurOut("The maxlength parameter .... The default is -1.\n");
96                 mothurOut("The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n");
97                 mothurOut("The screen.seqs command should be in the following format: \n");
98                 mothurOut("screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig,  \n");
99                 mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  \n");       
100                 mothurOut("Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
101                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
102
103         }
104         catch(exception& e) {
105                 errorOut(e, "ScreenSeqsCommand", "help");
106                 exit(1);
107         }
108 }
109
110 //***************************************************************************************************************
111
112 ScreenSeqsCommand::~ScreenSeqsCommand(){        /*      do nothing      */      }
113
114 //***************************************************************************************************************
115
116 int ScreenSeqsCommand::execute(){
117         try{
118                 
119                 if (abort == true) { return 0; }
120                                 
121                 ifstream inFASTA;
122                 openInputFile(fastafile, inFASTA);
123                 
124                 set<string> badSeqNames;
125                 
126                 string goodSeqFile = getRootName(fastafile) + "good" + getExtension(fastafile);
127                 string badSeqFile = getRootName(fastafile) + "bad" + getExtension(fastafile);
128                 
129                 ofstream goodSeqOut;    openOutputFile(goodSeqFile, goodSeqOut);
130                 ofstream badSeqOut;             openOutputFile(badSeqFile, badSeqOut);          
131                 
132                 while(!inFASTA.eof()){
133                         Sequence currSeq(inFASTA);
134                         if (currSeq.getName() != "") {
135                                 bool goodSeq = 1;               //      innocent until proven guilty
136                                 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos())                  {       goodSeq = 0;    }
137                                 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos())                                {       goodSeq = 0;    }
138                                 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases())                {       goodSeq = 0;    }
139                                 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer())   {       goodSeq = 0;    }
140                                 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases())                {       goodSeq = 0;    }
141                                 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases())                {       goodSeq = 0;    }
142                                 
143                                 if(goodSeq == 1){
144                                         currSeq.printSequence(goodSeqOut);      
145                                 }
146                                 else{
147                                         currSeq.printSequence(badSeqOut);       
148                                         badSeqNames.insert(currSeq.getName());
149                                 }
150                         }
151                         gobble(inFASTA);
152                 }       
153                 if(namefile != "" && groupfile != "")   {       screenNameGroupFile(badSeqNames);       }       // this screens both names and groups
154                 else if(groupfile != "")                                {       screenGroupFile(badSeqNames);           }       // this screens just the groups
155                 if(alignreport != "")                                   {       screenAlignReport(badSeqNames);         }
156                 
157                 goodSeqOut.close();
158                 badSeqOut.close();
159                 inFASTA.close();
160                 return 0;
161         }
162         catch(exception& e) {
163                 errorOut(e, "ScreenSeqsCommand", "execute");
164                 exit(1);
165         }
166 }
167
168 //***************************************************************************************************************
169
170 void ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
171
172         ifstream inputNames;
173         openInputFile(namefile, inputNames);
174         set<string> badSeqGroups;
175         string seqName, seqList, group;
176         set<string>::iterator it;
177
178         string goodNameFile = getRootName(namefile) + "good" + getExtension(namefile);
179         string badNameFile = getRootName(namefile) + "bad" + getExtension(namefile);
180         
181         ofstream goodNameOut;   openOutputFile(goodNameFile, goodNameOut);
182         ofstream badNameOut;    openOutputFile(badNameFile, badNameOut);                
183         
184         while(!inputNames.eof()){
185                 inputNames >> seqName >> seqList;
186                 it = badSeqNames.find(seqName);
187                 
188                 if(it != badSeqNames.end()){
189                         badSeqNames.erase(it);
190                         badNameOut << seqName << '\t' << seqList << endl;
191                         if(namefile != ""){
192                                 int start = 0;
193                                 for(int i=0;i<seqList.length();i++){
194                                         if(seqList[i] == ','){
195                                                 badSeqGroups.insert(seqList.substr(start,i-start));
196                                                 start = i+1;
197                                         }                                       
198                                 }
199                                 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
200                         }
201                 }
202                 else{
203                         goodNameOut << seqName << '\t' << seqList << endl;
204                 }
205                 gobble(inputNames);
206         }
207         inputNames.close();
208         goodNameOut.close();
209         badNameOut.close();
210         
211         //we were unable to remove some of the bad sequences
212         if (badSeqNames.size() != 0) {
213                 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {  
214                         mothurOut("Your namefile does not include the sequence " + *it + " please correct."); 
215                         mothurOutEndLine();
216                 }
217         }
218
219         if(groupfile != ""){
220                 
221                 ifstream inputGroups;
222                 openInputFile(groupfile, inputGroups);
223
224                 string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile);
225                 string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile);
226                 
227                 ofstream goodGroupOut;  openOutputFile(goodGroupFile, goodGroupOut);
228                 ofstream badGroupOut;   openOutputFile(badGroupFile, badGroupOut);              
229                 
230                 while(!inputGroups.eof()){
231                         inputGroups >> seqName >> group;
232
233                         it = badSeqGroups.find(seqName);
234                         
235                         if(it != badSeqGroups.end()){
236                                 badSeqGroups.erase(it);
237                                 badGroupOut << seqName << '\t' << group << endl;
238                         }
239                         else{
240                                 goodGroupOut << seqName << '\t' << group << endl;
241                         }
242                         gobble(inputGroups);
243                 }
244                 inputGroups.close();
245                 goodGroupOut.close();
246                 badGroupOut.close();
247                 
248                 //we were unable to remove some of the bad sequences
249                 if (badSeqGroups.size() != 0) {
250                         for (it = badSeqGroups.begin(); it != badSeqGroups.end(); it++) {  
251                                 mothurOut("Your namefile does not include the sequence " + *it + " please correct."); 
252                                 mothurOutEndLine();
253                         }
254                 }
255         }
256 }
257
258 //***************************************************************************************************************
259
260 void ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
261
262         ifstream inputGroups;
263         openInputFile(groupfile, inputGroups);
264         string seqName, group;
265         set<string>::iterator it;
266         
267         string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile);
268         string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile);
269         
270         ofstream goodGroupOut;  openOutputFile(goodGroupFile, goodGroupOut);
271         ofstream badGroupOut;   openOutputFile(badGroupFile, badGroupOut);              
272         
273         while(!inputGroups.eof()){
274                 inputGroups >> seqName >> group;
275                 it = badSeqNames.find(seqName);
276                 
277                 if(it != badSeqNames.end()){
278                         badSeqNames.erase(it);
279                         badGroupOut << seqName << '\t' << group << endl;
280                 }
281                 else{
282                         goodGroupOut << seqName << '\t' << group << endl;
283                 }
284                 gobble(inputGroups);
285         }
286         
287         //we were unable to remove some of the bad sequences
288         if (badSeqNames.size() != 0) {
289                 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {  
290                         mothurOut("Your groupfile does not include the sequence " + *it + " please correct."); 
291                         mothurOutEndLine();
292                 }
293         }
294         
295         inputGroups.close();
296         goodGroupOut.close();
297         badGroupOut.close();
298         
299 }
300
301 //***************************************************************************************************************
302
303 void ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
304         
305         ifstream inputAlignReport;
306         openInputFile(alignreport, inputAlignReport);
307         string seqName, group;
308         set<string>::iterator it;
309         
310         string goodAlignReportFile = getRootName(alignreport) + "good" + getExtension(alignreport);
311         string badAlignReportFile = getRootName(alignreport) + "bad" + getExtension(alignreport);
312         
313         ofstream goodAlignReportOut;    openOutputFile(goodAlignReportFile, goodAlignReportOut);
314         ofstream badAlignReportOut;             openOutputFile(badAlignReportFile, badAlignReportOut);          
315
316         while (!inputAlignReport.eof()) {               //      need to copy header
317                 char c = inputAlignReport.get();
318                 goodAlignReportOut << c;
319                 badAlignReportOut << c;
320                 if (c == 10 || c == 13){        break;  }       
321         }
322
323         while(!inputAlignReport.eof()){
324                 inputAlignReport >> seqName;
325                 it = badSeqNames.find(seqName);
326                 string line;            
327                 while (!inputAlignReport.eof()) {               //      need to copy header
328                         char c = inputAlignReport.get();
329                         line += c;
330                         if (c == 10 || c == 13){        break;  }       
331                 }
332                 
333                 if(it != badSeqNames.end()){
334                         badSeqNames.erase(it);
335                         badAlignReportOut << seqName << '\t' << line;;
336                 }
337                 else{
338                         goodAlignReportOut << seqName << '\t' << line;
339                 }
340                 gobble(inputAlignReport);
341         }
342         
343         //we were unable to remove some of the bad sequences
344         if (badSeqNames.size() != 0) {
345                 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {  
346                         mothurOut("Your file does not include the sequence " + *it + " please correct."); 
347                         mothurOutEndLine();
348                 }
349         }
350
351         inputAlignReport.close();
352         goodAlignReportOut.close();
353         badAlignReportOut.close();
354         
355 }
356
357 //***************************************************************************************************************
358
359