]> git.donarmstrong.com Git - mothur.git/blob - screenseqscommand.cpp
added an alignreport option to screen seqs
[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") { cout << "fasta is a required parameter for the screen.seqs command." << endl; 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") { namefile = ""; } 
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                 cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function ScreenSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80                 exit(1);
81         }
82         catch(...) {
83                 cout << "An unknown error has occurred in the ScreenSeqsCommand class function ScreenSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }       
86 }
87 //**********************************************************************************************************************
88
89 void ScreenSeqsCommand::help(){
90         try {
91                 cout << "The screen.seqs command reads a fastafile and creates ....." << "\n";
92                 cout << "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, and group." << "\n";
93                 cout << "The fasta parameter is required." << "\n";
94                 cout << "The start parameter .... The default is -1." << "\n";
95                 cout << "The end parameter .... The default is -1." << "\n";
96                 cout << "The maxambig parameter .... The default is -1." << "\n";
97                 cout << "The maxhomop parameter .... The default is -1." << "\n";
98                 cout << "The minlength parameter .... The default is -1." << "\n";
99                 cout << "The maxlength parameter .... The default is -1." << "\n";
100                 cout << "The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile." << "\n";
101                 cout << "The screen.seqs command should be in the following format: " << "\n";
102                 cout << "screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig,  " << "\n";
103                 cout << "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength)  " << "\n";    
104                 cout << "Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...)." << "\n";
105                 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta)." << "\n" << "\n";
106
107         }
108         catch(exception& e) {
109                 cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110                 exit(1);
111         }
112         catch(...) {
113                 cout << "An unknown error has occurred in the ScreenSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
114                 exit(1);
115         }       
116 }
117
118 //***************************************************************************************************************
119
120 ScreenSeqsCommand::~ScreenSeqsCommand(){        /*      do nothing      */      }
121
122 //***************************************************************************************************************
123
124 int ScreenSeqsCommand::execute(){
125         try{
126                 
127                 if (abort == true) { return 0; }
128                                 
129                 ifstream inFASTA;
130                 openInputFile(fastafile, inFASTA);
131                 
132                 set<string> badSeqNames;
133                 
134                 string goodSeqFile = getRootName(fastafile) + "good" + getExtension(fastafile);
135                 string badSeqFile = getRootName(fastafile) + "bad" + getExtension(fastafile);
136                 
137                 ofstream goodSeqOut;    openOutputFile(goodSeqFile, goodSeqOut);
138                 ofstream badSeqOut;             openOutputFile(badSeqFile, badSeqOut);          
139                 
140                 while(!inFASTA.eof()){
141                         Sequence currSeq(inFASTA);
142                         bool goodSeq = 1;               //      innocent until proven guilty
143                         if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos())                  {       goodSeq = 0;    }
144                         if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos())                                {       goodSeq = 0;    }
145                         if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases())                {       goodSeq = 0;    }
146                         if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer())   {       goodSeq = 0;    }
147                         if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases())                {       goodSeq = 0;    }
148                         if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases())                {       goodSeq = 0;    }
149                         
150                         if(goodSeq == 1){
151                                 currSeq.printSequence(goodSeqOut);      
152                         }
153                         else{
154                                 currSeq.printSequence(badSeqOut);       
155                                 badSeqNames.insert(currSeq.getName());
156                         }
157                         gobble(inFASTA);
158                 }       
159                 if(namefile != "")              {       screenNameGroupFile(badSeqNames);       }
160                 if(groupfile != "")             {       screenGroupFile(badSeqNames);           }
161                 if(alignreport != "")   {       screenAlignReport(badSeqNames);         }
162                 
163                 return 0;
164         }
165         catch(exception& e) {
166                 cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
167                 exit(1);
168         }
169         catch(...) {
170                 cout << "An unknown error has occurred in the ScreenSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171                 exit(1);
172         }
173         
174 }
175
176 //***************************************************************************************************************
177
178 void ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
179
180         ifstream inputNames;
181         openInputFile(namefile, inputNames);
182         set<string> badSeqGroups;
183         string seqName, seqList, group;
184         set<string>::iterator it;
185
186         string goodNameFile = getRootName(namefile) + "good" + getExtension(namefile);
187         string badNameFile = getRootName(namefile) + "bad" + getExtension(namefile);
188         
189         ofstream goodNameOut;   openOutputFile(goodNameFile, goodNameOut);
190         ofstream badNameOut;    openOutputFile(badNameFile, badNameOut);                
191         
192         while(!inputNames.eof()){
193                 inputNames >> seqName >> seqList;
194                 it = badSeqNames.find(seqName);
195                 
196                 if(it != badSeqNames.end()){
197                         badSeqNames.erase(it);
198                         badNameOut << seqName << '\t' << seqList << endl;
199                         if(namefile != ""){
200                                 int start = 0;
201                                 for(int i=0;i<seqList.length();i++){
202                                         if(seqList[i] == ','){
203                                                 badSeqGroups.insert(seqList.substr(start,i-start));
204                                                 start = i+1;
205                                         }                                       
206                                 }
207                                 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
208                         }
209                 }
210                 else{
211                         goodNameOut << seqName << '\t' << seqList << endl;
212                 }
213                 gobble(inputNames);
214         }
215         inputNames.close();
216         goodNameOut.close();
217         badNameOut.close();
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 }
249
250 //***************************************************************************************************************
251
252 void ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
253
254         ifstream inputGroups;
255         openInputFile(groupfile, inputGroups);
256         string seqName, group;
257         set<string>::iterator it;
258         
259         string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile);
260         string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile);
261         
262         ofstream goodGroupOut;  openOutputFile(goodGroupFile, goodGroupOut);
263         ofstream badGroupOut;   openOutputFile(badGroupFile, badGroupOut);              
264         
265         while(!inputGroups.eof()){
266                 inputGroups >> seqName >> group;
267                 it = badSeqNames.find(seqName);
268                 
269                 if(it != badSeqNames.end()){
270                         badSeqNames.erase(it);
271                         badGroupOut << seqName << '\t' << group << endl;
272                 }
273                 else{
274                         goodGroupOut << seqName << '\t' << group << endl;
275                 }
276                 gobble(inputGroups);
277         }
278         inputGroups.close();
279         goodGroupOut.close();
280         badGroupOut.close();
281         
282 }
283
284 //***************************************************************************************************************
285
286 void ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
287         
288         ifstream inputAlignReport;
289         openInputFile(alignreport, inputAlignReport);
290         string seqName, group;
291         set<string>::iterator it;
292         
293         string goodAlignReportFile = getRootName(alignreport) + "good" + getExtension(alignreport);
294         string badAlignReportFile = getRootName(alignreport) + "bad" + getExtension(alignreport);
295         
296         ofstream goodAlignReportOut;    openOutputFile(goodAlignReportFile, goodAlignReportOut);
297         ofstream badAlignReportOut;             openOutputFile(badAlignReportFile, badAlignReportOut);          
298
299         while (!inputAlignReport.eof()) {               //      need to copy header
300                 char c = inputAlignReport.get();
301                 goodAlignReportOut << c;
302                 badAlignReportOut << c;
303                 if (c == 10 || c == 13){        break;  }       
304         }
305
306         while(!inputAlignReport.eof()){
307                 inputAlignReport >> seqName;
308                 it = badSeqNames.find(seqName);
309                 string line;            
310                 while (!inputAlignReport.eof()) {               //      need to copy header
311                         char c = inputAlignReport.get();
312                         line += c;
313                         if (c == 10 || c == 13){        break;  }       
314                 }
315                 
316                 if(it != badSeqNames.end()){
317                         badSeqNames.erase(it);
318                         badAlignReportOut << seqName << '\t' << line;;
319                 }
320                 else{
321                         goodAlignReportOut << seqName << '\t' << line;
322                 }
323                 gobble(inputAlignReport);
324         }
325         inputAlignReport.close();
326         goodAlignReportOut.close();
327         badAlignReportOut.close();
328         
329 }
330
331 //***************************************************************************************************************
332
333