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"
27 class ClusterSplitCommand : public Command {
30 ClusterSplitCommand(string);
31 ClusterSplitCommand();
32 ~ClusterSplitCommand() {}
34 vector<string> setParameters();
35 string getCommandName() { return "cluster.split"; }
36 string getCommandCategory() { return "Clustering"; }
37 string getHelpString();
38 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"; }
39 string getDescription() { return "splits your sequences by distance or taxonomy then clusters into OTUs"; }
42 void help() { m->mothurOut(getHelpString()); }
45 vector<int> processIDS; //processid
46 vector<string> outputNames;
48 string method, fileroot, tag, outputDir, phylipfile, columnfile, namefile, distfile, format, showabund, timing, splitmethod, taxFile, fastafile;
49 double cutoff, splitcutoff;
50 int precision, length, processors, taxLevelCutoff;
51 bool print_start, abort, hard, large;
53 ofstream outList, outRabund, outSabund;
55 void printData(ListVector*);
56 vector<string> createProcesses(vector< map<string, string> >, set<string>&);
57 vector<string> cluster(vector< map<string, string> >, set<string>&);
58 int mergeLists(vector<string>, map<float, int>, ListVector*);
59 map<float, int> completeListFile(vector<string>, string, set<string>&, ListVector*&);
60 int createMergedDistanceFile(vector< map<string, string> >);
63 /////////////////not working for Windows////////////////////////////////////////////////////////////
64 // getting an access violation error. This is most likely caused by the
65 // threads stepping on eachother's structures, as I can run the thread function and the cluster fuction
66 // in separately without errors occuring. I suspect it may be in the use of the
67 // static class mothurOut, but I can't pinpoint the problem. All other objects are made new
68 // within the thread. MothurOut is used by almost all the classes in mothur, so if this was
69 // really the cause I would expect to see all the windows threaded commands to have issues, but not
70 // all do. So far, shhh.flows and trim.flows have similiar problems. Other thoughts, could it have
71 // anything to do with mothur's use of copy constructors in many of our data structures. ie. listvector
72 // is copied by nameassignment and passed to read which passes to the thread? -westcott 2-8-12
73 ////////////////////////////////////////////////////////////////////////////////////////////////////
74 /**************************************************************************************************/
75 //custom data structure for threads to use.
76 // This is passed by void pointer so it can be any data type
77 // that can be passed using a single void pointer (LPVOID).
80 vector < map<string, string> > distNames;
83 double cutoff, precision;
84 string tag, outputDir;
85 vector<string> listFiles;
91 clusterData(vector < map<string, string> > dv, MothurOut* mout, double cu, string me, string ou, bool hd, double pre, int len, int th) {
104 /**************************************************************************************************/
105 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
107 static DWORD WINAPI MyClusterThreadFunction(LPVOID lpParam){
108 clusterData* pDataArray;
109 pDataArray = (clusterData*)lpParam;
112 cout << "starting " << endl;
114 double smallestCutoff = pDataArray->cutoff;
116 //cluster each distance file
117 for (int i = 0; i < pDataArray->distNames.size(); i++) {
119 Cluster* mycluster = NULL;
120 SparseMatrix* mymatrix = NULL;
121 ListVector* mylist = NULL;
122 ListVector myoldList;
123 RAbundVector* myrabund = NULL;
125 if (pDataArray->m->control_pressed) { break; }
127 string thisNamefile = pDataArray->distNames[i].begin()->second;
128 string thisDistFile = pDataArray->distNames[i].begin()->first;
129 cout << thisNamefile << '\t' << thisDistFile << endl;
130 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Reading " + thisDistFile); pDataArray->m->mothurOutEndLine();
132 ReadMatrix* myread = new ReadColumnMatrix(thisDistFile);
133 myread->setCutoff(pDataArray->cutoff);
134 NameAssignment* mynameMap = new NameAssignment(thisNamefile);
135 mynameMap->readMap();
136 cout << "done reading " << thisNamefile << endl;
137 myread->read(mynameMap);
138 cout << "done reading " << thisDistFile << endl;
139 if (pDataArray->m->control_pressed) { delete myread; delete mynameMap; break; }
141 mylist = myread->getListVector();
143 mymatrix = myread->getMatrix();
144 cout << "here" << endl;
145 delete myread; myread = NULL;
146 delete mynameMap; mynameMap = NULL;
148 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Clustering " + thisDistFile); pDataArray->m->mothurOutEndLine();
150 myrabund = new RAbundVector(mylist->getRAbundVector());
151 cout << "here" << endl;
153 if (pDataArray->method == "furthest") { mycluster = new CompleteLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
154 else if(pDataArray->method == "nearest"){ mycluster = new SingleLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
155 else if(pDataArray->method == "average"){ mycluster = new AverageLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
156 pDataArray->tag = mycluster->getTag();
157 cout << "here" << endl;
158 if (pDataArray->outputDir == "") { pDataArray->outputDir += pDataArray->m->hasPath(thisDistFile); }
159 string fileroot = pDataArray->outputDir + pDataArray->m->getRootName(pDataArray->m->getSimpleName(thisDistFile));
160 cout << "here" << endl;
162 pDataArray->m->openOutputFile(fileroot+ pDataArray->tag + ".list", listFile);
163 cout << "here" << endl;
164 pDataArray->listFiles.push_back(fileroot+ pDataArray->tag + ".list");
166 float previousDist = 0.00000;
167 float rndPreviousDist = 0.00000;
171 bool print_start = true;
172 int start = time(NULL);
173 double saveCutoff = pDataArray->cutoff;
175 while (mymatrix->getSmallDist() < pDataArray->cutoff && mymatrix->getNNodes() > 0){
177 if (pDataArray->m->control_pressed) { //clean up
178 delete mymatrix; delete mylist; delete mycluster; delete myrabund;
180 for (int i = 0; i < pDataArray->listFiles.size(); i++) { pDataArray->m->mothurRemove(pDataArray->listFiles[i]); }
181 pDataArray->listFiles.clear(); break;
184 mycluster->update(saveCutoff);
186 float dist = mymatrix->getSmallDist();
188 if (pDataArray->hard) {
189 rndDist = pDataArray->m->ceilDist(dist, pDataArray->precision);
191 rndDist = pDataArray->m->roundDist(dist, pDataArray->precision);
194 if(previousDist <= 0.0000 && dist != previousDist){
195 myoldList.setLabel("unique");
196 myoldList.print(listFile);
197 if (pDataArray->labels.count("unique") == 0) { pDataArray->labels.insert("unique"); }
199 else if(rndDist != rndPreviousDist){
200 myoldList.setLabel(toString(rndPreviousDist, pDataArray->length-1));
201 myoldList.print(listFile);
202 if (pDataArray->labels.count(toString(rndPreviousDist, pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist, pDataArray->length-1)); }
206 rndPreviousDist = rndDist;
210 cout << "here2" << endl;
211 if(previousDist <= 0.0000){
212 myoldList.setLabel("unique");
213 myoldList.print(listFile);
214 if (pDataArray->labels.count("unique") == 0) { pDataArray->labels.insert("unique"); }
216 else if(rndPreviousDist<pDataArray->cutoff){
217 myoldList.setLabel(toString(rndPreviousDist, pDataArray->length-1));
218 myoldList.print(listFile);
219 if (pDataArray->labels.count(toString(rndPreviousDist, pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist, pDataArray->length-1)); }
222 delete mymatrix; delete mylist; delete mycluster; delete myrabund;
223 mymatrix = NULL; mylist = NULL; mycluster = NULL; myrabund = NULL;
226 if (pDataArray->m->control_pressed) { //clean up
227 for (int i = 0; i < pDataArray->listFiles.size(); i++) { pDataArray->m->mothurRemove(pDataArray->listFiles[i]); }
228 pDataArray->listFiles.clear(); break;
230 cout << "here3" << endl;
231 pDataArray->m->mothurRemove(thisDistFile);
232 pDataArray->m->mothurRemove(thisNamefile);
233 cout << "here4" << endl;
234 if (saveCutoff != pDataArray->cutoff) {
235 if (pDataArray->hard) { saveCutoff = pDataArray->m->ceilDist(saveCutoff, pDataArray->precision); }
236 else { saveCutoff = pDataArray->m->roundDist(saveCutoff, pDataArray->precision); }
238 pDataArray->m->mothurOut("Cutoff was " + toString(pDataArray->cutoff) + " changed cutoff to " + toString(saveCutoff)); pDataArray->m->mothurOutEndLine();
240 cout << "here5" << endl;
241 if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff; }
244 pDataArray->cutoff = smallestCutoff;
249 catch(exception& e) {
250 pDataArray->m->errorOut(e, "ClusterSplitCommand", "MyClusterThreadFunction");