]> git.donarmstrong.com Git - mothur.git/blob - clusterfragmentscommand.cpp
added pipeline commands which involved change to command factory and command class...
[mothur.git] / clusterfragmentscommand.cpp
1 /*
2  *  ryanscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/23/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "clusterfragmentscommand.h"
11
12 //**********************************************************************************************************************
13 //sort by unaligned
14 inline bool comparePriority(seqRNode first, seqRNode second) {  
15         bool better = false;
16         
17         if (first.length > second.length) { 
18                 better = true;
19         }else if (first.length == second.length) {
20                 if (first.numIdentical > second.numIdentical) {
21                         better = true;
22                 }
23         }
24         
25         return better; 
26 }
27 //**********************************************************************************************************************
28 vector<string> ClusterFragmentsCommand::getValidParameters(){   
29         try {
30                 string AlignArray[] =  {"fasta","name","outputdir","inputdir"};
31                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
32                 return myArray;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "ClusterFragmentsCommand", "getValidParameters");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 ClusterFragmentsCommand::ClusterFragmentsCommand(){     
41         try {
42                 //initialize outputTypes
43                 vector<string> tempOutNames;
44                 outputTypes["fasta"] = tempOutNames;
45                 outputTypes["name"] = tempOutNames;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 vector<string> ClusterFragmentsCommand::getRequiredParameters(){        
54         try {
55                 string Array[] =  {"fasta"};
56                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
57                 return myArray;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredParameters");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 vector<string> ClusterFragmentsCommand::getRequiredFiles(){     
66         try {
67                 vector<string> myArray;
68                 return myArray;
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredFiles");
72                 exit(1);
73         }
74 }
75 //**********************************************************************************************************************
76 ClusterFragmentsCommand::ClusterFragmentsCommand(string option) {
77         try {
78                 abort = false;
79                 
80                 //allow user to run help
81                 if(option == "help") { help(); abort = true; }
82                 
83                 else {
84                         //valid paramters for this command
85                         string Array[] =  {"fasta","name","outputdir","inputdir"};
86                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
87                         
88                         OptionParser parser(option);
89                         map<string, string> parameters = parser.getParameters();
90                         
91                         ValidParameters validParameter;
92                         map<string, string>::iterator it;
93                 
94                         //check to make sure all parameters are valid for command
95                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
96                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
97                         }
98                         
99                         //initialize outputTypes
100                         vector<string> tempOutNames;
101                         outputTypes["fasta"] = tempOutNames;
102                         outputTypes["name"] = tempOutNames;
103                         
104                         //if the user changes the input directory command factory will send this info to us in the output parameter 
105                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
106                         if (inputDir == "not found"){   inputDir = "";          }
107                         else {
108                                 string path;
109                                 it = parameters.find("fasta");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
115                                 }
116                                 
117                                 it = parameters.find("name");
118                                 //user has given a template file
119                                 if(it != parameters.end()){ 
120                                         path = m->hasPath(it->second);
121                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
123                                 }
124                         }
125
126                         //check for required parameters
127                         fastafile = validParameter.validFile(parameters, "fasta", true);
128                         if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the cluster.fragments command."); m->mothurOutEndLine(); abort = true; }
129                         else if (fastafile == "not open") { abort = true; }     
130                         
131                         //if the user changes the output directory command factory will send this info to us in the output parameter 
132                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);      }
133
134                         //check for optional parameter and set defaults
135                         // ...at some point should added some additional type checking...
136                         namefile = validParameter.validFile(parameters, "name", true);
137                         if (namefile == "not found") { namefile =  "";  }
138                         else if (namefile == "not open") { abort = true; }      
139                         else {  readNameFile();  }
140                 }
141                                 
142         }
143         catch(exception& e) {
144                 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
145                 exit(1);
146         }
147 }
148
149 //**********************************************************************************************************************
150 ClusterFragmentsCommand::~ClusterFragmentsCommand(){}   
151 //**********************************************************************************************************************
152 void ClusterFragmentsCommand::help(){
153         try {
154                 m->mothurOut("The cluster.fragments command groups sequences that are part of a larger sequence.\n");
155                 m->mothurOut("The cluster.fragments command outputs a new fasta and name file.\n");
156                 m->mothurOut("The cluster.fragments command parameters are fasta and name. The fasta parameter is required. \n");
157                 m->mothurOut("The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n");
158                 m->mothurOut("The cluster.fragments command should be in the following format: \n");
159                 m->mothurOut("cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n");
160                 m->mothurOut("Example cluster.fragments(fasta=amazon.fasta).\n");
161                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "ClusterFragmentsCommand", "help");
165                 exit(1);
166         }
167 }
168 //**********************************************************************************************************************
169 int ClusterFragmentsCommand::execute(){
170         try {
171                 
172                 if (abort == true) { return 0; }
173                 
174                 int start = time(NULL);
175                 
176                 //reads fasta file and return number of seqs
177                 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
178                 
179                 if (m->control_pressed) { return 0; }
180         
181                 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
182                 
183                 //sort seqs by length of unaligned sequence
184                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
185         
186                 int count = 0;
187
188                 //think about running through twice...
189                 for (int i = 0; i < numSeqs; i++) {
190                         
191                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
192                                 
193                                 string iBases = alignSeqs[i].seq.getUnaligned();
194                                 
195                                 //try to merge it with all smaller seqs
196                                 for (int j = i+1; j < numSeqs; j++) {
197                                         
198                                         if (m->control_pressed) { return 0; }
199                                         
200                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
201                                                 
202                                                 string jBases = alignSeqs[j].seq.getUnaligned();
203                                                 
204                                                 int pos = iBases.find(jBases);
205                                                                                                 
206                                                 if (pos != string::npos) {
207                                                         //merge
208                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
209                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
210
211                                                         alignSeqs[j].active = 0;
212                                                         alignSeqs[j].numIdentical = 0;
213                                                         count++;
214                                                 }
215                                         }//end if j active
216                                 }//end if i != j
217                         
218                                 //remove from active list 
219                                 alignSeqs[i].active = 0;
220                                 
221                         }//end if active i
222                         if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
223                 }
224                 
225                 if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }
226         
227                 
228                 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
229                 
230                 string newFastaFile = fileroot + "fragclust.fasta";
231                 string newNamesFile = fileroot + "names";
232                 
233                 if (m->control_pressed) { return 0; }
234                 
235                 m->mothurOutEndLine();
236                 m->mothurOut("Total number of sequences before cluster.fragments was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
237                 m->mothurOut("cluster.fragments removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
238                 
239                 printData(newFastaFile, newNamesFile);
240                 
241                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
242                 
243                 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
244                 
245                 m->mothurOutEndLine();
246                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
247                 m->mothurOut(newFastaFile); m->mothurOutEndLine();      
248                 m->mothurOut(newNamesFile); m->mothurOutEndLine();      
249                 outputNames.push_back(newFastaFile);  outputNames.push_back(newNamesFile); outputTypes["fasta"].push_back(newFastaFile); outputTypes["name"].push_back(newNamesFile);
250                 m->mothurOutEndLine();
251
252                 return 0;
253                 
254         }
255         catch(exception& e) {
256                 m->errorOut(e, "ClusterFragmentsCommand", "execute");
257                 exit(1);
258         }
259 }
260
261 /**************************************************************************************************/
262 int ClusterFragmentsCommand::readFASTA(){
263         try {
264         
265                 ifstream inFasta;
266                 m->openInputFile(fastafile, inFasta);
267                 
268                 while (!inFasta.eof()) {
269                         
270                         if (m->control_pressed) { inFasta.close(); return 0; }
271                         
272                         Sequence seq(inFasta);  m->gobble(inFasta);
273                         
274                         if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
275                                 if (namefile != "") {
276                                         itSize = sizes.find(seq.getName());
277                                         
278                                         if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
279                                         else{
280                                                 seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length());
281                                                 alignSeqs.push_back(tempNode);
282                                         }       
283                                 }else { //no names file, you are identical to yourself 
284                                         seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length());
285                                         alignSeqs.push_back(tempNode);
286                                 }
287                         }
288                 }
289                 
290                 inFasta.close();
291                 return alignSeqs.size();
292         }
293         
294         catch(exception& e) {
295                 m->errorOut(e, "ClusterFragmentsCommand", "readFASTA");
296                 exit(1);
297         }
298 }
299 /**************************************************************************************************/
300 void ClusterFragmentsCommand::printData(string newfasta, string newname){
301         try {
302                 ofstream outFasta;
303                 ofstream outNames;
304                 
305                 m->openOutputFile(newfasta, outFasta);
306                 m->openOutputFile(newname, outNames);
307                 
308                 for (int i = 0; i < alignSeqs.size(); i++) {
309                         if (alignSeqs[i].numIdentical != 0) {
310                                 alignSeqs[i].seq.printSequence(outFasta); 
311                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
312                         }
313                 }
314                 
315                 outFasta.close();
316                 outNames.close();
317         }
318         catch(exception& e) {
319                 m->errorOut(e, "ClusterFragmentsCommand", "printData");
320                 exit(1);
321         }
322 }
323 /**************************************************************************************************/
324
325 void ClusterFragmentsCommand::readNameFile(){
326         try {
327                 ifstream in;
328                 m->openInputFile(namefile, in);
329                 string firstCol, secondCol;
330                                 
331                 while (!in.eof()) {
332                         in >> firstCol >> secondCol; m->gobble(in);
333                         names[firstCol] = secondCol;
334                         int size = 1;
335
336                         for(int i=0;i<secondCol.size();i++){
337                                 if(secondCol[i] == ','){        size++; }
338                         }
339                         sizes[firstCol] = size;
340                 }
341                 in.close();
342         }
343         catch(exception& e) {
344                 m->errorOut(e, "ClusterFragmentsCommand", "readNameFile");
345                 exit(1);
346         }
347 }
348
349 /**************************************************************************************************/
350