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") { 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 += 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 //reads fasta file and return number of seqs
119 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
121 if (m->control_pressed) { return 0; }
123 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
124 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; }
126 string fileroot = outputDir + getRootName(getSimpleName(fastafile));
127 string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
128 string newNamesFile = fileroot + "precluster.names";
132 openOutputFile(newFastaFile, outFasta);
133 openOutputFile(newNamesFile, outNames);
135 //sort seqs by number of identical seqs
136 alignSeqs.sort(comparePriority);
141 //think about running through twice...
142 list<seqPNode>::iterator itList;
143 list<seqPNode>::iterator itList2;
144 for (itList = alignSeqs.begin(); itList != alignSeqs.end(); itList++) {
145 //try to merge it with all smaller seqs
146 for (itList2 = alignSeqs.begin(); itList2 != alignSeqs.end(); itList2++) {
148 if (m->control_pressed) { outFasta.close(); outNames.close(); remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
150 if ((*itList).seq.getName() != (*itList2).seq.getName()) { //you don't want to merge with yourself
151 //are you within "diff" bases
152 int mismatch = calcMisMatches((*itList).seq.getAligned(), (*itList2).seq.getAligned());
154 if (mismatch <= diffs) {
156 (*itList).names += ',' + (*itList2).names;
157 (*itList).numIdentical += (*itList2).numIdentical;
159 alignSeqs.erase(itList2); itList2--;
165 //ouptut this sequence
166 printData(outFasta, outNames, (*itList));
169 alignSeqs.erase(itList); itList--;
173 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
176 if(i % 100 != 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
181 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
184 m->mothurOut("Total number of sequences before precluster was " + toString(numSeqs) + "."); m->mothurOutEndLine();
185 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine();
187 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
189 m->mothurOutEndLine();
190 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
191 m->mothurOut(newFastaFile); m->mothurOutEndLine();
192 m->mothurOut(newNamesFile); m->mothurOutEndLine();
193 m->mothurOutEndLine();
198 catch(exception& e) {
199 m->errorOut(e, "PreClusterCommand", "execute");
204 /**************************************************************************************************/
206 //this requires the names and fasta file to be in the same order
208 int PreClusterCommand::readFASTA(){
213 //openInputFile(namefile, inNames);
214 openInputFile(fastafile, inFasta);
216 //string firstCol, secondCol, nameString;
219 while (!inFasta.eof()) {
221 if (m->control_pressed) { inFasta.close(); return 0; }
223 //inNames >> firstCol >> secondCol;
224 //nameString = secondCol;
228 //while (secondCol.find_first_of(',') != -1) {
230 // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
233 Sequence seq(inFasta); gobble(inFasta);
235 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
236 if (namefile != "") {
237 itSize = sizes.find(seq.getName());
239 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
241 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
242 alignSeqs.push_back(tempNode);
243 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
245 }else { //no names file, you are identical to yourself
246 seqPNode tempNode(1, seq, seq.getName());
247 alignSeqs.push_back(tempNode);
248 if (seq.getAligned().length() > length) { length = seq.getAligned().length(); }
254 return alignSeqs.size();
257 catch(exception& e) {
258 m->errorOut(e, "PreClusterCommand", "readFASTA");
263 /**************************************************************************************************/
265 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
269 for (int i = 0; i < seq1.length(); i++) {
271 if (seq1[i] != seq2[i]) { numBad++; }
272 if (numBad > diffs) { return length; } //to far to cluster
277 catch(exception& e) {
278 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
283 /**************************************************************************************************/
285 void PreClusterCommand::printData(ofstream& outFasta, ofstream& outNames, seqPNode thisSeq){
287 thisSeq.seq.printSequence(outFasta);
288 outNames << thisSeq.seq.getName() << '\t' << thisSeq.names << endl;
290 catch(exception& e) {
291 m->errorOut(e, "PreClusterCommand", "printData");
295 /**************************************************************************************************/
297 void PreClusterCommand::readNameFile(){
300 openInputFile(namefile, in);
301 string firstCol, secondCol;
304 in >> firstCol >> secondCol; gobble(in);
305 names[firstCol] = secondCol;
307 while (secondCol.find_first_of(',') != -1) {
309 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
311 sizes[firstCol] = size;
315 catch(exception& e) {
316 m->errorOut(e, "PreClusterCommand", "readNameFile");
321 /**************************************************************************************************/