1 #ifndef CLUSTERSPLITCOMMAND_H
2 #define CLUSTERSPLITCOMMAND_H
5 * clustersplitcommand.h
8 * Created by westcott on 5/19/10.
9 * Copyright 2010 Schloss Lab. All rights reserved.
13 #include "command.hpp"
14 #include "rabundvector.hpp"
15 #include "sabundvector.hpp"
16 #include "listvector.hpp"
17 #include "cluster.hpp"
18 #include "sparsematrix.hpp"
19 #include "readcluster.h"
20 #include "splitmatrix.h"
21 #include "readphylip.h"
22 #include "readcolumn.h"
23 #include "readmatrix.hpp"
24 #include "inputdata.h"
25 #include "clustercommand.h"
26 #include "clusterclassic.h"
28 class ClusterSplitCommand : public Command {
31 ClusterSplitCommand(string);
32 ClusterSplitCommand();
33 ~ClusterSplitCommand() {}
35 vector<string> setParameters();
36 string getCommandName() { return "cluster.split"; }
37 string getCommandCategory() { return "Clustering"; }
38 string getHelpString();
39 string getCitation() { return "Schloss PD, Westcott SL (2011). Assessing and improving methods used in OTU-based approaches for 16S rRNA gene sequence analysis. Appl Environ Microbiol. \nhttp://www.mothur.org/wiki/Cluster.split"; }
40 string getDescription() { return "splits your sequences by distance or taxonomy then clusters into OTUs"; }
43 void help() { m->mothurOut(getHelpString()); }
46 vector<int> processIDS; //processid
47 vector<string> outputNames;
49 string method, fileroot, tag, outputDir, phylipfile, columnfile, namefile, distfile, format, showabund, timing, splitmethod, taxFile, fastafile;
50 double cutoff, splitcutoff;
51 int precision, length, processors, taxLevelCutoff;
52 bool print_start, abort, hard, large, classic;
54 ofstream outList, outRabund, outSabund;
56 void printData(ListVector*);
57 vector<string> createProcesses(vector< map<string, string> >, set<string>&);
58 vector<string> cluster(vector< map<string, string> >, set<string>&);
59 string clusterFile(string, string, set<string>&, double&);
60 string clusterClassicFile(string, string, set<string>&, double&);
61 int mergeLists(vector<string>, map<float, int>, ListVector*);
62 map<float, int> completeListFile(vector<string>, string, set<string>&, ListVector*&);
63 int createMergedDistanceFile(vector< map<string, string> >);
66 /////////////////not working for Windows////////////////////////////////////////////////////////////
67 // getting an access violation error. This is most likely caused by the
68 // threads stepping on eachother's structures, as I can run the thread function and the cluster fuction
69 // in separately without errors occuring. I suspect it may be in the use of the
70 // static class mothurOut, but I can't pinpoint the problem. All other objects are made new
71 // within the thread. MothurOut is used by almost all the classes in mothur, so if this was
72 // really the cause I would expect to see all the windows threaded commands to have issues, but not
73 // all do. So far, shhh.flows and trim.flows have similiar problems. Other thoughts, could it have
74 // anything to do with mothur's use of copy constructors in many of our data structures. ie. listvector
75 // is copied by nameassignment and passed to read which passes to the thread? -westcott 2-8-12
76 ////////////////////////////////////////////////////////////////////////////////////////////////////
77 /**************************************************************************************************/
78 //custom data structure for threads to use.
79 // This is passed by void pointer so it can be any data type
80 // that can be passed using a single void pointer (LPVOID).
83 vector < map<string, string> > distNames;
86 double cutoff, precision;
87 string tag, outputDir;
88 vector<string> listFiles;
94 clusterData(vector < map<string, string> > dv, MothurOut* mout, double cu, string me, string ou, bool hd, double pre, int len, int th) {
107 /**************************************************************************************************/
108 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
110 static DWORD WINAPI MyClusterThreadFunction(LPVOID lpParam){
111 clusterData* pDataArray;
112 pDataArray = (clusterData*)lpParam;
115 cout << "starting " << endl;
117 double smallestCutoff = pDataArray->cutoff;
119 //cluster each distance file
120 for (int i = 0; i < pDataArray->distNames.size(); i++) {
122 Cluster* mycluster = NULL;
123 SparseMatrix* mymatrix = NULL;
124 ListVector* mylist = NULL;
125 ListVector myoldList;
126 RAbundVector* myrabund = NULL;
128 if (pDataArray->m->control_pressed) { break; }
130 string thisNamefile = pDataArray->distNames[i].begin()->second;
131 string thisDistFile = pDataArray->distNames[i].begin()->first;
132 cout << thisNamefile << '\t' << thisDistFile << endl;
133 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Reading " + thisDistFile); pDataArray->m->mothurOutEndLine();
135 ReadMatrix* myread = new ReadColumnMatrix(thisDistFile);
136 myread->setCutoff(pDataArray->cutoff);
137 NameAssignment* mynameMap = new NameAssignment(thisNamefile);
138 mynameMap->readMap();
139 cout << "done reading " << thisNamefile << endl;
140 myread->read(mynameMap);
141 cout << "done reading " << thisDistFile << endl;
142 if (pDataArray->m->control_pressed) { delete myread; delete mynameMap; break; }
144 mylist = myread->getListVector();
146 mymatrix = myread->getMatrix();
147 cout << "here" << endl;
148 delete myread; myread = NULL;
149 delete mynameMap; mynameMap = NULL;
151 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Clustering " + thisDistFile); pDataArray->m->mothurOutEndLine();
153 myrabund = new RAbundVector(mylist->getRAbundVector());
154 cout << "here" << endl;
156 if (pDataArray->method == "furthest") { mycluster = new CompleteLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
157 else if(pDataArray->method == "nearest"){ mycluster = new SingleLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
158 else if(pDataArray->method == "average"){ mycluster = new AverageLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
159 pDataArray->tag = mycluster->getTag();
160 cout << "here" << endl;
161 if (pDataArray->outputDir == "") { pDataArray->outputDir += pDataArray->m->hasPath(thisDistFile); }
162 string fileroot = pDataArray->outputDir + pDataArray->m->getRootName(pDataArray->m->getSimpleName(thisDistFile));
163 cout << "here" << endl;
165 pDataArray->m->openOutputFile(fileroot+ pDataArray->tag + ".list", listFile);
166 cout << "here" << endl;
167 pDataArray->listFiles.push_back(fileroot+ pDataArray->tag + ".list");
169 float previousDist = 0.00000;
170 float rndPreviousDist = 0.00000;
174 bool print_start = true;
175 int start = time(NULL);
176 double saveCutoff = pDataArray->cutoff;
178 while (mymatrix->getSmallDist() < pDataArray->cutoff && mymatrix->getNNodes() > 0){
180 if (pDataArray->m->control_pressed) { //clean up
181 delete mymatrix; delete mylist; delete mycluster; delete myrabund;
183 for (int i = 0; i < pDataArray->listFiles.size(); i++) { pDataArray->m->mothurRemove(pDataArray->listFiles[i]); }
184 pDataArray->listFiles.clear(); break;
187 mycluster->update(saveCutoff);
189 float dist = mymatrix->getSmallDist();
191 if (pDataArray->hard) {
192 rndDist = pDataArray->m->ceilDist(dist, pDataArray->precision);
194 rndDist = pDataArray->m->roundDist(dist, pDataArray->precision);
197 if(previousDist <= 0.0000 && dist != previousDist){
198 myoldList.setLabel("unique");
199 myoldList.print(listFile);
200 if (pDataArray->labels.count("unique") == 0) { pDataArray->labels.insert("unique"); }
202 else if(rndDist != rndPreviousDist){
203 myoldList.setLabel(toString(rndPreviousDist, pDataArray->length-1));
204 myoldList.print(listFile);
205 if (pDataArray->labels.count(toString(rndPreviousDist, pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist, pDataArray->length-1)); }
209 rndPreviousDist = rndDist;
213 cout << "here2" << endl;
214 if(previousDist <= 0.0000){
215 myoldList.setLabel("unique");
216 myoldList.print(listFile);
217 if (pDataArray->labels.count("unique") == 0) { pDataArray->labels.insert("unique"); }
219 else if(rndPreviousDist<pDataArray->cutoff){
220 myoldList.setLabel(toString(rndPreviousDist, pDataArray->length-1));
221 myoldList.print(listFile);
222 if (pDataArray->labels.count(toString(rndPreviousDist, pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist, pDataArray->length-1)); }
225 delete mymatrix; delete mylist; delete mycluster; delete myrabund;
226 mymatrix = NULL; mylist = NULL; mycluster = NULL; myrabund = NULL;
229 if (pDataArray->m->control_pressed) { //clean up
230 for (int i = 0; i < pDataArray->listFiles.size(); i++) { pDataArray->m->mothurRemove(pDataArray->listFiles[i]); }
231 pDataArray->listFiles.clear(); break;
233 cout << "here3" << endl;
234 pDataArray->m->mothurRemove(thisDistFile);
235 pDataArray->m->mothurRemove(thisNamefile);
236 cout << "here4" << endl;
237 if (saveCutoff != pDataArray->cutoff) {
238 if (pDataArray->hard) { saveCutoff = pDataArray->m->ceilDist(saveCutoff, pDataArray->precision); }
239 else { saveCutoff = pDataArray->m->roundDist(saveCutoff, pDataArray->precision); }
241 pDataArray->m->mothurOut("Cutoff was " + toString(pDataArray->cutoff) + " changed cutoff to " + toString(saveCutoff)); pDataArray->m->mothurOutEndLine();
243 cout << "here5" << endl;
244 if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff; }
247 pDataArray->cutoff = smallestCutoff;
252 catch(exception& e) {
253 pDataArray->m->errorOut(e, "ClusterSplitCommand", "MyClusterThreadFunction");