2 * preclustercommand.cpp
5 * Created by westcott on 12/21/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "preclustercommand.h"
12 //**********************************************************************************************************************
13 inline bool comparePriority(seqPNode first, seqPNode second) { return (first.numIdentical > second.numIdentical); }
14 //**********************************************************************************************************************
16 PreClusterCommand::PreClusterCommand(string option){
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta", "name", "diffs"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
33 //check to make sure all parameters are valid for command
34 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
35 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
38 //check for required parameters
39 fastafile = validParameter.validFile(parameters, "fasta", true);
40 if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; }
41 else if (fastafile == "not open") { abort = true; }
43 //check for optional parameter and set defaults
44 // ...at some point should added some additional type checking...
45 namefile = validParameter.validFile(parameters, "name", true);
47 if (namefile == "not found") { namefile = ""; }
48 else if (namefile == "not open") { abort = true; }
49 // else { readNameFile(); }
51 string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; }
57 errorOut(e, "PreClusterCommand", "PreClusterCommand");
62 //**********************************************************************************************************************
63 PreClusterCommand::~PreClusterCommand(){}
64 //**********************************************************************************************************************
66 void PreClusterCommand::help(){
68 mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
69 mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
70 mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
71 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");
72 mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n");
73 mothurOut("The pre.cluster command should be in the following format: \n");
74 mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
75 mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
76 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
79 errorOut(e, "PreClusterCommand", "help");
83 //**********************************************************************************************************************
85 int PreClusterCommand::execute(){
88 if (abort == true) { return 0; }
90 //reads fasta file and return number of seqs
91 int numSeqs = readNamesFASTA(); //fills alignSeqs and makes all seqs active
93 if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0; }
94 if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0; }
96 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
99 //sort seqs by number of identical seqs
100 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
104 //think about running through twice...
105 for (int i = 0; i < numSeqs; i++) {
108 // itActive = active.find(alignSeqs[i].seq.getName());
110 if (alignSeqs[i].active) { //this sequence has not been merged yet
112 //try to merge it with all smaller seqs
113 for (int j = i+1; j < numSeqs; j++) {
114 if (alignSeqs[j].active) { //this sequence has not been merged yet
115 //are you within "diff" bases
116 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
118 if (mismatch <= diffs) {
120 alignSeqs[i].names += ',' + alignSeqs[j].names;
121 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
123 alignSeqs[j].active = 0;
124 alignSeqs[j].numIdentical = 0;
130 //remove from active list
131 alignSeqs[i].active = 0;
133 if(i % 100 == 0) { cout << i << '\t' << numSeqs - count << '\t' << count << endl; }
136 string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile);
137 string newNamesFile = getRootName(fastafile) + "precluster.names";
140 mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine();
141 mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine();
142 printData(newFastaFile, newNamesFile);
147 catch(exception& e) {
148 errorOut(e, "PreClusterCommand", "execute");
152 /**************************************************************************************************/
153 int PreClusterCommand::readFASTA(){
156 // openInputFile(fastafile, inFasta);
158 //// map<string, string>::iterator it;
160 // while (!inFasta.eof()) {
161 // Sequence temp(inFasta); //read seq
163 // if (temp.getName() != "") {
164 // if (namefile != "") {
165 // //make sure fasta and name files match
166 // it = names.find(temp.getName());
167 // if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
168 // }else { sizes[temp.getName()] = 1; }
170 // seqPNode tempNode(sizes[temp.getName()], temp);
171 // alignSeqs.push_back(tempNode);
172 // active[temp.getName()] = true;
178 // if (alignSeqs.size() != 0) { length = alignSeqs[0].seq.getAligned().length(); }
180 return alignSeqs.size();
182 catch(exception& e) {
183 errorOut(e, "PreClusterCommand", "readFASTA");
187 /**************************************************************************************************/
189 int PreClusterCommand::readNamesFASTA(){
194 openInputFile(namefile, inNames);
195 openInputFile(fastafile, inFasta);
197 string firstCol, secondCol, nameString;
200 while (inFasta && inNames) {
202 inNames >> firstCol >> secondCol;
203 nameString = secondCol;
207 while (secondCol.find_first_of(',') != -1) {
209 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
211 Sequence seq(inFasta);
212 if (seq.getName() != firstCol) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
214 seqPNode tempNode(size, seq, nameString);
215 alignSeqs.push_back(tempNode);
216 if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); }
221 return alignSeqs.size();
224 catch(exception& e) {
225 errorOut(e, "PreClusterCommand", "readNamesFASTA");
230 /**************************************************************************************************/
232 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
236 for (int i = 0; i < seq1.length(); i++) {
238 if (seq1[i] != seq2[i]) { numBad++; }
239 if (numBad > diffs) { return length; } //to far to cluster
244 catch(exception& e) {
245 errorOut(e, "PreClusterCommand", "calcMisMatches");
250 /**************************************************************************************************/
252 void PreClusterCommand::printData(string newfasta, string newname){
257 openOutputFile(newfasta, outFasta);
258 openOutputFile(newname, outNames);
261 for (int i = 0; i < alignSeqs.size(); i++) {
262 if (alignSeqs[i].numIdentical != 0) {
263 alignSeqs[i].seq.printSequence(outFasta);
264 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
272 catch(exception& e) {
273 errorOut(e, "PreClusterCommand", "printData");
278 /**************************************************************************************************/