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 = m->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 = m->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") { m->mothurOut("fasta is a required parameter for the pre.cluster command."); m->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 += m->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 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
90 //**********************************************************************************************************************
91 PreClusterCommand::~PreClusterCommand(){}
92 //**********************************************************************************************************************
94 void PreClusterCommand::help(){
96 m->mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
97 m->mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
98 m->mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
99 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");
100 m->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 m->mothurOut("The pre.cluster command should be in the following format: \n");
102 m->mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
103 m->mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
104 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
106 catch(exception& e) {
107 m->errorOut(e, "PreClusterCommand", "help");
111 //**********************************************************************************************************************
113 int PreClusterCommand::execute(){
116 if (abort == true) { return 0; }
118 int start = time(NULL);
120 //reads fasta file and return number of seqs
121 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
123 if (m->control_pressed) { return 0; }
125 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
126 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; }
128 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
131 //sort seqs by number of identical seqs
132 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
136 //think about running through twice...
137 for (int i = 0; i < numSeqs; i++) {
140 // itActive = active.find(alignSeqs[i].seq.getName());
142 if (alignSeqs[i].active) { //this sequence has not been merged yet
144 //try to merge it with all smaller seqs
145 for (int j = i+1; j < numSeqs; j++) {
147 if (m->control_pressed) { return 0; }
149 if (alignSeqs[j].active) { //this sequence has not been merged yet
150 //are you within "diff" bases
151 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
153 if (mismatch <= diffs) {
155 alignSeqs[i].names += ',' + alignSeqs[j].names;
156 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
158 alignSeqs[j].active = 0;
159 alignSeqs[j].numIdentical = 0;
165 //remove from active list
166 alignSeqs[i].active = 0;
169 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
172 if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
175 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
177 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
178 string newNamesFile = fileroot + "precluster.names";
180 if (m->control_pressed) { return 0; }
182 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
183 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
184 printData(newFastaFile, newNamesFile);
186 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
188 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
190 m->mothurOutEndLine();
191 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
192 m->mothurOut(newFastaFile); m->mothurOutEndLine();
193 m->mothurOut(newNamesFile); m->mothurOutEndLine();
194 m->mothurOutEndLine();
199 catch(exception& e) {
200 m->errorOut(e, "PreClusterCommand", "execute");
205 /**************************************************************************************************/
207 //this requires the names and fasta file to be in the same order
209 int PreClusterCommand::readFASTA(){
214 //m->openInputFile(namefile, inNames);
215 m->openInputFile(fastafile, inFasta);
217 //string firstCol, secondCol, nameString;
220 while (!inFasta.eof()) {
222 if (m->control_pressed) { inFasta.close(); return 0; }
224 //inNames >> firstCol >> secondCol;
225 //nameString = secondCol;
227 //m->gobble(inNames);
229 //while (secondCol.find_first_of(',') != -1) {
231 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
234 Sequence seq(inFasta); m->gobble(inFasta);
236 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
237 if (namefile != "") {
238 itSize = sizes.find(seq.getName());
240 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
242 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
243 alignSeqs.push_back(tempNode);
244 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
246 }else { //no names file, you are identical to yourself
247 seqPNode tempNode(1, seq, seq.getName());
248 alignSeqs.push_back(tempNode);
249 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
255 return alignSeqs.size();
258 catch(exception& e) {
259 m->errorOut(e, "PreClusterCommand", "readFASTA");
264 /**************************************************************************************************/
266 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
270 for (int i = 0; i < seq1.length(); i++) {
272 if (seq1[i] != seq2[i]) { numBad++; }
273 if (numBad > diffs) { return length; } //to far to cluster
278 catch(exception& e) {
279 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
284 /**************************************************************************************************/
286 void PreClusterCommand::printData(string newfasta, string newname){
291 m->openOutputFile(newfasta, outFasta);
292 m->openOutputFile(newname, outNames);
295 for (int i = 0; i < alignSeqs.size(); i++) {
296 if (alignSeqs[i].numIdentical != 0) {
297 alignSeqs[i].seq.printSequence(outFasta);
298 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
306 catch(exception& e) {
307 m->errorOut(e, "PreClusterCommand", "printData");
311 /**************************************************************************************************/
313 void PreClusterCommand::readNameFile(){
316 m->openInputFile(namefile, in);
317 string firstCol, secondCol;
320 in >> firstCol >> secondCol; m->gobble(in);
321 names[firstCol] = secondCol;
324 for(int i=0;i<secondCol.size();i++){
325 if(secondCol[i] == ','){ size++; }
327 sizes[firstCol] = size;
331 catch(exception& e) {
332 m->errorOut(e, "PreClusterCommand", "readNameFile");
337 /**************************************************************************************************/