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