]> git.donarmstrong.com Git - mothur.git/blob - screenseqscommand.cpp
added logfile feature
[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                         bool goodSeq = 1;               //      innocent until proven guilty
135                         if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos())                  {       goodSeq = 0;    }
136                         if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos())                                {       goodSeq = 0;    }
137                         if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases())                {       goodSeq = 0;    }
138                         if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer())   {       goodSeq = 0;    }
139                         if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases())                {       goodSeq = 0;    }
140                         if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases())                {       goodSeq = 0;    }
141                         
142                         if(goodSeq == 1){
143                                 currSeq.printSequence(goodSeqOut);      
144                         }
145                         else{
146                                 currSeq.printSequence(badSeqOut);       
147                                 badSeqNames.insert(currSeq.getName());
148                         }
149                         gobble(inFASTA);
150                 }       
151                 if(namefile != "" && groupfile != "")   {       screenNameGroupFile(badSeqNames);       }       // this screens both names and groups
152                 else if(groupfile != "")                                {       screenGroupFile(badSeqNames);           }       // this screens just the groups
153                 if(alignreport != "")                                   {       screenAlignReport(badSeqNames);         }
154                 
155                 goodSeqOut.close();
156                 badSeqOut.close();
157                 inFASTA.close();
158                 return 0;
159         }
160         catch(exception& e) {
161                 errorOut(e, "ScreenSeqsCommand", "execute");
162                 exit(1);
163         }
164 }
165
166 //***************************************************************************************************************
167
168 void ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
169
170         ifstream inputNames;
171         openInputFile(namefile, inputNames);
172         set<string> badSeqGroups;
173         string seqName, seqList, group;
174         set<string>::iterator it;
175
176         string goodNameFile = getRootName(namefile) + "good" + getExtension(namefile);
177         string badNameFile = getRootName(namefile) + "bad" + getExtension(namefile);
178         
179         ofstream goodNameOut;   openOutputFile(goodNameFile, goodNameOut);
180         ofstream badNameOut;    openOutputFile(badNameFile, badNameOut);                
181         
182         while(!inputNames.eof()){
183                 inputNames >> seqName >> seqList;
184                 it = badSeqNames.find(seqName);
185                 
186                 if(it != badSeqNames.end()){
187                         badSeqNames.erase(it);
188                         badNameOut << seqName << '\t' << seqList << endl;
189                         if(namefile != ""){
190                                 int start = 0;
191                                 for(int i=0;i<seqList.length();i++){
192                                         if(seqList[i] == ','){
193                                                 badSeqGroups.insert(seqList.substr(start,i-start));
194                                                 start = i+1;
195                                         }                                       
196                                 }
197                                 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
198                         }
199                 }
200                 else{
201                         goodNameOut << seqName << '\t' << seqList << endl;
202                 }
203                 gobble(inputNames);
204         }
205         inputNames.close();
206         goodNameOut.close();
207         badNameOut.close();
208         
209         if(groupfile != ""){
210                 
211                 ifstream inputGroups;
212                 openInputFile(groupfile, inputGroups);
213
214                 string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile);
215                 string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile);
216                 
217                 ofstream goodGroupOut;  openOutputFile(goodGroupFile, goodGroupOut);
218                 ofstream badGroupOut;   openOutputFile(badGroupFile, badGroupOut);              
219                 
220                 while(!inputGroups.eof()){
221                         inputGroups >> seqName >> group;
222
223                         it = badSeqGroups.find(seqName);
224                         
225                         if(it != badSeqGroups.end()){
226                                 badSeqGroups.erase(it);
227                                 badGroupOut << seqName << '\t' << group << endl;
228                         }
229                         else{
230                                 goodGroupOut << seqName << '\t' << group << endl;
231                         }
232                         gobble(inputGroups);
233                 }
234                 inputGroups.close();
235                 goodGroupOut.close();
236                 badGroupOut.close();
237         }
238 }
239
240 //***************************************************************************************************************
241
242 void ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
243
244         ifstream inputGroups;
245         openInputFile(groupfile, inputGroups);
246         string seqName, group;
247         set<string>::iterator it;
248         
249         string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile);
250         string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile);
251         
252         ofstream goodGroupOut;  openOutputFile(goodGroupFile, goodGroupOut);
253         ofstream badGroupOut;   openOutputFile(badGroupFile, badGroupOut);              
254         
255         while(!inputGroups.eof()){
256                 inputGroups >> seqName >> group;
257                 it = badSeqNames.find(seqName);
258                 
259                 if(it != badSeqNames.end()){
260                         badSeqNames.erase(it);
261                         badGroupOut << seqName << '\t' << group << endl;
262                 }
263                 else{
264                         goodGroupOut << seqName << '\t' << group << endl;
265                 }
266                 gobble(inputGroups);
267         }
268         inputGroups.close();
269         goodGroupOut.close();
270         badGroupOut.close();
271         
272 }
273
274 //***************************************************************************************************************
275
276 void ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
277         
278         ifstream inputAlignReport;
279         openInputFile(alignreport, inputAlignReport);
280         string seqName, group;
281         set<string>::iterator it;
282         
283         string goodAlignReportFile = getRootName(alignreport) + "good" + getExtension(alignreport);
284         string badAlignReportFile = getRootName(alignreport) + "bad" + getExtension(alignreport);
285         
286         ofstream goodAlignReportOut;    openOutputFile(goodAlignReportFile, goodAlignReportOut);
287         ofstream badAlignReportOut;             openOutputFile(badAlignReportFile, badAlignReportOut);          
288
289         while (!inputAlignReport.eof()) {               //      need to copy header
290                 char c = inputAlignReport.get();
291                 goodAlignReportOut << c;
292                 badAlignReportOut << c;
293                 if (c == 10 || c == 13){        break;  }       
294         }
295
296         while(!inputAlignReport.eof()){
297                 inputAlignReport >> seqName;
298                 it = badSeqNames.find(seqName);
299                 string line;            
300                 while (!inputAlignReport.eof()) {               //      need to copy header
301                         char c = inputAlignReport.get();
302                         line += c;
303                         if (c == 10 || c == 13){        break;  }       
304                 }
305                 
306                 if(it != badSeqNames.end()){
307                         badSeqNames.erase(it);
308                         badAlignReportOut << seqName << '\t' << line;;
309                 }
310                 else{
311                         goodAlignReportOut << seqName << '\t' << line;
312                 }
313                 gobble(inputAlignReport);
314         }
315         inputAlignReport.close();
316         goodAlignReportOut.close();
317         badAlignReportOut.close();
318         
319 }
320
321 //***************************************************************************************************************
322
323