]> git.donarmstrong.com Git - mothur.git/blob - clustersplitcommand.h
Revert to previous commit
[mothur.git] / clustersplitcommand.h
1 #ifndef CLUSTERSPLITCOMMAND_H
2 #define CLUSTERSPLITCOMMAND_H
3
4 /*
5  *  clustersplitcommand.h
6  *  Mothur
7  *
8  *  Created by westcott on 5/19/10.
9  *  Copyright 2010 Schloss Lab. All rights reserved.
10  *
11  */
12  
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
27 class ClusterSplitCommand : public Command {
28         
29 public:
30         ClusterSplitCommand(string);
31         ClusterSplitCommand();
32         ~ClusterSplitCommand() {}
33         
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"; }
40         
41         int execute(); 
42         void help() { m->mothurOut(getHelpString()); }  
43
44 private:
45         vector<int> processIDS;   //processid
46         vector<string> outputNames;
47         
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;
52         time_t start;
53         ofstream outList, outRabund, outSabund;
54         
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> >);
61 };
62
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).
78 struct clusterData {
79         set<string> labels;
80         vector < map<string, string> > distNames; 
81         string method; 
82     MothurOut* m;
83         double cutoff, precision;
84     string tag, outputDir;
85     vector<string> listFiles;
86     bool hard;
87     int length, threadID;
88         
89         
90         clusterData(){}
91         clusterData(vector < map<string, string> > dv, MothurOut* mout, double cu, string me, string ou, bool hd, double pre, int len, int th) {
92                 distNames = dv;
93                 m = mout;
94                 cutoff = cu;
95         method = me;
96                 outputDir = ou;
97         hard = hd;
98         precision = pre;
99         length = len;
100         threadID = th;
101         }
102 };
103
104 /**************************************************************************************************/
105 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
106 #else
107 static DWORD WINAPI MyClusterThreadFunction(LPVOID lpParam){ 
108         clusterData* pDataArray;
109         pDataArray = (clusterData*)lpParam;
110         
111         try {
112                 cout << "starting " << endl;            
113                 
114                 double smallestCutoff = pDataArray->cutoff;
115                 
116                 //cluster each distance file
117                 for (int i = 0; i < pDataArray->distNames.size(); i++) {
118             
119             Cluster* mycluster = NULL;
120             SparseMatrix* mymatrix = NULL;
121             ListVector* mylist = NULL;
122             ListVector myoldList;
123             RAbundVector* myrabund = NULL;
124                         
125                         if (pDataArray->m->control_pressed) { break; }
126                         
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();
131                         
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; }
140             
141                         mylist = myread->getListVector();
142                         myoldList = *mylist;
143                         mymatrix = myread->getMatrix();
144             cout << "here" << endl;     
145                         delete myread; myread = NULL;
146                         delete mynameMap; mynameMap = NULL;
147                         
148             pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Clustering " + thisDistFile); pDataArray->m->mothurOutEndLine();
149             
150                         myrabund = new RAbundVector(mylist->getRAbundVector());
151                          cout << "here" << endl;        
152                         //create cluster
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;        
161                         ofstream listFile;
162                         pDataArray->m->openOutputFile(fileroot+ pDataArray->tag + ".list",      listFile);
163              cout << "here" << endl;    
164                         pDataArray->listFiles.push_back(fileroot+ pDataArray->tag + ".list");
165             
166                         float previousDist = 0.00000;
167                         float rndPreviousDist = 0.00000;
168                         
169                         myoldList = *mylist;
170         
171                         bool print_start = true;
172                         int start = time(NULL);
173                         double saveCutoff = pDataArray->cutoff;
174             
175                         while (mymatrix->getSmallDist() < pDataArray->cutoff && mymatrix->getNNodes() > 0){
176                 
177                                 if (pDataArray->m->control_pressed) { //clean up
178                                         delete mymatrix; delete mylist; delete mycluster; delete myrabund;
179                                         listFile.close();
180                                         for (int i = 0; i < pDataArray->listFiles.size(); i++) {        pDataArray->m->mothurRemove(pDataArray->listFiles[i]);  }
181                                         pDataArray->listFiles.clear(); break;
182                                 }
183                 
184                                 mycluster->update(saveCutoff);
185                 
186                                 float dist = mymatrix->getSmallDist();
187                                 float rndDist;
188                                 if (pDataArray->hard) {
189                                         rndDist = pDataArray->m->ceilDist(dist, pDataArray->precision); 
190                                 }else{
191                                         rndDist = pDataArray->m->roundDist(dist, pDataArray->precision); 
192                                 }
193                 
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");  }
198                                 }
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)); }
203                                 }
204                 
205                                 previousDist = dist;
206                                 rndPreviousDist = rndDist;
207                                 myoldList = *mylist;
208                         }
209             
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"); }
215                         }
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)); }
220                         }
221             
222                         delete mymatrix; delete mylist; delete mycluster; delete myrabund; 
223             mymatrix = NULL; mylist = NULL; mycluster = NULL; myrabund = NULL;
224                         listFile.close();
225                         
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;
229                         }
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);  }
237                 
238                                 pDataArray->m->mothurOut("Cutoff was " + toString(pDataArray->cutoff) + " changed cutoff to " + toString(saveCutoff)); pDataArray->m->mothurOutEndLine();  
239                         }
240                          cout << "here5" << endl;       
241                         if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff;  }
242                 }
243                 
244                 pDataArray->cutoff = smallestCutoff;
245                 
246                 return 0;
247                 
248         }
249         catch(exception& e) {
250                 pDataArray->m->errorOut(e, "ClusterSplitCommand", "MyClusterThreadFunction");
251                 exit(1);
252         }
253
254 #endif
255
256
257
258
259 #endif
260