]> git.donarmstrong.com Git - mothur.git/blob - chimeraseqscommand.cpp
added sorted parameter to get.oturep, added error checking to chimera classes in...
[mothur.git] / chimeraseqscommand.cpp
1 /*
2  *  chimeraseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/29/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "chimeraseqscommand.h"
11 #include "bellerophon.h"
12 #include "pintail.h"
13 #include "ccode.h"
14 #include "chimeracheckrdp.h"
15 #include "chimeraslayer.h"
16
17
18 //***************************************************************************************************************
19
20 ChimeraSeqsCommand::ChimeraSeqsCommand(string option){
21         try {
22                 abort = false;
23                 
24                 //allow user to run help
25                 if(option == "help") { help(); abort = true; }
26                 
27                 else {
28                         //valid paramters for this command
29                         string Array[] =  {"fasta", "filter", "correction", "processors", "method", "window", "increment", "template", "conservation", "quantile", "mask", "numwanted", "ksize", "svg", "name", "match","mismatch", "divergence", "minsim", "parents", "iters" };
30                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
31                         
32                         OptionParser parser(option);
33                         map<string,string> parameters = parser.getParameters();
34                         
35                         ValidParameters validParameter;
36                         
37                         //check to make sure all parameters are valid for command
38                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
39                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
40                         }
41                         
42                         //check for required parameters
43                         fastafile = validParameter.validFile(parameters, "fasta", true);
44                         if (fastafile == "not open") { abort = true; }
45                         else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the chimera.seqs command."); mothurOutEndLine(); abort = true;  }     
46                         
47                         templatefile = validParameter.validFile(parameters, "template", true);
48                         if (templatefile == "not open") { abort = true; }
49                         else if (templatefile == "not found") { templatefile = "";  }   
50                         
51                         consfile = validParameter.validFile(parameters, "conservation", true);
52                         if (consfile == "not open") { abort = true; }
53                         else if (consfile == "not found") { consfile = "";  }   
54                         
55                         quanfile = validParameter.validFile(parameters, "quantile", true);
56                         if (quanfile == "not open") { abort = true; }
57                         else if (quanfile == "not found") { quanfile = "";  }
58                         
59                         namefile = validParameter.validFile(parameters, "name", true);
60                         if (namefile == "not open") { abort = true; }
61                         else if (namefile == "not found") { namefile = "";  }
62
63                         maskfile = validParameter.validFile(parameters, "mask", false);
64                         if (maskfile == "not found") { maskfile = "";  }        
65                         else if (maskfile != "default")  { 
66                                 ifstream in;
67                                 int     ableToOpen = openInputFile(maskfile, in);
68                                 if (ableToOpen == 1) { abort = true; }
69                                 in.close();
70                         }
71                         
72                         method = validParameter.validFile(parameters, "method", false);                 if (method == "not found") { method = "pintail"; }
73                         
74                         string temp;
75                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
76                         filter = isTrue(temp);
77                         
78                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
79                         correction = isTrue(temp);
80                         
81                         temp = validParameter.validFile(parameters, "processors", false);               if (temp == "not found") { temp = "1"; }
82                         convert(temp, processors);
83                         
84                         temp = validParameter.validFile(parameters, "ksize", false);                    if (temp == "not found") { temp = "7"; }
85                         convert(temp, ksize);
86                         
87                         temp = validParameter.validFile(parameters, "svg", false);                              if (temp == "not found") { temp = "F"; }
88                         svg = isTrue(temp);
89                         
90                         temp = validParameter.validFile(parameters, "window", false);   
91                         if ((temp == "not found") && (method == "chimeraslayer")) { temp = "100"; }                     
92                         else if (temp == "not found") { temp = "0"; }
93                         convert(temp, window);
94                         
95                         temp = validParameter.validFile(parameters, "match", false);                    if (temp == "not found") { temp = "5"; }
96                         convert(temp, match);
97                         
98                         temp = validParameter.validFile(parameters, "mismatch", false);                 if (temp == "not found") { temp = "-4"; }
99                         convert(temp, mismatch);
100                         
101                         temp = validParameter.validFile(parameters, "divergence", false);               if (temp == "not found") { temp = "1.0"; }
102                         convert(temp, divR);
103                         
104                         temp = validParameter.validFile(parameters, "minsim", false);                   if (temp == "not found") { temp = "90"; }
105                         convert(temp, minSimilarity);
106                         
107                         temp = validParameter.validFile(parameters, "parents", false);                  if (temp == "not found") { temp = "5"; }
108                         convert(temp, parents); 
109                         
110                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
111                         convert(temp, iters); 
112                          
113                         temp = validParameter.validFile(parameters, "increment", false);                
114                         if ((temp == "not found") && ((method == "chimeracheck") || (method == "chimeraslayer"))) { temp = "10"; }
115                         else if (temp == "not found") { temp = "25"; }
116                         convert(temp, increment);
117                         
118                         temp = validParameter.validFile(parameters, "numwanted", false);
119                         if ((temp == "not found") && (method == "chimeraslayer")) { temp = "10"; }              
120                         else if (temp == "not found") { temp = "20"; }
121                         convert(temp, numwanted);
122
123                         
124                         
125                         if (((method != "bellerophon")) && (templatefile == "")) { mothurOut("You must provide a template file with the pintail, ccode, chimeraslayer or chimeracheck methods."); mothurOutEndLine(); abort = true;  }
126                         
127
128                 }
129         }
130         catch(exception& e) {
131                 errorOut(e, "ChimeraSeqsCommand", "ChimeraSeqsCommand");
132                 exit(1);
133         }
134 }
135 //**********************************************************************************************************************
136
137 void ChimeraSeqsCommand::help(){
138         try {
139         
140                 //"fasta", "filter", "correction", "processors", "method", "window", "increment", "template", "conservation", "quantile", "mask", "numwanted", "ksize", "svg", "name"
141                 //mothurOut("chimera.seqs ASSUMES that your sequences are ALIGNED and if using a template that the template file sequences are the same length as the fasta file sequences.\n\n");
142                 mothurOut("The chimera.seqs command reads a fastafile and creates list of potentially chimeric sequences.\n");
143                 mothurOut("The chimera.seqs command parameters are fasta, filter, correction, processors, mask, method, window, increment, template, conservation, quantile, numwanted, ksize, svg, name, iters.\n");
144                 mothurOut("The fasta parameter is always required and template is required if using pintail, ccode or chimeracheck.\n");
145                 mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter. \n");
146                 mothurOut("The correction parameter allows you to put more emphasis on the distance between highly similar sequences and less emphasis on the differences between remote homologs.\n");
147                 mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
148                 mothurOut("The method parameter allows you to specify the method for finding chimeric sequences.  The default is pintail. Options include bellerophon, ccode and chimeracheck \n");
149                 mothurOut("The mask parameter allows you to specify a file containing one sequence you wish to use as a mask for the your sequences. \n");
150                 mothurOut("The window parameter allows you to specify the window size for searching for chimeras. \n");
151                 mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences.\n");
152                 mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences. \n");
153                 mothurOut("The conservation parameter allows you to enter a frequency file containing the highest bases frequency at each place in the alignment.\n");
154                 mothurOut("The quantile parameter allows you to enter a file containing quantiles for a template files sequences.\n");
155                 mothurOut("The numwanted parameter allows you to specify how many sequences you would each query sequence compared with.\n");
156                 mothurOut("The ksize parameter allows you to input kmersize. \n");
157                 mothurOut("The svg parameter allows you to specify whether or not you would like a svg file outputted for each query sequence.\n");
158                 mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
159                 //mothurOut("The iters parameter allows you to specify the number of bootstrap iters to do with the chimeraslayer method.\n");
160                 mothurOut("NOT ALL PARAMETERS ARE USED BY ALL METHODS. Please look below for method specifics.\n\n");
161                 mothurOut("Details for each method: \n"); 
162                 mothurOut("\tpintail: \n"); 
163                 mothurOut("\t\tparameters: fasta=required, template=required, filter=F, mask=no mask, processors=1, window=300, increment=25, conservation=not required, but will improve speed, quantile=not required, but will greatly improve speed. \n"); 
164                 mothurOut("\t\tIf you have run chimera.seqs using pintail a .quan and .freq file will be created for your template, if you have not provided them for use in future command executions.\n");
165                 mothurOut("\tbellerophon: \n"); 
166                 mothurOut("\t\tparameters: fasta=required, filter=F, processors=1, window=1/4 length of seq, increment=25, correction=T. \n"); 
167                 mothurOut("\tccode: \n"); 
168                 mothurOut("\t\tparameters: fasta=required, template=required, filter=F, mask=no mask, processors=1, window=10% of length, numwanted=20\n"); 
169                 mothurOut("\tchimeracheck: \n"); 
170                 mothurOut("\t\tparameters: fasta=required, template=required, processors=1, increment=10, ksize=7, svg=F, name=none\n\n"); 
171                 //mothurOut("\tchimeraslayer: \n"); 
172                 //mothurOut("\t\tparameters: fasta=required, template=required, processors=1, increment=10, mask=no mask, numwanted=10, match=5, mismatch=-4, divergence=1.0, minsim=90, parents=5, iters=1000, window=100. \n\n"); 
173                 mothurOut("The chimera.seqs command should be in the following format: \n");
174                 mothurOut("chimera.seqs(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors, method=bellerophon) \n");
175                 mothurOut("Example: chimera.seqs(fasta=AD.align, filter=True, correction=true, method=bellerophon, window=200) \n");
176                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");        
177         }
178         catch(exception& e) {
179                 errorOut(e, "ChimeraSeqsCommand", "help");
180                 exit(1);
181         }
182 }
183
184 //***************************************************************************************************************
185
186 ChimeraSeqsCommand::~ChimeraSeqsCommand(){      /*      do nothing      */      }
187
188 //***************************************************************************************************************
189
190 int ChimeraSeqsCommand::execute(){
191         try{
192                 
193                 if (abort == true) { return 0; }
194                 
195                 if (method == "bellerophon")                    {               chimera = new Bellerophon(fastafile);                                           }
196                 else if (method == "pintail")                   {               chimera = new Pintail(fastafile, templatefile);                         }
197                 else if (method == "ccode")                             {               chimera = new Ccode(fastafile, templatefile);                           }
198                 else if (method == "chimeracheck")              {               chimera = new ChimeraCheckRDP(fastafile, templatefile);         }
199                 //else if (method == "chimeraslayer")           {               chimera = new ChimeraSlayer(fastafile, templatefile);           }
200                 else { mothurOut("Not a valid method."); mothurOutEndLine(); return 0;          }
201                 
202                 //set user options
203                 if (maskfile == "default") { mothurOut("I am using the default 236627 EU009184.1 Shigella dysenteriae str. FBD013."); mothurOutEndLine();  }
204                 
205                 //saves time to avoid generating it
206                 chimera->setCons(consfile);     
207                 
208                 //saves time to avoid generating it
209                 chimera->setQuantiles(quanfile);                                
210                 
211                 chimera->setMask(maskfile);
212                 chimera->setFilter(filter);
213                 chimera->setCorrection(correction);
214                 chimera->setProcessors(processors);
215                 chimera->setWindow(window);
216                 chimera->setIncrement(increment);
217                 chimera->setNumWanted(numwanted);
218                 chimera->setKmerSize(ksize);
219                 chimera->setSVG(svg);
220                 chimera->setName(namefile);
221                 chimera->setMatch(match);
222                 chimera->setMisMatch(mismatch);
223                 chimera->setDivR(divR);
224                 chimera->setParents(parents);
225                 chimera->setMinSim(minSimilarity);
226                 chimera->setIters(iters);
227                 
228                                 
229                 //find chimeras
230                 int error = chimera->getChimeras();
231                 
232                 //there was a problem
233                 if (error == 1) {  return 0;  }
234                 
235                 string outputFileName = getRootName(fastafile) + method + maskfile + ".chimeras";
236                 ofstream out;
237                 openOutputFile(outputFileName, out);
238                 
239                 //print results
240                 chimera->print(out);
241                 
242                 out.close();
243                 
244                 delete chimera;
245                 
246                 return 0;
247                 
248         }
249         catch(exception& e) {
250                 errorOut(e, "ChimeraSeqsCommand", "execute");
251                 exit(1);
252         }
253 }
254 /**************************************************************************************************/
255