5 * Created by westcott on 9/23/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "clusterfragmentscommand.h"
12 //**********************************************************************************************************************
14 inline bool comparePriority(seqRNode first, seqRNode second) {
17 if (first.length > second.length) {
19 }else if (first.length == second.length) {
20 if (first.numIdentical > second.numIdentical) {
27 //**********************************************************************************************************************
28 ClusterFragmentsCommand::ClusterFragmentsCommand(string option) {
32 //allow user to run help
33 if(option == "help") { help(); abort = true; }
36 //valid paramters for this command
37 string Array[] = {"fasta","name","outputdir","inputdir"};
38 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
40 OptionParser parser(option);
41 map<string, string> parameters = parser.getParameters();
43 ValidParameters validParameter;
44 map<string, string>::iterator it;
46 //check to make sure all parameters are valid for command
47 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
48 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
51 //if the user changes the input directory command factory will send this info to us in the output parameter
52 string inputDir = validParameter.validFile(parameters, "inputdir", false);
53 if (inputDir == "not found"){ inputDir = ""; }
56 it = parameters.find("fasta");
57 //user has given a template file
58 if(it != parameters.end()){
59 path = m->hasPath(it->second);
60 //if the user has not given a path then, add inputdir. else leave path alone.
61 if (path == "") { parameters["fasta"] = inputDir + it->second; }
64 it = parameters.find("name");
65 //user has given a template file
66 if(it != parameters.end()){
67 path = m->hasPath(it->second);
68 //if the user has not given a path then, add inputdir. else leave path alone.
69 if (path == "") { parameters["name"] = inputDir + it->second; }
73 //check for required parameters
74 fastafile = validParameter.validFile(parameters, "fasta", true);
75 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the cluster.fragments command."); m->mothurOutEndLine(); abort = true; }
76 else if (fastafile == "not open") { abort = true; }
78 //if the user changes the output directory command factory will send this info to us in the output parameter
79 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
81 //check for optional parameter and set defaults
82 // ...at some point should added some additional type checking...
83 namefile = validParameter.validFile(parameters, "name", true);
84 if (namefile == "not found") { namefile = ""; }
85 else if (namefile == "not open") { abort = true; }
86 else { readNameFile(); }
91 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
96 //**********************************************************************************************************************
97 ClusterFragmentsCommand::~ClusterFragmentsCommand(){}
98 //**********************************************************************************************************************
99 void ClusterFragmentsCommand::help(){
101 m->mothurOut("The cluster.fragments command groups sequences that are part of a larger sequence.\n");
102 m->mothurOut("The cluster.fragments command outputs a new fasta and name file.\n");
103 m->mothurOut("The cluster.fragments command parameters are fasta and name. The fasta parameter is required. \n");
104 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");
105 m->mothurOut("The cluster.fragments command should be in the following format: \n");
106 m->mothurOut("cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n");
107 m->mothurOut("Example cluster.fragments(fasta=amazon.fasta).\n");
108 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
110 catch(exception& e) {
111 m->errorOut(e, "ClusterFragmentsCommand", "help");
115 //**********************************************************************************************************************
116 int ClusterFragmentsCommand::execute(){
119 if (abort == true) { return 0; }
121 int start = time(NULL);
123 //reads fasta file and return number of seqs
124 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
126 if (m->control_pressed) { return 0; }
128 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
130 //sort seqs by length of unaligned sequence
131 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
135 //think about running through twice...
136 for (int i = 0; i < numSeqs; i++) {
138 if (alignSeqs[i].active) { //this sequence has not been merged yet
140 string iBases = alignSeqs[i].seq.getUnaligned();
142 //try to merge it with all smaller seqs
143 for (int j = i+1; j < numSeqs; j++) {
145 if (m->control_pressed) { return 0; }
147 if (alignSeqs[j].active) { //this sequence has not been merged yet
149 string jBases = alignSeqs[j].seq.getUnaligned();
151 int pos = iBases.find(jBases);
153 if (pos != string::npos) {
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 + "fragclust.fasta";
178 string newNamesFile = fileroot + "names";
180 if (m->control_pressed) { return 0; }
182 m->mothurOutEndLine();
183 m->mothurOut("Total number of sequences before cluster.fragments was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
184 m->mothurOut("cluster.fragments removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
186 printData(newFastaFile, newNamesFile);
188 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
190 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
192 m->mothurOutEndLine();
193 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
194 m->mothurOut(newFastaFile); m->mothurOutEndLine();
195 m->mothurOut(newNamesFile); m->mothurOutEndLine();
196 m->mothurOutEndLine();
201 catch(exception& e) {
202 m->errorOut(e, "ClusterFragmentsCommand", "execute");
207 /**************************************************************************************************/
208 int ClusterFragmentsCommand::readFASTA(){
212 m->openInputFile(fastafile, inFasta);
214 while (!inFasta.eof()) {
216 if (m->control_pressed) { inFasta.close(); return 0; }
218 Sequence seq(inFasta); m->gobble(inFasta);
220 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
221 if (namefile != "") {
222 itSize = sizes.find(seq.getName());
224 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
226 seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length());
227 alignSeqs.push_back(tempNode);
229 }else { //no names file, you are identical to yourself
230 seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length());
231 alignSeqs.push_back(tempNode);
237 return alignSeqs.size();
240 catch(exception& e) {
241 m->errorOut(e, "ClusterFragmentsCommand", "readFASTA");
245 /**************************************************************************************************/
246 void ClusterFragmentsCommand::printData(string newfasta, string newname){
251 m->openOutputFile(newfasta, outFasta);
252 m->openOutputFile(newname, outNames);
254 for (int i = 0; i < alignSeqs.size(); i++) {
255 if (alignSeqs[i].numIdentical != 0) {
256 alignSeqs[i].seq.printSequence(outFasta);
257 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
264 catch(exception& e) {
265 m->errorOut(e, "ClusterFragmentsCommand", "printData");
269 /**************************************************************************************************/
271 void ClusterFragmentsCommand::readNameFile(){
274 m->openInputFile(namefile, in);
275 string firstCol, secondCol;
278 in >> firstCol >> secondCol; m->gobble(in);
279 names[firstCol] = secondCol;
282 for(int i=0;i<secondCol.size();i++){
283 if(secondCol[i] == ','){ size++; }
285 sizes[firstCol] = size;
289 catch(exception& e) {
290 m->errorOut(e, "ClusterFragmentsCommand", "readNameFile");
295 /**************************************************************************************************/