]> git.donarmstrong.com Git - mothur.git/blob - clustersplitcommand.h
Merge branch 'master' of https://github.com/mothur/mothur.git
[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 #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 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"; }
41         
42         int execute(); 
43         void help() { m->mothurOut(getHelpString()); }  
44
45 private:
46         vector<int> processIDS;   //processid
47         vector<string> outputNames;
48         
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;
53         time_t start;
54         ofstream outList, outRabund, outSabund;
55         
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> >);
64 };
65
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).
81 struct clusterData {
82         set<string> labels;
83         vector < map<string, string> > distNames; 
84         string method; 
85     MothurOut* m;
86         double cutoff, precision;
87     string tag, outputDir;
88     vector<string> listFiles;
89     bool hard;
90     int length, threadID;
91         
92         
93         clusterData(){}
94         clusterData(vector < map<string, string> > dv, MothurOut* mout, double cu, string me, string ou, bool hd, double pre, int len, int th) {
95                 distNames = dv;
96                 m = mout;
97                 cutoff = cu;
98         method = me;
99                 outputDir = ou;
100         hard = hd;
101         precision = pre;
102         length = len;
103         threadID = th;
104         }
105 };
106
107 /**************************************************************************************************/
108 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
109 #else
110 static DWORD WINAPI MyClusterThreadFunction(LPVOID lpParam){ 
111         clusterData* pDataArray;
112         pDataArray = (clusterData*)lpParam;
113         
114         try {
115                 cout << "starting " << endl;            
116                 
117                 double smallestCutoff = pDataArray->cutoff;
118                 
119                 //cluster each distance file
120                 for (int i = 0; i < pDataArray->distNames.size(); i++) {
121             
122             Cluster* mycluster = NULL;
123             SparseMatrix* mymatrix = NULL;
124             ListVector* mylist = NULL;
125             ListVector myoldList;
126             RAbundVector* myrabund = NULL;
127                         
128                         if (pDataArray->m->control_pressed) { break; }
129                         
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();
134                         
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; }
143             
144                         mylist = myread->getListVector();
145                         myoldList = *mylist;
146                         mymatrix = myread->getMatrix();
147             cout << "here" << endl;     
148                         delete myread; myread = NULL;
149                         delete mynameMap; mynameMap = NULL;
150                         
151             pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Clustering " + thisDistFile); pDataArray->m->mothurOutEndLine();
152             
153                         myrabund = new RAbundVector(mylist->getRAbundVector());
154                          cout << "here" << endl;        
155                         //create cluster
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;        
164                         ofstream listFile;
165                         pDataArray->m->openOutputFile(fileroot+ pDataArray->tag + ".list",      listFile);
166              cout << "here" << endl;    
167                         pDataArray->listFiles.push_back(fileroot+ pDataArray->tag + ".list");
168             
169                         float previousDist = 0.00000;
170                         float rndPreviousDist = 0.00000;
171                         
172                         myoldList = *mylist;
173         
174                         bool print_start = true;
175                         int start = time(NULL);
176                         double saveCutoff = pDataArray->cutoff;
177             
178                         while (mymatrix->getSmallDist() < pDataArray->cutoff && mymatrix->getNNodes() > 0){
179                 
180                                 if (pDataArray->m->control_pressed) { //clean up
181                                         delete mymatrix; delete mylist; delete mycluster; delete myrabund;
182                                         listFile.close();
183                                         for (int i = 0; i < pDataArray->listFiles.size(); i++) {        pDataArray->m->mothurRemove(pDataArray->listFiles[i]);  }
184                                         pDataArray->listFiles.clear(); break;
185                                 }
186                 
187                                 mycluster->update(saveCutoff);
188                 
189                                 float dist = mymatrix->getSmallDist();
190                                 float rndDist;
191                                 if (pDataArray->hard) {
192                                         rndDist = pDataArray->m->ceilDist(dist, pDataArray->precision); 
193                                 }else{
194                                         rndDist = pDataArray->m->roundDist(dist, pDataArray->precision); 
195                                 }
196                 
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");  }
201                                 }
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)); }
206                                 }
207                 
208                                 previousDist = dist;
209                                 rndPreviousDist = rndDist;
210                                 myoldList = *mylist;
211                         }
212             
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"); }
218                         }
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)); }
223                         }
224             
225                         delete mymatrix; delete mylist; delete mycluster; delete myrabund; 
226             mymatrix = NULL; mylist = NULL; mycluster = NULL; myrabund = NULL;
227                         listFile.close();
228                         
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;
232                         }
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);  }
240                 
241                                 pDataArray->m->mothurOut("Cutoff was " + toString(pDataArray->cutoff) + " changed cutoff to " + toString(saveCutoff)); pDataArray->m->mothurOutEndLine();  
242                         }
243                          cout << "here5" << endl;       
244                         if (saveCutoff < smallestCutoff) { smallestCutoff = saveCutoff;  }
245                 }
246                 
247                 pDataArray->cutoff = smallestCutoff;
248                 
249                 return 0;
250                 
251         }
252         catch(exception& e) {
253                 pDataArray->m->errorOut(e, "ClusterSplitCommand", "MyClusterThreadFunction");
254                 exit(1);
255         }
256
257 #endif
258
259
260
261
262 #endif
263