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