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); }
15 //**********************************************************************************************************************
16 vector<string> PreClusterCommand::setParameters(){
18 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
19 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
20 CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "PreClusterCommand", "setParameters");
33 //**********************************************************************************************************************
34 string PreClusterCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The pre.cluster command groups sequences that are within a given number of base mismatches.\n";
38 helpString += "The pre.cluster command outputs a new fasta and name file.\n";
39 helpString += "The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n";
40 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";
41 helpString += "The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n";
42 helpString += "The pre.cluster command should be in the following format: \n";
43 helpString += "pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n";
44 helpString += "Example pre.cluster(fasta=amazon.fasta, diffs=2).\n";
45 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
49 m->errorOut(e, "PreClusterCommand", "getHelpString");
54 //**********************************************************************************************************************
55 PreClusterCommand::PreClusterCommand(){
57 abort = true; calledHelp = true;
59 vector<string> tempOutNames;
60 outputTypes["fasta"] = tempOutNames;
61 outputTypes["name"] = tempOutNames;
64 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
68 //**********************************************************************************************************************
70 PreClusterCommand::PreClusterCommand(string option) {
72 abort = false; calledHelp = false;
74 //allow user to run help
75 if(option == "help") { help(); abort = true; calledHelp = true; }
76 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
79 vector<string> myArray = setParameters();
81 OptionParser parser(option);
82 map<string, string> parameters = parser.getParameters();
84 ValidParameters validParameter;
85 map<string, string>::iterator it;
87 //check to make sure all parameters are valid for command
88 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
89 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["fasta"] = tempOutNames;
95 outputTypes["name"] = tempOutNames;
97 //if the user changes the input directory command factory will send this info to us in the output parameter
98 string inputDir = validParameter.validFile(parameters, "inputdir", false);
99 if (inputDir == "not found"){ inputDir = ""; }
102 it = parameters.find("fasta");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["fasta"] = inputDir + it->second; }
110 it = parameters.find("name");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["name"] = inputDir + it->second; }
119 //check for required parameters
120 fastafile = validParameter.validFile(parameters, "fasta", true);
121 if (fastafile == "not found") {
122 fastafile = m->getFastaFile();
123 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
124 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
126 else if (fastafile == "not open") { abort = true; }
128 //if the user changes the output directory command factory will send this info to us in the output parameter
129 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
131 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
134 //check for optional parameter and set defaults
135 // ...at some point should added some additional type checking...
136 namefile = validParameter.validFile(parameters, "name", true);
137 if (namefile == "not found") { namefile = ""; }
138 else if (namefile == "not open") { abort = true; }
139 else { readNameFile(); }
141 string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; }
142 convert(temp, diffs);
146 catch(exception& e) {
147 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
151 //**********************************************************************************************************************
153 int PreClusterCommand::execute(){
156 if (abort == true) { if (calledHelp) { return 0; } return 2; }
158 int start = time(NULL);
160 //reads fasta file and return number of seqs
161 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
163 if (m->control_pressed) { return 0; }
165 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
166 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; }
168 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
171 //sort seqs by number of identical seqs
172 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
176 //think about running through twice...
177 for (int i = 0; i < numSeqs; i++) {
180 // itActive = active.find(alignSeqs[i].seq.getName());
182 if (alignSeqs[i].active) { //this sequence has not been merged yet
184 //try to merge it with all smaller seqs
185 for (int j = i+1; j < numSeqs; j++) {
187 if (m->control_pressed) { return 0; }
189 if (alignSeqs[j].active) { //this sequence has not been merged yet
190 //are you within "diff" bases
191 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
193 if (mismatch <= diffs) {
195 alignSeqs[i].names += ',' + alignSeqs[j].names;
196 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
198 alignSeqs[j].active = 0;
199 alignSeqs[j].numIdentical = 0;
205 //remove from active list
206 alignSeqs[i].active = 0;
209 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
212 if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
215 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
217 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
218 string newNamesFile = fileroot + "precluster.names";
220 if (m->control_pressed) { return 0; }
222 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
223 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
224 printData(newFastaFile, newNamesFile);
226 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
228 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
230 m->mothurOutEndLine();
231 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
232 m->mothurOut(newFastaFile); m->mothurOutEndLine(); outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
233 m->mothurOut(newNamesFile); m->mothurOutEndLine(); outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
234 m->mothurOutEndLine();
236 //set fasta file as new current fastafile
238 itTypes = outputTypes.find("fasta");
239 if (itTypes != outputTypes.end()) {
240 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
243 itTypes = outputTypes.find("name");
244 if (itTypes != outputTypes.end()) {
245 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
251 catch(exception& e) {
252 m->errorOut(e, "PreClusterCommand", "execute");
257 /**************************************************************************************************/
259 //this requires the names and fasta file to be in the same order
261 int PreClusterCommand::readFASTA(){
266 //m->openInputFile(namefile, inNames);
267 m->openInputFile(fastafile, inFasta);
269 //string firstCol, secondCol, nameString;
272 while (!inFasta.eof()) {
274 if (m->control_pressed) { inFasta.close(); return 0; }
276 //inNames >> firstCol >> secondCol;
277 //nameString = secondCol;
279 //m->gobble(inNames);
281 //while (secondCol.find_first_of(',') != -1) {
283 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
286 Sequence seq(inFasta); m->gobble(inFasta);
288 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
289 if (namefile != "") {
290 itSize = sizes.find(seq.getName());
292 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
294 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
295 alignSeqs.push_back(tempNode);
296 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
298 }else { //no names file, you are identical to yourself
299 seqPNode tempNode(1, seq, seq.getName());
300 alignSeqs.push_back(tempNode);
301 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
307 return alignSeqs.size();
310 catch(exception& e) {
311 m->errorOut(e, "PreClusterCommand", "readFASTA");
316 /**************************************************************************************************/
318 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
322 for (int i = 0; i < seq1.length(); i++) {
324 if (seq1[i] != seq2[i]) { numBad++; }
325 if (numBad > diffs) { return length; } //to far to cluster
330 catch(exception& e) {
331 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
336 /**************************************************************************************************/
338 void PreClusterCommand::printData(string newfasta, string newname){
343 m->openOutputFile(newfasta, outFasta);
344 m->openOutputFile(newname, outNames);
347 for (int i = 0; i < alignSeqs.size(); i++) {
348 if (alignSeqs[i].numIdentical != 0) {
349 alignSeqs[i].seq.printSequence(outFasta);
350 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
358 catch(exception& e) {
359 m->errorOut(e, "PreClusterCommand", "printData");
363 /**************************************************************************************************/
365 void PreClusterCommand::readNameFile(){
368 m->openInputFile(namefile, in);
369 string firstCol, secondCol;
372 in >> firstCol >> secondCol; m->gobble(in);
373 names[firstCol] = secondCol;
376 for(int i=0;i<secondCol.size();i++){
377 if(secondCol[i] == ','){ size++; }
379 sizes[firstCol] = size;
383 catch(exception& e) {
384 m->errorOut(e, "PreClusterCommand", "readNameFile");
389 /**************************************************************************************************/