]> git.donarmstrong.com Git - mothur.git/blob - clustersplitcommand.h
added sparseDistanceMatrix class. Modified cluster commands to use the new sparse...
[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 "sparsedistancematrix.h"
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"
27
28 class ClusterSplitCommand : public Command {
29         
30 public:
31         ClusterSplitCommand(string);
32         ClusterSplitCommand();
33         ~ClusterSplitCommand() {}
34         
35         vector<string> setParameters();
36         string getCommandName()                 { return "cluster.split";               }
37         string getCommandCategory()             { return "Clustering";                  }
38         string getOutputFileNameTag(string, string);
39         string getHelpString(); 
40         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"; }
41         string getDescription()         { return "splits your sequences by distance or taxonomy then clusters into OTUs"; }
42         
43         int execute(); 
44         void help() { m->mothurOut(getHelpString()); }  
45
46 private:
47         vector<int> processIDS;   //processid
48         vector<string> outputNames;
49         
50         string method, fileroot, tag, outputDir, phylipfile, columnfile, namefile, distfile, format, showabund, timing, splitmethod, taxFile, fastafile;
51         double cutoff, splitcutoff;
52         int precision, length, processors, taxLevelCutoff;
53         bool print_start, abort, hard, large, classic;
54         time_t start;
55         ofstream outList, outRabund, outSabund;
56         
57         void printData(ListVector*);
58         vector<string> createProcesses(vector< map<string, string> >, set<string>&);
59         vector<string> cluster(vector< map<string, string> >, set<string>&);
60     string clusterFile(string, string, set<string>&, double&);
61     string clusterClassicFile(string, string, set<string>&, double&);
62         int mergeLists(vector<string>, map<float, int>, ListVector*);
63         map<float, int> completeListFile(vector<string>, string, set<string>&, ListVector*&);
64         int createMergedDistanceFile(vector< map<string, string> >);
65 };
66
67 /////////////////not working for Windows////////////////////////////////////////////////////////////
68 // getting an access violation error.  This is most likely caused by the 
69 // threads stepping on eachother's structures, as I can run the thread function and the cluster fuction 
70 // in separately without errors occuring.  I suspect it may be in the use of the
71 // static class mothurOut, but I can't pinpoint the problem.  All other objects are made new
72 // within the thread.  MothurOut is used by almost all the classes in mothur, so if this was 
73 // really the cause I would expect to see all the windows threaded commands to have issues, but not 
74 // all do. So far, shhh.flows and trim.flows have similiar problems. Other thoughts, could it have 
75 // anything to do with mothur's use of copy constructors in many of our data structures. ie. listvector 
76 // is copied by nameassignment and passed to read which passes to the thread?  -westcott 2-8-12
77 ////////////////////////////////////////////////////////////////////////////////////////////////////
78 /**************************************************************************************************/
79 //custom data structure for threads to use.
80 // This is passed by void pointer so it can be any data type
81 // that can be passed using a single void pointer (LPVOID).
82 struct clusterData {
83         set<string> labels;
84         vector < map<string, string> > distNames; 
85         string method; 
86     MothurOut* m;
87         double cutoff, precision;
88     string tag, outputDir;
89     vector<string> listFiles;
90     bool hard;
91     int length, threadID;
92         
93         
94         clusterData(){}
95         clusterData(vector < map<string, string> > dv, MothurOut* mout, double cu, string me, string ou, bool hd, double pre, int len, int th) {
96                 distNames = dv;
97                 m = mout;
98                 cutoff = cu;
99         method = me;
100                 outputDir = ou;
101         hard = hd;
102         precision = pre;
103         length = len;
104         threadID = th;
105         }
106 };
107
108 /**************************************************************************************************/
109 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
110 #else
111 static DWORD WINAPI MyClusterThreadFunction(LPVOID lpParam){ 
112         clusterData* pDataArray;
113         pDataArray = (clusterData*)lpParam;
114         
115         try {
116                 cout << "starting " << endl;            
117                 
118                 double smallestCutoff = pDataArray->cutoff;
119                 
120                 //cluster each distance file
121                 for (int i = 0; i < pDataArray->distNames.size(); i++) {
122             
123             Cluster* mycluster = NULL;
124             SparseMatrix* mymatrix = NULL;
125             ListVector* mylist = NULL;
126             ListVector myoldList;
127             RAbundVector* myrabund = NULL;
128                         
129                         if (pDataArray->m->control_pressed) { break; }
130                         
131                         string thisNamefile = pDataArray->distNames[i].begin()->second;
132                         string thisDistFile = pDataArray->distNames[i].begin()->first;
133             cout << thisNamefile << '\t' << thisDistFile << endl;       
134                         pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Reading " + thisDistFile); pDataArray->m->mothurOutEndLine();
135                         
136                         ReadMatrix* myread = new ReadColumnMatrix(thisDistFile);        
137                         myread->setCutoff(pDataArray->cutoff);
138                         NameAssignment* mynameMap = new NameAssignment(thisNamefile);
139                         mynameMap->readMap();
140             cout << "done reading " << thisNamefile << endl;  
141                         myread->read(mynameMap);
142                         cout << "done reading " << thisDistFile << endl;  
143                         if (pDataArray->m->control_pressed) {  delete myread; delete mynameMap; break; }
144             
145                         mylist = myread->getListVector();
146                         myoldList = *mylist;
147                         mymatrix = myread->getMatrix();
148             cout << "here" << endl;     
149                         delete myread; myread = NULL;
150                         delete mynameMap; mynameMap = NULL;
151                         
152             pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Clustering " + thisDistFile); pDataArray->m->mothurOutEndLine();
153             
154                         myrabund = new RAbundVector(mylist->getRAbundVector());
155                          cout << "here" << endl;        
156                         //create cluster
157                         if (pDataArray->method == "furthest")   {       mycluster = new CompleteLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
158                         else if(pDataArray->method == "nearest"){       mycluster = new SingleLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method); }
159                         else if(pDataArray->method == "average"){       mycluster = new AverageLinkage(myrabund, mylist, mymatrix, pDataArray->cutoff, pDataArray->method);     }
160                         pDataArray->tag = mycluster->getTag();
161              cout << "here" << endl;    
162                         if (pDataArray->outputDir == "") { pDataArray->outputDir += pDataArray->m->hasPath(thisDistFile); }
163                         string fileroot = pDataArray->outputDir + pDataArray->m->getRootName(pDataArray->m->getSimpleName(thisDistFile));
164                          cout << "here" << endl;        
165                         ofstream listFile;
166                         pDataArray->m->openOutputFile(fileroot+ pDataArray->tag + ".list",      listFile);
167              cout << "here" << endl;    
168                         pDataArray->listFiles.push_back(fileroot+ pDataArray->tag + ".list");
169             
170                         float previousDist = 0.00000;
171                         float rndPreviousDist = 0.00000;
172                         
173                         myoldList = *mylist;
174         
175                         bool print_start = true;
176                         int start = time(NULL);
177                         double saveCutoff = pDataArray->cutoff;
178             
179                         while (mymatrix->getSmallDist() < pDataArray->cutoff && mymatrix->getNNodes() > 0){
180                 
181                                 if (pDataArray->m->control_pressed) { //clean up
182                                         delete mymatrix; delete mylist; delete mycluster; delete myrabund;
183                                         listFile.close();
184                                         for (int i = 0; i < pDataArray->listFiles.size(); i++) {        pDataArray->m->mothurRemove(pDataArray->listFiles[i]);  }
185                                         pDataArray->listFiles.clear(); break;
186                                 }
187                 
188                                 mycluster->update(saveCutoff);
189                 
190                                 float dist = mymatrix->getSmallDist();
191                                 float rndDist;
192                                 if (pDataArray->hard) {
193                                         rndDist = pDataArray->m->ceilDist(dist, pDataArray->precision); 
194                                 }else{
195                                         rndDist = pDataArray->m->roundDist(dist, pDataArray->precision); 
196                                 }
197                 
198                                 if(previousDist <= 0.0000 && dist != previousDist){
199                                         myoldList.setLabel("unique");
200                                         myoldList.print(listFile);
201                                         if (pDataArray->labels.count("unique") == 0) {  pDataArray->labels.insert("unique");  }
202                                 }
203                                 else if(rndDist != rndPreviousDist){
204                                         myoldList.setLabel(toString(rndPreviousDist,  pDataArray->length-1));
205                                         myoldList.print(listFile);
206                                         if (pDataArray->labels.count(toString(rndPreviousDist,  pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist,  pDataArray->length-1)); }
207                                 }
208                 
209                                 previousDist = dist;
210                                 rndPreviousDist = rndDist;
211                                 myoldList = *mylist;
212                         }
213             
214              cout << "here2" << endl;   
215                         if(previousDist <= 0.0000){
216                                 myoldList.setLabel("unique");
217                                 myoldList.print(listFile);
218                                 if (pDataArray->labels.count("unique") == 0) { pDataArray->labels.insert("unique"); }
219                         }
220                         else if(rndPreviousDist<pDataArray->cutoff){
221                                 myoldList.setLabel(toString(rndPreviousDist,  pDataArray->length-1));
222                                 myoldList.print(listFile);
223                                 if (pDataArray->labels.count(toString(rndPreviousDist,  pDataArray->length-1)) == 0) { pDataArray->labels.insert(toString(rndPreviousDist,  pDataArray->length-1)); }
224                         }
225             
226                         delete mymatrix; delete mylist; delete mycluster; delete myrabund; 
227             mymatrix = NULL; mylist = NULL; mycluster = NULL; myrabund = NULL;
228                         listFile.close();
229                         
230                         if (pDataArray->m->control_pressed) { //clean up
231                                 for (int i = 0; i < pDataArray->listFiles.size(); i++) {        pDataArray->m->mothurRemove(pDataArray->listFiles[i]);  }
232                                 pDataArray->listFiles.clear(); break;
233                         }
234                          cout << "here3" << endl;       
235                         pDataArray->m->mothurRemove(thisDistFile);
236                         pDataArray->m->mothurRemove(thisNamefile);
237                          cout << "here4" << endl;       
238                         if (saveCutoff != pDataArray->cutoff) { 
239                                 if (pDataArray->hard)   {  saveCutoff = pDataArray->m->ceilDist(saveCutoff, pDataArray->precision);     }
240                                 else            {       saveCutoff = pDataArray->m->roundDist(saveCutoff, pDataArray->precision);  }
241                 
242                                 pDataArray->m->mothurOut("Cutoff was " + toString(pDataArray->cutoff) + " changed cutoff to " + toString(saveCutoff)); pDataArray->m->mothurOutEndLine();  
243                         }
244                          cout << "here5" << endl;       
245                         if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff;  }
246                 }
247                 
248                 pDataArray->cutoff = smallestCutoff;
249                 
250                 return 0;
251                 
252         }
253         catch(exception& e) {
254                 pDataArray->m->errorOut(e, "ClusterSplitCommand", "MyClusterThreadFunction");
255                 exit(1);
256         }
257
258 #endif
259
260
261
262
263 #endif
264