]> git.donarmstrong.com Git - mothur.git/blob - chimeraseqscommand.cpp
f980310008cb562c8128344e17a477db60801d76
[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
14
15 //***************************************************************************************************************
16
17 ChimeraSeqsCommand::ChimeraSeqsCommand(string option){
18         try {
19                 abort = false;
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         //valid paramters for this command
26                         string Array[] =  {"fasta", "filter", "correction", "processors", "method", "window", "increment", "template", "conservation", "quantile", "mask" };
27                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string,string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
33                         
34                         //check to make sure all parameters are valid for command
35                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         //check for required parameters
40                         fastafile = validParameter.validFile(parameters, "fasta", true);
41                         if (fastafile == "not open") { abort = true; }
42                         else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the chimera.seqs command."); mothurOutEndLine(); abort = true;  }     
43                         
44                         templatefile = validParameter.validFile(parameters, "template", true);
45                         if (templatefile == "not open") { abort = true; }
46                         else if (templatefile == "not found") { templatefile = "";  }   
47                         
48                         consfile = validParameter.validFile(parameters, "conservation", true);
49                         if (consfile == "not open") { abort = true; }
50                         else if (consfile == "not found") { consfile = "";  }   
51                         
52                         quanfile = validParameter.validFile(parameters, "quantile", true);
53                         if (quanfile == "not open") { abort = true; }
54                         else if (quanfile == "not found") { quanfile = "";  }
55                                 
56                         maskfile = validParameter.validFile(parameters, "mask", false);
57                         if (maskfile == "not found") { maskfile = "";  }        
58                         else if (maskfile != "default")  { 
59                                 ifstream in;
60                                 int     ableToOpen = openInputFile(maskfile, in);
61                                 if (ableToOpen == 1) { abort = true; }
62                                 in.close();
63                         }
64                 
65
66                         
67
68                         string temp;
69                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
70                         filter = isTrue(temp);
71                         
72                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
73                         correction = isTrue(temp);
74                         
75                         temp = validParameter.validFile(parameters, "processors", false);               if (temp == "not found") { temp = "1"; }
76                         convert(temp, processors);
77                         
78                         temp = validParameter.validFile(parameters, "window", false);                   if (temp == "not found") { temp = "0"; }
79                         convert(temp, window);
80                                         
81                         temp = validParameter.validFile(parameters, "increment", false);                        if (temp == "not found") { temp = "25"; }
82                         convert(temp, increment);
83                                 
84                         method = validParameter.validFile(parameters, "method", false);         if (method == "not found") { method = "pintail"; }
85                         
86                         if ((method == "pintail") && (templatefile == "")) { mothurOut("You must provide a template file with the pintail method."); mothurOutEndLine(); abort = true;  }
87                         
88
89                 }
90         }
91         catch(exception& e) {
92                 errorOut(e, "ChimeraSeqsCommand", "ChimeraSeqsCommand");
93                 exit(1);
94         }
95 }
96 //**********************************************************************************************************************
97
98 void ChimeraSeqsCommand::help(){
99         try {
100                 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");
101                 mothurOut("The chimera.seqs command reads a fastafile and creates list of potentially chimeric sequences.\n");
102                 mothurOut("The chimera.seqs command parameters are fasta, filter, correction, processors, mask, method, window, increment, template, conservation and quantile.\n");
103                 mothurOut("The fasta parameter is always required and template is required if using pintail.\n");
104                 mothurOut("The filter parameter allows you to specify if you would like to apply a 50% soft filter this only applies when the method is bellerphon.  The default is false. \n");
105                 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.   The default is true. This only applies when the method is bellerphon.\n");
106                 mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
107                 mothurOut("The method parameter allows you to specify the method for finding chimeric sequences.  The default is pintail. Options include..... \n");
108                 mothurOut("The mask parameter allows you to specify a file containing one sequence you wish to use as a mask for the pintail.  The default is no mask.  If you enter mask=default, then the mask is 236627 EU009184.1 Shigella dysenteriae str. FBD013. \n");
109                 mothurOut("The window parameter allows you to specify the window size for searching for chimeras.  The default is 300 is method is pintail unless the sequence length is less than 300, and 1/4 sequence length for bellerphon.\n");
110                 mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences.  The default is 25.\n");
111                 mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences for use by the pintail algorythm. It is a required parameter if using pintail.\n");
112                 mothurOut("The conservation parameter allows you to enter a frequency file containing the highest bases frequency at each place in the alignment for use by the pintail algorythm. It is not required, but will speed up the pintail method.\n");
113                 mothurOut("The quantile parameter allows you to enter a file containing quantiles for a template files sequences for use by the pintail algorythm. It is not required, but will speed up the pintail method.\n");
114                 mothurOut("If you have run chimera.seqs using pintail a .quan and .freq file will be created if you have not provided them for use in future command executions.");
115                 mothurOut("The chimera.seqs command should be in the following format: \n");
116                 mothurOut("chimera.seqs(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors, method=bellerophon) \n");
117                 mothurOut("Example: chimera.seqs(fasta=AD.align, filter=True, correction=true, processors=2, method=yourMethod) \n");
118                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");        
119         }
120         catch(exception& e) {
121                 errorOut(e, "ChimeraSeqsCommand", "help");
122                 exit(1);
123         }
124 }
125
126 //***************************************************************************************************************
127
128 ChimeraSeqsCommand::~ChimeraSeqsCommand(){      /*      do nothing      */      }
129
130 //***************************************************************************************************************
131
132 int ChimeraSeqsCommand::execute(){
133         try{
134                 
135                 if (abort == true) { return 0; }
136                 
137                 if (method == "bellerophon")    {               chimera = new Bellerophon(fastafile);                   }
138                 else if (method == "pintail")   {               chimera = new Pintail(fastafile, templatefile); }
139                 else { mothurOut("Not a valid method."); mothurOutEndLine(); return 0;          }
140                 
141                 //set user options
142                 if (maskfile == "default") { mothurOut("I am using the default 236627 EU009184.1 Shigella dysenteriae str. FBD013."); mothurOutEndLine();  }
143                 
144                 //saves time to avoid generating it
145                 if (consfile != "")                     {               chimera->setCons(consfile);                                             }
146                 else                                            {               chimera->setCons("");                                                   }
147                 
148                 //saves time to avoid generating it
149                 if (quanfile != "")                     {               chimera->setQuantiles(quanfile);                                }
150                 else                                            {               chimera->setQuantiles("");                                              }
151                 
152                 chimera->setMask(maskfile);
153                 chimera->setFilter(filter);
154                 chimera->setCorrection(correction);
155                 chimera->setProcessors(processors);
156                 chimera->setWindow(window);
157                 chimera->setIncrement(increment);
158                                 
159                 //find chimeras
160                 chimera->getChimeras();
161                 
162                 string outputFileName = getRootName(fastafile) + method + maskfile + ".chimeras";
163                 ofstream out;
164                 openOutputFile(outputFileName, out);
165                 
166                 //print results
167                 chimera->print(out);
168                 
169                 out.close();
170                 
171                 delete chimera;
172                 
173                 return 0;
174                 
175         }
176         catch(exception& e) {
177                 errorOut(e, "ChimeraSeqsCommand", "execute");
178                 exit(1);
179         }
180 }
181 /**************************************************************************************************/
182