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 vector<string> ClusterFragmentsCommand::getValidParameters(){
30 string AlignArray[] = {"fasta","name","outputdir","inputdir"};
31 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
35 m->errorOut(e, "ClusterFragmentsCommand", "getValidParameters");
39 //**********************************************************************************************************************
40 ClusterFragmentsCommand::ClusterFragmentsCommand(){
42 //initialize outputTypes
43 vector<string> tempOutNames;
44 outputTypes["fasta"] = tempOutNames;
45 outputTypes["name"] = tempOutNames;
48 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
52 //**********************************************************************************************************************
53 vector<string> ClusterFragmentsCommand::getRequiredParameters(){
55 string Array[] = {"fasta"};
56 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
60 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredParameters");
64 //**********************************************************************************************************************
65 vector<string> ClusterFragmentsCommand::getRequiredFiles(){
67 vector<string> myArray;
71 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredFiles");
75 //**********************************************************************************************************************
76 ClusterFragmentsCommand::ClusterFragmentsCommand(string option) {
80 //allow user to run help
81 if(option == "help") { help(); abort = true; }
84 //valid paramters for this command
85 string Array[] = {"fasta","name","outputdir","inputdir"};
86 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
88 OptionParser parser(option);
89 map<string, string> parameters = parser.getParameters();
91 ValidParameters validParameter;
92 map<string, string>::iterator it;
94 //check to make sure all parameters are valid for command
95 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
96 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["fasta"] = tempOutNames;
102 outputTypes["name"] = tempOutNames;
104 //if the user changes the input directory command factory will send this info to us in the output parameter
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 it = parameters.find("fasta");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["fasta"] = inputDir + it->second; }
117 it = parameters.find("name");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["name"] = inputDir + it->second; }
126 //check for required parameters
127 fastafile = validParameter.validFile(parameters, "fasta", true);
128 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the cluster.fragments command."); m->mothurOutEndLine(); abort = true; }
129 else if (fastafile == "not open") { abort = true; }
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
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(); }
143 catch(exception& e) {
144 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
149 //**********************************************************************************************************************
150 ClusterFragmentsCommand::~ClusterFragmentsCommand(){}
151 //**********************************************************************************************************************
152 void ClusterFragmentsCommand::help(){
154 m->mothurOut("The cluster.fragments command groups sequences that are part of a larger sequence.\n");
155 m->mothurOut("The cluster.fragments command outputs a new fasta and name file.\n");
156 m->mothurOut("The cluster.fragments command parameters are fasta and name. The fasta parameter is required. \n");
157 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");
158 m->mothurOut("The cluster.fragments command should be in the following format: \n");
159 m->mothurOut("cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n");
160 m->mothurOut("Example cluster.fragments(fasta=amazon.fasta).\n");
161 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
163 catch(exception& e) {
164 m->errorOut(e, "ClusterFragmentsCommand", "help");
168 //**********************************************************************************************************************
169 int ClusterFragmentsCommand::execute(){
172 if (abort == true) { return 0; }
174 int start = time(NULL);
176 //reads fasta file and return number of seqs
177 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
179 if (m->control_pressed) { return 0; }
181 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
183 //sort seqs by length of unaligned sequence
184 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
188 //think about running through twice...
189 for (int i = 0; i < numSeqs; i++) {
191 if (alignSeqs[i].active) { //this sequence has not been merged yet
193 string iBases = alignSeqs[i].seq.getUnaligned();
195 //try to merge it with all smaller seqs
196 for (int j = i+1; j < numSeqs; j++) {
198 if (m->control_pressed) { return 0; }
200 if (alignSeqs[j].active) { //this sequence has not been merged yet
202 string jBases = alignSeqs[j].seq.getUnaligned();
204 int pos = iBases.find(jBases);
206 if (pos != string::npos) {
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 + "fragclust.fasta";
231 string newNamesFile = fileroot + "names";
233 if (m->control_pressed) { return 0; }
235 m->mothurOutEndLine();
236 m->mothurOut("Total number of sequences before cluster.fragments was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
237 m->mothurOut("cluster.fragments removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
239 printData(newFastaFile, newNamesFile);
241 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
243 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
245 m->mothurOutEndLine();
246 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
247 m->mothurOut(newFastaFile); m->mothurOutEndLine();
248 m->mothurOut(newNamesFile); m->mothurOutEndLine();
249 outputNames.push_back(newFastaFile); outputNames.push_back(newNamesFile); outputTypes["fasta"].push_back(newFastaFile); outputTypes["name"].push_back(newNamesFile);
250 m->mothurOutEndLine();
255 catch(exception& e) {
256 m->errorOut(e, "ClusterFragmentsCommand", "execute");
261 /**************************************************************************************************/
262 int ClusterFragmentsCommand::readFASTA(){
266 m->openInputFile(fastafile, inFasta);
268 while (!inFasta.eof()) {
270 if (m->control_pressed) { inFasta.close(); return 0; }
272 Sequence seq(inFasta); m->gobble(inFasta);
274 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
275 if (namefile != "") {
276 itSize = sizes.find(seq.getName());
278 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
280 seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length());
281 alignSeqs.push_back(tempNode);
283 }else { //no names file, you are identical to yourself
284 seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length());
285 alignSeqs.push_back(tempNode);
291 return alignSeqs.size();
294 catch(exception& e) {
295 m->errorOut(e, "ClusterFragmentsCommand", "readFASTA");
299 /**************************************************************************************************/
300 void ClusterFragmentsCommand::printData(string newfasta, string newname){
305 m->openOutputFile(newfasta, outFasta);
306 m->openOutputFile(newname, outNames);
308 for (int i = 0; i < alignSeqs.size(); i++) {
309 if (alignSeqs[i].numIdentical != 0) {
310 alignSeqs[i].seq.printSequence(outFasta);
311 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
318 catch(exception& e) {
319 m->errorOut(e, "ClusterFragmentsCommand", "printData");
323 /**************************************************************************************************/
325 void ClusterFragmentsCommand::readNameFile(){
328 m->openInputFile(namefile, in);
329 string firstCol, secondCol;
332 in >> firstCol >> secondCol; m->gobble(in);
333 names[firstCol] = secondCol;
336 for(int i=0;i<secondCol.size();i++){
337 if(secondCol[i] == ','){ size++; }
339 sizes[firstCol] = size;
343 catch(exception& e) {
344 m->errorOut(e, "ClusterFragmentsCommand", "readNameFile");
349 /**************************************************************************************************/