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", "outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string, string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string, string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
36 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 it = parameters.find("name");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["name"] = inputDir + it->second; }
61 //check for required parameters
62 fastafile = validParameter.validFile(parameters, "fasta", true);
63 if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; }
64 else if (fastafile == "not open") { abort = true; }
66 //if the user changes the output directory command factory will send this info to us in the output parameter
67 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
69 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
72 //check for optional parameter and set defaults
73 // ...at some point should added some additional type checking...
74 namefile = validParameter.validFile(parameters, "name", true);
75 if (namefile == "not found") { namefile = ""; }
76 else if (namefile == "not open") { abort = true; }
77 else { readNameFile(); }
79 string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; }
85 errorOut(e, "PreClusterCommand", "PreClusterCommand");
90 //**********************************************************************************************************************
91 PreClusterCommand::~PreClusterCommand(){}
92 //**********************************************************************************************************************
94 void PreClusterCommand::help(){
96 mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
97 mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
98 mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
99 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");
100 mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n");
101 mothurOut("The pre.cluster command should be in the following format: \n");
102 mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
103 mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
104 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
106 catch(exception& e) {
107 errorOut(e, "PreClusterCommand", "help");
111 //**********************************************************************************************************************
113 int PreClusterCommand::execute(){
116 if (abort == true) { return 0; }
118 //reads fasta file and return number of seqs
119 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
121 if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0; }
122 if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0; }
124 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
127 //sort seqs by number of identical seqs
128 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
132 //think about running through twice...
133 for (int i = 0; i < numSeqs; i++) {
136 // itActive = active.find(alignSeqs[i].seq.getName());
138 if (alignSeqs[i].active) { //this sequence has not been merged yet
140 //try to merge it with all smaller seqs
141 for (int j = i+1; j < numSeqs; j++) {
142 if (alignSeqs[j].active) { //this sequence has not been merged yet
143 //are you within "diff" bases
144 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
146 if (mismatch <= diffs) {
148 alignSeqs[i].names += ',' + alignSeqs[j].names;
149 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
151 alignSeqs[j].active = 0;
152 alignSeqs[j].numIdentical = 0;
158 //remove from active list
159 alignSeqs[i].active = 0;
161 if(i % 100 == 0) { cout << i << '\t' << numSeqs - count << '\t' << count << endl; }
164 string fileroot = outputDir + getRootName(getSimpleName(fastafile));
166 string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
167 string newNamesFile = fileroot + "precluster.names";
170 mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine();
171 mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine();
172 printData(newFastaFile, newNamesFile);
177 catch(exception& e) {
178 errorOut(e, "PreClusterCommand", "execute");
182 /**************************************************************************************************
183 int PreClusterCommand::readFASTA(){
186 // openInputFile(fastafile, inFasta);
188 //// map<string, string>::iterator it;
190 // while (!inFasta.eof()) {
191 // Sequence temp(inFasta); //read seq
193 // if (temp.getName() != "") {
194 // if (namefile != "") {
195 // //make sure fasta and name files match
196 // it = names.find(temp.getName());
197 // if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
198 // }else { sizes[temp.getName()] = 1; }
200 // seqPNode tempNode(sizes[temp.getName()], temp);
201 // alignSeqs.push_back(tempNode);
202 // active[temp.getName()] = true;
208 // if (alignSeqs.size() != 0) { length = alignSeqs[0].seq.getAligned().length(); }
210 return alignSeqs.size();
212 catch(exception& e) {
213 errorOut(e, "PreClusterCommand", "readFASTA");
217 /**************************************************************************************************/
218 //this seems to require the names and fasta file to be in the same order???
219 int PreClusterCommand::readFASTA(){
224 //openInputFile(namefile, inNames);
225 openInputFile(fastafile, inFasta);
227 //string firstCol, secondCol, nameString;
230 while (!inFasta.eof()) {
232 //inNames >> firstCol >> secondCol;
233 //nameString = secondCol;
237 //while (secondCol.find_first_of(',') != -1) {
239 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
242 Sequence seq(inFasta); gobble(inFasta);
244 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
245 if (namefile != "") {
246 itSize = sizes.find(seq.getName());
248 if (itSize == sizes.end()) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
250 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
251 alignSeqs.push_back(tempNode);
252 if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); }
254 }else { //no names file, you are identical to yourself
255 seqPNode tempNode(1, seq, seq.getName());
256 alignSeqs.push_back(tempNode);
257 if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); }
263 return alignSeqs.size();
266 catch(exception& e) {
267 errorOut(e, "PreClusterCommand", "readFASTA");
272 /**************************************************************************************************/
274 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
278 for (int i = 0; i < seq1.length(); i++) {
280 if (seq1[i] != seq2[i]) { numBad++; }
281 if (numBad > diffs) { return length; } //to far to cluster
286 catch(exception& e) {
287 errorOut(e, "PreClusterCommand", "calcMisMatches");
292 /**************************************************************************************************/
294 void PreClusterCommand::printData(string newfasta, string newname){
299 openOutputFile(newfasta, outFasta);
300 openOutputFile(newname, outNames);
303 for (int i = 0; i < alignSeqs.size(); i++) {
304 if (alignSeqs[i].numIdentical != 0) {
305 alignSeqs[i].seq.printSequence(outFasta);
306 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
314 catch(exception& e) {
315 errorOut(e, "PreClusterCommand", "printData");
319 /**************************************************************************************************/
320 void PreClusterCommand::readNameFile(){
323 openInputFile(namefile, in);
324 string firstCol, secondCol;
327 in >> firstCol >> secondCol; gobble(in);
328 names[firstCol] = secondCol;
330 while (secondCol.find_first_of(',') != -1) {
332 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
334 sizes[firstCol] = size;
338 catch(exception& e) {
339 errorOut(e, "PreClusterCommand", "readNameFile");
344 /**************************************************************************************************/