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(){
43 //initialize outputTypes
44 vector<string> tempOutNames;
45 outputTypes["fasta"] = tempOutNames;
46 outputTypes["name"] = tempOutNames;
49 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
53 //**********************************************************************************************************************
54 vector<string> ClusterFragmentsCommand::getRequiredParameters(){
56 string Array[] = {"fasta"};
57 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
61 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredParameters");
65 //**********************************************************************************************************************
66 vector<string> ClusterFragmentsCommand::getRequiredFiles(){
68 vector<string> myArray;
72 m->errorOut(e, "ClusterFragmentsCommand", "getRequiredFiles");
76 //**********************************************************************************************************************
77 ClusterFragmentsCommand::ClusterFragmentsCommand(string option) {
81 //allow user to run help
82 if(option == "help") { help(); abort = true; }
85 //valid paramters for this command
86 string Array[] = {"fasta","name","outputdir","inputdir"};
87 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
89 OptionParser parser(option);
90 map<string, string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string, string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) {
97 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["fasta"] = tempOutNames;
103 outputTypes["name"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
118 it = parameters.find("name");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["name"] = inputDir + it->second; }
127 //check for required parameters
128 fastafile = validParameter.validFile(parameters, "fasta", true);
129 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the cluster.fragments command."); m->mothurOutEndLine(); abort = true; }
130 else if (fastafile == "not open") { abort = true; }
132 //if the user changes the output directory command factory will send this info to us in the output parameter
133 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
135 //check for optional parameter and set defaults
136 // ...at some point should added some additional type checking...
137 namefile = validParameter.validFile(parameters, "name", true);
138 if (namefile == "not found") { namefile = ""; }
139 else if (namefile == "not open") { abort = true; }
140 else { readNameFile(); }
144 catch(exception& e) {
145 m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand");
150 //**********************************************************************************************************************
151 ClusterFragmentsCommand::~ClusterFragmentsCommand(){}
152 //**********************************************************************************************************************
153 void ClusterFragmentsCommand::help(){
155 m->mothurOut("The cluster.fragments command groups sequences that are part of a larger sequence.\n");
156 m->mothurOut("The cluster.fragments command outputs a new fasta and name file.\n");
157 m->mothurOut("The cluster.fragments command parameters are fasta and name. The fasta parameter is required. \n");
158 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");
159 m->mothurOut("The cluster.fragments command should be in the following format: \n");
160 m->mothurOut("cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n");
161 m->mothurOut("Example cluster.fragments(fasta=amazon.fasta).\n");
162 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
164 catch(exception& e) {
165 m->errorOut(e, "ClusterFragmentsCommand", "help");
169 //**********************************************************************************************************************
170 int ClusterFragmentsCommand::execute(){
173 if (abort == true) { return 0; }
175 int start = time(NULL);
177 //reads fasta file and return number of seqs
178 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
180 if (m->control_pressed) { return 0; }
182 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; }
184 //sort seqs by length of unaligned sequence
185 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
189 //think about running through twice...
190 for (int i = 0; i < numSeqs; i++) {
192 if (alignSeqs[i].active) { //this sequence has not been merged yet
194 string iBases = alignSeqs[i].seq.getUnaligned();
196 //try to merge it with all smaller seqs
197 for (int j = i+1; j < numSeqs; j++) {
199 if (m->control_pressed) { return 0; }
201 if (alignSeqs[j].active) { //this sequence has not been merged yet
203 string jBases = alignSeqs[j].seq.getUnaligned();
205 int pos = iBases.find(jBases);
207 if (pos != string::npos) {
209 alignSeqs[i].names += ',' + alignSeqs[j].names;
210 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
212 alignSeqs[j].active = 0;
213 alignSeqs[j].numIdentical = 0;
219 //remove from active list
220 alignSeqs[i].active = 0;
223 if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
226 if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
229 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
231 string newFastaFile = fileroot + "fragclust.fasta";
232 string newNamesFile = fileroot + "names";
234 if (m->control_pressed) { return 0; }
236 m->mothurOutEndLine();
237 m->mothurOut("Total number of sequences before cluster.fragments was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
238 m->mothurOut("cluster.fragments removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
240 printData(newFastaFile, newNamesFile);
242 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
244 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
246 m->mothurOutEndLine();
247 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
248 m->mothurOut(newFastaFile); m->mothurOutEndLine();
249 m->mothurOut(newNamesFile); m->mothurOutEndLine();
250 outputNames.push_back(newFastaFile); outputNames.push_back(newNamesFile); outputTypes["fasta"].push_back(newFastaFile); outputTypes["name"].push_back(newNamesFile);
251 m->mothurOutEndLine();
256 catch(exception& e) {
257 m->errorOut(e, "ClusterFragmentsCommand", "execute");
262 /**************************************************************************************************/
263 int ClusterFragmentsCommand::readFASTA(){
267 m->openInputFile(fastafile, inFasta);
269 while (!inFasta.eof()) {
271 if (m->control_pressed) { inFasta.close(); return 0; }
273 Sequence seq(inFasta); m->gobble(inFasta);
275 if (seq.getName() != "") { //can get "" if commented line is at end of fasta file
276 if (namefile != "") {
277 itSize = sizes.find(seq.getName());
279 if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
281 seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length());
282 alignSeqs.push_back(tempNode);
284 }else { //no names file, you are identical to yourself
285 seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length());
286 alignSeqs.push_back(tempNode);
292 return alignSeqs.size();
295 catch(exception& e) {
296 m->errorOut(e, "ClusterFragmentsCommand", "readFASTA");
300 /**************************************************************************************************/
301 void ClusterFragmentsCommand::printData(string newfasta, string newname){
306 m->openOutputFile(newfasta, outFasta);
307 m->openOutputFile(newname, outNames);
309 for (int i = 0; i < alignSeqs.size(); i++) {
310 if (alignSeqs[i].numIdentical != 0) {
311 alignSeqs[i].seq.printSequence(outFasta);
312 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
319 catch(exception& e) {
320 m->errorOut(e, "ClusterFragmentsCommand", "printData");
324 /**************************************************************************************************/
326 void ClusterFragmentsCommand::readNameFile(){
329 m->openInputFile(namefile, in);
330 string firstCol, secondCol;
333 in >> firstCol >> secondCol; m->gobble(in);
334 names[firstCol] = secondCol;
337 for(int i=0;i<secondCol.size();i++){
338 if(secondCol[i] == ','){ size++; }
340 sizes[firstCol] = size;
344 catch(exception& e) {
345 m->errorOut(e, "ClusterFragmentsCommand", "readNameFile");
350 /**************************************************************************************************/