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 //**********************************************************************************************************************
15 vector<string> PreClusterCommand::getValidParameters(){
17 string Array[] = {"fasta", "name", "diffs", "outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "PreClusterCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 PreClusterCommand::PreClusterCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["name"] = tempOutNames;
35 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
39 //**********************************************************************************************************************
40 vector<string> PreClusterCommand::getRequiredParameters(){
42 string Array[] = {"fasta"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "PreClusterCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> PreClusterCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "PreClusterCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 PreClusterCommand::PreClusterCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"fasta", "name", "diffs", "outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
84 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["fasta"] = tempOutNames;
90 outputTypes["name"] = tempOutNames;
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
97 it = parameters.find("fasta");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["fasta"] = inputDir + it->second; }
105 it = parameters.find("name");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["name"] = inputDir + it->second; }
114 //check for required parameters
115 fastafile = validParameter.validFile(parameters, "fasta", true);
116 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the pre.cluster command."); m->mothurOutEndLine(); abort = true; }
117 else if (fastafile == "not open") { abort = true; }
119 //if the user changes the output directory command factory will send this info to us in the output parameter
120 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
122 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
125 //check for optional parameter and set defaults
126 // ...at some point should added some additional type checking...
127 namefile = validParameter.validFile(parameters, "name", true);
128 if (namefile == "not found") { namefile = ""; }
129 else if (namefile == "not open") { abort = true; }
130 else { readNameFile(); }
132 string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; }
133 convert(temp, diffs);
137 catch(exception& e) {
138 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
143 //**********************************************************************************************************************
144 PreClusterCommand::~PreClusterCommand(){}
145 //**********************************************************************************************************************
147 void PreClusterCommand::help(){
149 m->mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
150 m->mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
151 m->mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
152 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");
153 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");
154 m->mothurOut("The pre.cluster command should be in the following format: \n");
155 m->mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
156 m->mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
157 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
159 catch(exception& e) {
160 m->errorOut(e, "PreClusterCommand", "help");
164 //**********************************************************************************************************************
166 int PreClusterCommand::execute(){
169 if (abort == true) { return 0; }
171 int start = time(NULL);
173 //reads fasta file and return number of seqs
174 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
176 if (m->control_pressed) { return 0; }
178 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
179 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; }
181 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
184 //sort seqs by number of identical seqs
185 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
189 //think about running through twice...
190 for (int i = 0; i < numSeqs; i++) {
193 // itActive = active.find(alignSeqs[i].seq.getName());
195 if (alignSeqs[i].active) { //this sequence has not been merged yet
197 //try to merge it with all smaller seqs
198 for (int j = i+1; j < numSeqs; j++) {
200 if (m->control_pressed) { return 0; }
202 if (alignSeqs[j].active) { //this sequence has not been merged yet
203 //are you within "diff" bases
204 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
206 if (mismatch <= diffs) {
208 alignSeqs[i].names += ',' + alignSeqs[j].names;
209 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
211 alignSeqs[j].active = 0;
212 alignSeqs[j].numIdentical = 0;
218 //remove from active list
219 alignSeqs[i].active = 0;
222 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
225 if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
228 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
230 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
231 string newNamesFile = fileroot + "precluster.names";
233 if (m->control_pressed) { return 0; }
235 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
236 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
237 printData(newFastaFile, newNamesFile);
239 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
241 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
243 m->mothurOutEndLine();
244 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245 m->mothurOut(newFastaFile); m->mothurOutEndLine(); outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
246 m->mothurOut(newNamesFile); m->mothurOutEndLine(); outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
247 m->mothurOutEndLine();
252 catch(exception& e) {
253 m->errorOut(e, "PreClusterCommand", "execute");
258 /**************************************************************************************************/
260 //this requires the names and fasta file to be in the same order
262 int PreClusterCommand::readFASTA(){
267 //m->openInputFile(namefile, inNames);
268 m->openInputFile(fastafile, inFasta);
270 //string firstCol, secondCol, nameString;
273 while (!inFasta.eof()) {
275 if (m->control_pressed) { inFasta.close(); return 0; }
277 //inNames >> firstCol >> secondCol;
278 //nameString = secondCol;
280 //m->gobble(inNames);
282 //while (secondCol.find_first_of(',') != -1) {
284 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
287 Sequence seq(inFasta); m->gobble(inFasta);
289 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
290 if (namefile != "") {
291 itSize = sizes.find(seq.getName());
293 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
295 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
296 alignSeqs.push_back(tempNode);
297 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
299 }else { //no names file, you are identical to yourself
300 seqPNode tempNode(1, seq, seq.getName());
301 alignSeqs.push_back(tempNode);
302 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
308 return alignSeqs.size();
311 catch(exception& e) {
312 m->errorOut(e, "PreClusterCommand", "readFASTA");
317 /**************************************************************************************************/
319 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
323 for (int i = 0; i < seq1.length(); i++) {
325 if (seq1[i] != seq2[i]) { numBad++; }
326 if (numBad > diffs) { return length; } //to far to cluster
331 catch(exception& e) {
332 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
337 /**************************************************************************************************/
339 void PreClusterCommand::printData(string newfasta, string newname){
344 m->openOutputFile(newfasta, outFasta);
345 m->openOutputFile(newname, outNames);
348 for (int i = 0; i < alignSeqs.size(); i++) {
349 if (alignSeqs[i].numIdentical != 0) {
350 alignSeqs[i].seq.printSequence(outFasta);
351 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
359 catch(exception& e) {
360 m->errorOut(e, "PreClusterCommand", "printData");
364 /**************************************************************************************************/
366 void PreClusterCommand::readNameFile(){
369 m->openInputFile(namefile, in);
370 string firstCol, secondCol;
373 in >> firstCol >> secondCol; m->gobble(in);
374 names[firstCol] = secondCol;
377 for(int i=0;i<secondCol.size();i++){
378 if(secondCol[i] == ','){ size++; }
380 sizes[firstCol] = size;
384 catch(exception& e) {
385 m->errorOut(e, "PreClusterCommand", "readNameFile");
390 /**************************************************************************************************/