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