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