]> git.donarmstrong.com Git - mothur.git/blob - clusterfragmentscommand.cpp
added getCommandInfoCommand for gui
[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 #include "needlemanoverlap.hpp"
12
13 //**********************************************************************************************************************
14 //sort by unaligned
15 inline bool comparePriority(seqRNode first, seqRNode second) {  
16         bool better = false;
17         
18         if (first.length > second.length) { 
19                 better = true;
20         }else if (first.length == second.length) {
21                 if (first.numIdentical > second.numIdentical) {
22                         better = true;
23                 }
24         }
25         
26         return better; 
27 }
28 //**********************************************************************************************************************
29 vector<string> ClusterFragmentsCommand::setParameters(){        
30         try {
31                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
32                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
33                 CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs);
34                 CommandParameter ppercent("percent", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppercent);
35                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
36                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
37                 
38                 vector<string> myArray;
39                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "ClusterFragmentsCommand", "setParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 string ClusterFragmentsCommand::getHelpString(){        
49         try {
50                 string helpString = "";
51                 helpString += "The cluster.fragments command groups sequences that are part of a larger sequence.\n";
52                 helpString += "The cluster.fragments command outputs a new fasta and name file.\n";
53                 helpString += "The cluster.fragments command parameters are fasta, name, diffs and percent. The fasta parameter is required, unless you have a valid current file. \n";
54                 helpString += "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";
55                 helpString += "The diffs parameter allows you to set the number of differences allowed, default=0. \n";
56                 helpString += "The percent parameter allows you to set percentage of differences allowed, default=0. percent=2 means if the number of difference is less than or equal to two percent of the length of the fragment, then cluster.\n";
57                 helpString += "You may use diffs and percent at the same time to say something like: If the number or differences is greater than 1 or more than 2% of the fragment length, don't merge. \n";
58                 helpString += "The cluster.fragments command should be in the following format: \n";
59                 helpString += "cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n";
60                 helpString += "Example cluster.fragments(fasta=amazon.fasta).\n";
61                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
62                 return helpString;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "ClusterFragmentsCommand", "getHelpString");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70 ClusterFragmentsCommand::ClusterFragmentsCommand(){     
71         try {
72                 abort = true; calledHelp = true; 
73                 setParameters();
74                 vector<string> tempOutNames;
75                 outputTypes["fasta"] = tempOutNames;
76                 outputTypes["name"] = tempOutNames;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84 ClusterFragmentsCommand::ClusterFragmentsCommand(string option) {
85         try {
86                 abort = false; calledHelp = false;   
87                 
88                 //allow user to run help
89                 if(option == "help") { help(); abort = true; calledHelp = true; }
90                 
91                 else {
92                         vector<string> myArray = setParameters();
93                         
94                         OptionParser parser(option);
95                         map<string, string> parameters = parser.getParameters();
96                         
97                         ValidParameters validParameter;
98                         map<string, string>::iterator it;
99                 
100                         //check to make sure all parameters are valid for command
101                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
102                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
103                         }
104                         
105                         //initialize outputTypes
106                         vector<string> tempOutNames;
107                         outputTypes["fasta"] = tempOutNames;
108                         outputTypes["name"] = tempOutNames;
109                         
110                         //if the user changes the input directory command factory will send this info to us in the output parameter 
111                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
112                         if (inputDir == "not found"){   inputDir = "";          }
113                         else {
114                                 string path;
115                                 it = parameters.find("fasta");
116                                 //user has given a template file
117                                 if(it != parameters.end()){ 
118                                         path = m->hasPath(it->second);
119                                         //if the user has not given a path then, add inputdir. else leave path alone.
120                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
121                                 }
122                                 
123                                 it = parameters.find("name");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
129                                 }
130                         }
131
132                         //check for required parameters
133                         fastafile = validParameter.validFile(parameters, "fasta", true);
134                         if (fastafile == "not found") {                                 
135                                 fastafile = m->getFastaFile(); 
136                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
137                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
138                         }
139                         else if (fastafile == "not open") { fastafile = ""; abort = true; }     
140                         
141                         //if the user changes the output directory command factory will send this info to us in the output parameter 
142                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);      }
143
144                         //check for optional parameter and set defaults
145                         // ...at some point should added some additional type checking...
146                         namefile = validParameter.validFile(parameters, "name", true);
147                         if (namefile == "not found") { namefile =  "";  }
148                         else if (namefile == "not open") { abort = true; }      
149                         else {  readNameFile();  }
150                         
151                         string temp;
152                         temp = validParameter.validFile(parameters, "diffs", false);            if (temp == "not found"){       temp = "0";                             }
153                         convert(temp, diffs); 
154                         
155                         temp = validParameter.validFile(parameters, "percent", false);          if (temp == "not found"){       temp = "0";                             }
156                         convert(temp, percent);
157                         
158                 }
159                                 
160         }
161         catch(exception& e) {
162                 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
163                 exit(1);
164         }
165 }
166 //**********************************************************************************************************************
167 int ClusterFragmentsCommand::execute(){
168         try {
169                 
170                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
171                 
172                 int start = time(NULL);
173                 
174                 //reads fasta file and return number of seqs
175                 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
176                 
177                 if (m->control_pressed) { return 0; }
178         
179                 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
180                 
181                 //sort seqs by length of unaligned sequence
182                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
183         
184                 int count = 0;
185
186                 //think about running through twice...
187                 for (int i = 0; i < numSeqs; i++) {
188                         
189                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
190                                 
191                                 string iBases = alignSeqs[i].seq.getUnaligned();
192                                 
193                                 //try to merge it with all smaller seqs
194                                 for (int j = i+1; j < numSeqs; j++) {
195                                         
196                                         if (m->control_pressed) { return 0; }
197                                         
198                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
199                                                 
200                                                 string jBases = alignSeqs[j].seq.getUnaligned();
201                                                                                                         
202                                                 if (isFragment(iBases, jBases)) {
203                                                         //merge
204                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
205                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
206
207                                                         alignSeqs[j].active = 0;
208                                                         alignSeqs[j].numIdentical = 0;
209                                                         count++;
210                                                 }
211                                         }//end if j active
212                                 }//end if i != j
213                         
214                                 //remove from active list 
215                                 alignSeqs[i].active = 0;
216                                 
217                         }//end if active i
218                         if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
219                 }
220                 
221                 if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }
222         
223                 
224                 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
225                 
226                 string newFastaFile = fileroot + "fragclust.fasta";
227                 string newNamesFile = fileroot + "names";
228                 
229                 if (m->control_pressed) { return 0; }
230                 
231                 m->mothurOutEndLine();
232                 m->mothurOut("Total number of sequences before cluster.fragments was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
233                 m->mothurOut("cluster.fragments removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
234                 
235                 printData(newFastaFile, newNamesFile);
236                 
237                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
238                 
239                 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
240                 
241                 m->mothurOutEndLine();
242                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
243                 m->mothurOut(newFastaFile); m->mothurOutEndLine();      
244                 m->mothurOut(newNamesFile); m->mothurOutEndLine();      
245                 outputNames.push_back(newFastaFile);  outputNames.push_back(newNamesFile); outputTypes["fasta"].push_back(newFastaFile); outputTypes["name"].push_back(newNamesFile);
246                 m->mothurOutEndLine();
247                 
248                 //set fasta file as new current fastafile
249                 string current = "";
250                 itTypes = outputTypes.find("fasta");
251                 if (itTypes != outputTypes.end()) {
252                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
253                 }
254                 
255                 itTypes = outputTypes.find("name");
256                 if (itTypes != outputTypes.end()) {
257                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
258                 }
259
260                 return 0;
261                 
262         }
263         catch(exception& e) {
264                 m->errorOut(e, "ClusterFragmentsCommand", "execute");
265                 exit(1);
266         }
267 }
268 //***************************************************************************************************************
269 bool ClusterFragmentsCommand::isFragment(string seq1, string seq2){
270         try {
271                 bool fragment = false;
272                 
273                 //exact match
274                 int pos = seq1.find(seq2);
275                 if (pos != string::npos) { return true; }
276                 //no match, no diffs wanted
277                 else if ((diffs == 0) && (percent == 0)) { return false; }
278                 else { //try aligning and see if you can find it
279                         
280                         //find number of acceptable differences for this sequence fragment
281                         int totalDiffs;
282                         if (diffs == 0) { //you didnt set diffs you want a percentage
283                                 totalDiffs = floor((seq2.length() * (percent / 100.0)));
284                         }else if (percent == 0) { //you didn't set percent you want diffs
285                                 totalDiffs = diffs;
286                         }else if ((percent != 0) && (diffs != 0)) { //you want both, set total diffs to smaller of 2
287                                 totalDiffs = diffs;
288                                 int percentDiff = floor((seq2.length() * (percent / 100.0)));
289                                 if (percentDiff < totalDiffs) { totalDiffs = percentDiff; }
290                         }
291                         
292                         Alignment* alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (seq1.length()+totalDiffs+1));
293                                                         
294                         //use needleman to align 
295                         alignment->align(seq2, seq1);
296                         string tempSeq2 = alignment->getSeqAAln();
297                         string temp = alignment->getSeqBAln();
298                         
299                         delete alignment;
300                         
301                         //chop gap ends
302                         int startPos = 0;
303                         int endPos = tempSeq2.length()-1;
304                         for (int i = 0; i < tempSeq2.length(); i++) {  if (isalpha(tempSeq2[i])) { startPos = i; break; } }
305                         for (int i = tempSeq2.length()-1; i >= 0; i--) {  if (isalpha(tempSeq2[i])) { endPos = i; break; } }
306                         
307                         //count number of diffs
308                         int numDiffs = 0;
309                         for (int i = startPos; i <= endPos; i++) {
310                                 if (tempSeq2[i] != temp[i]) { numDiffs++; }
311                         }
312                         
313                         if (numDiffs <= totalDiffs) { fragment = true; }
314                         
315                 }
316                 
317                 return fragment;
318                 
319         }
320         catch(exception& e) {
321                 m->errorOut(e, "ClusterFragmentsCommand", "isFragment");
322                 exit(1);
323         }
324 }
325 /**************************************************************************************************/
326 int ClusterFragmentsCommand::readFASTA(){
327         try {
328         
329                 ifstream inFasta;
330                 m->openInputFile(fastafile, inFasta);
331                 
332                 while (!inFasta.eof()) {
333                         
334                         if (m->control_pressed) { inFasta.close(); return 0; }
335                         
336                         Sequence seq(inFasta);  m->gobble(inFasta);
337                         
338                         if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
339                                 if (namefile != "") {
340                                         itSize = sizes.find(seq.getName());
341                                         
342                                         if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
343                                         else{
344                                                 seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length());
345                                                 alignSeqs.push_back(tempNode);
346                                         }       
347                                 }else { //no names file, you are identical to yourself 
348                                         seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length());
349                                         alignSeqs.push_back(tempNode);
350                                 }
351                         }
352                 }
353                 
354                 inFasta.close();
355                 return alignSeqs.size();
356         }
357         
358         catch(exception& e) {
359                 m->errorOut(e, "ClusterFragmentsCommand", "readFASTA");
360                 exit(1);
361         }
362 }
363 /**************************************************************************************************/
364 void ClusterFragmentsCommand::printData(string newfasta, string newname){
365         try {
366                 ofstream outFasta;
367                 ofstream outNames;
368                 
369                 m->openOutputFile(newfasta, outFasta);
370                 m->openOutputFile(newname, outNames);
371                 
372                 for (int i = 0; i < alignSeqs.size(); i++) {
373                         if (alignSeqs[i].numIdentical != 0) {
374                                 alignSeqs[i].seq.printSequence(outFasta); 
375                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
376                         }
377                 }
378                 
379                 outFasta.close();
380                 outNames.close();
381         }
382         catch(exception& e) {
383                 m->errorOut(e, "ClusterFragmentsCommand", "printData");
384                 exit(1);
385         }
386 }
387 /**************************************************************************************************/
388
389 void ClusterFragmentsCommand::readNameFile(){
390         try {
391                 ifstream in;
392                 m->openInputFile(namefile, in);
393                 string firstCol, secondCol;
394                                 
395                 while (!in.eof()) {
396                         in >> firstCol >> secondCol; m->gobble(in);
397                         names[firstCol] = secondCol;
398                         int size = 1;
399
400                         for(int i=0;i<secondCol.size();i++){
401                                 if(secondCol[i] == ','){        size++; }
402                         }
403                         sizes[firstCol] = size;
404                 }
405                 in.close();
406         }
407         catch(exception& e) {
408                 m->errorOut(e, "ClusterFragmentsCommand", "readNameFile");
409                 exit(1);
410         }
411 }
412
413 /**************************************************************************************************/
414