]> git.donarmstrong.com Git - mothur.git/blob - matrixoutputcommand.h
added unix to ifdefs. minor changes while testing 1.24.0.
[mothur.git] / matrixoutputcommand.h
1 #ifndef MATRIXOUTPUTCOMMAND_H
2 #define MATRIXOUTPUTCOMMAND_H
3
4 /*
5  *  matrixoutputcommand.h
6  *  Mothur
7  *
8  *  Created by Sarah Westcott on 5/20/09.
9  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10  *
11  */ 
12 #include "command.hpp"
13 #include "inputdata.h"
14 #include "groupmap.h"
15 #include "validcalculator.h"
16 #include "sharedsobscollectsummary.h"
17 #include "sharedchao1.h"
18 #include "sharedace.h"
19 #include "sharednseqs.h"
20 #include "sharedjabund.h"
21 #include "sharedsorabund.h"
22 #include "sharedjclass.h"
23 #include "sharedsorclass.h"
24 #include "sharedjest.h"
25 #include "sharedsorest.h"
26 #include "sharedthetayc.h"
27 #include "sharedthetan.h"
28 #include "sharedkstest.h"
29 #include "whittaker.h"
30 #include "sharedochiai.h"
31 #include "sharedanderbergs.h"
32 #include "sharedkulczynski.h"
33 #include "sharedkulczynskicody.h"
34 #include "sharedlennon.h"
35 #include "sharedmorisitahorn.h"
36 #include "sharedbraycurtis.h"
37 #include "sharedjackknife.h"
38 #include "whittaker.h"
39 #include "odum.h"
40 #include "canberra.h"
41 #include "structeuclidean.h"
42 #include "structchord.h"
43 #include "hellinger.h"
44 #include "manhattan.h"
45 #include "structpearson.h"
46 #include "soergel.h"
47 #include "spearman.h"
48 #include "structkulczynski.h"
49 #include "structchi2.h"
50 #include "speciesprofile.h"
51 #include "hamming.h"
52 #include "gower.h"
53 #include "memchi2.h"
54 #include "memchord.h"
55 #include "memeuclidean.h"
56 #include "mempearson.h"
57
58
59 // aka. dist.shared()
60
61 /* This command create a tree file for each similarity calculator at distance level, using various calculators to find the similiarity between groups. 
62         The user can select the labels they wish to use as well as the groups they would like included.
63         They can also use as many or as few calculators as they wish. */
64         
65
66 class MatrixOutputCommand : public Command {
67         
68 public:
69         MatrixOutputCommand(string);
70         MatrixOutputCommand();  
71         ~MatrixOutputCommand();
72         
73         vector<string> setParameters();
74         string getCommandName()                 { return "dist.shared";                         }
75         string getCommandCategory()             { return "OTU-Based Approaches";        }
76         string getHelpString(); 
77         string getCitation() { return "http://www.mothur.org/wiki/Dist.shared"; }
78         string getDescription()         { return "generate a distance matrix that describes the dissimilarity among multiple groups"; }
79
80         
81         int execute(); 
82         void help() { m->mothurOut(getHelpString()); }  
83         
84 private:
85         struct linePair {
86                 int start;
87                 int end;
88         };
89         vector<linePair> lines;
90         
91         void printSims(ostream&, vector< vector<float> >&);
92         int process(vector<SharedRAbundVector*>);
93         
94         vector<Calculator*> matrixCalculators;
95         //vector< vector<float> > simMatrix;
96         InputData* input;
97         vector<SharedRAbundVector*> lookup;
98         string exportFileName, output, sharedfile;
99         int numGroups, processors;
100         ofstream out;
101
102         bool abort, allLines;
103         set<string> labels; //holds labels to be used
104         string outputFile, calc, groups, label, outputDir;
105         vector<string>  Estimators, Groups, outputNames; //holds estimators to be used
106         int process(vector<SharedRAbundVector*>, string, string);
107         int driver(vector<SharedRAbundVector*>, int, int, vector< vector<seqDist> >&);
108
109 };
110         
111 /**************************************************************************************************/
112 //custom data structure for threads to use.
113 // This is passed by void pointer so it can be any data type
114 // that can be passed using a single void pointer (LPVOID).
115 struct distSharedData {
116     vector<SharedRAbundVector*> thisLookup;
117     vector< vector<seqDist> > calcDists;
118     vector<string>  Estimators;
119         unsigned long long start;
120         unsigned long long end;
121         MothurOut* m;
122         
123         distSharedData(){}
124         distSharedData(MothurOut* mout, unsigned long long st, unsigned long long en, vector<string> est, vector<SharedRAbundVector*> lu) {
125                 m = mout;
126                 start = st;
127                 end = en;
128         Estimators = est;
129         thisLookup = lu;
130         }
131 };
132 /**************************************************************************************************/
133 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
134 #else
135 static DWORD WINAPI MyDistSharedThreadFunction(LPVOID lpParam){ 
136         distSharedData* pDataArray;
137         pDataArray = (distSharedData*)lpParam;
138         
139         try {
140         
141         vector<Calculator*> matrixCalculators;
142         ValidCalculators validCalculator;
143         for (int i=0; i<pDataArray->Estimators.size(); i++) {
144             if (validCalculator.isValidCalculator("matrix", pDataArray->Estimators[i]) == true) { 
145                 if (pDataArray->Estimators[i] == "sharedsobs") { 
146                     matrixCalculators.push_back(new SharedSobsCS());
147                 }else if (pDataArray->Estimators[i] == "sharedchao") { 
148                     matrixCalculators.push_back(new SharedChao1());
149                 }else if (pDataArray->Estimators[i] == "sharedace") { 
150                     matrixCalculators.push_back(new SharedAce());
151                 }else if (pDataArray->Estimators[i] == "jabund") {      
152                     matrixCalculators.push_back(new JAbund());
153                 }else if (pDataArray->Estimators[i] == "sorabund") { 
154                     matrixCalculators.push_back(new SorAbund());
155                 }else if (pDataArray->Estimators[i] == "jclass") { 
156                     matrixCalculators.push_back(new Jclass());
157                 }else if (pDataArray->Estimators[i] == "sorclass") { 
158                     matrixCalculators.push_back(new SorClass());
159                 }else if (pDataArray->Estimators[i] == "jest") { 
160                     matrixCalculators.push_back(new Jest());
161                 }else if (pDataArray->Estimators[i] == "sorest") { 
162                     matrixCalculators.push_back(new SorEst());
163                 }else if (pDataArray->Estimators[i] == "thetayc") { 
164                     matrixCalculators.push_back(new ThetaYC());
165                 }else if (pDataArray->Estimators[i] == "thetan") { 
166                     matrixCalculators.push_back(new ThetaN());
167                 }else if (pDataArray->Estimators[i] == "kstest") { 
168                     matrixCalculators.push_back(new KSTest());
169                 }else if (pDataArray->Estimators[i] == "sharednseqs") { 
170                     matrixCalculators.push_back(new SharedNSeqs());
171                 }else if (pDataArray->Estimators[i] == "ochiai") { 
172                     matrixCalculators.push_back(new Ochiai());
173                 }else if (pDataArray->Estimators[i] == "anderberg") { 
174                     matrixCalculators.push_back(new Anderberg());
175                 }else if (pDataArray->Estimators[i] == "kulczynski") { 
176                     matrixCalculators.push_back(new Kulczynski());
177                 }else if (pDataArray->Estimators[i] == "kulczynskicody") { 
178                     matrixCalculators.push_back(new KulczynskiCody());
179                 }else if (pDataArray->Estimators[i] == "lennon") { 
180                     matrixCalculators.push_back(new Lennon());
181                 }else if (pDataArray->Estimators[i] == "morisitahorn") { 
182                     matrixCalculators.push_back(new MorHorn());
183                 }else if (pDataArray->Estimators[i] == "braycurtis") { 
184                     matrixCalculators.push_back(new BrayCurtis());
185                 }else if (pDataArray->Estimators[i] == "whittaker") { 
186                     matrixCalculators.push_back(new Whittaker());
187                 }else if (pDataArray->Estimators[i] == "odum") { 
188                     matrixCalculators.push_back(new Odum());
189                 }else if (pDataArray->Estimators[i] == "canberra") { 
190                     matrixCalculators.push_back(new Canberra());
191                 }else if (pDataArray->Estimators[i] == "structeuclidean") { 
192                     matrixCalculators.push_back(new StructEuclidean());
193                 }else if (pDataArray->Estimators[i] == "structchord") { 
194                     matrixCalculators.push_back(new StructChord());
195                 }else if (pDataArray->Estimators[i] == "hellinger") { 
196                     matrixCalculators.push_back(new Hellinger());
197                 }else if (pDataArray->Estimators[i] == "manhattan") { 
198                     matrixCalculators.push_back(new Manhattan());
199                 }else if (pDataArray->Estimators[i] == "structpearson") { 
200                     matrixCalculators.push_back(new StructPearson());
201                 }else if (pDataArray->Estimators[i] == "soergel") { 
202                     matrixCalculators.push_back(new Soergel());
203                 }else if (pDataArray->Estimators[i] == "spearman") { 
204                     matrixCalculators.push_back(new Spearman());
205                 }else if (pDataArray->Estimators[i] == "structkulczynski") { 
206                     matrixCalculators.push_back(new StructKulczynski());
207                 }else if (pDataArray->Estimators[i] == "speciesprofile") { 
208                     matrixCalculators.push_back(new SpeciesProfile());
209                 }else if (pDataArray->Estimators[i] == "hamming") { 
210                     matrixCalculators.push_back(new Hamming());
211                 }else if (pDataArray->Estimators[i] == "structchi2") { 
212                     matrixCalculators.push_back(new StructChi2());
213                 }else if (pDataArray->Estimators[i] == "gower") { 
214                     matrixCalculators.push_back(new Gower());
215                 }else if (pDataArray->Estimators[i] == "memchi2") { 
216                     matrixCalculators.push_back(new MemChi2());
217                 }else if (pDataArray->Estimators[i] == "memchord") { 
218                     matrixCalculators.push_back(new MemChord());
219                 }else if (pDataArray->Estimators[i] == "memeuclidean") { 
220                     matrixCalculators.push_back(new MemEuclidean());
221                 }else if (pDataArray->Estimators[i] == "mempearson") { 
222                     matrixCalculators.push_back(new MemPearson());
223                 }
224             }
225         }
226         
227         pDataArray->calcDists.resize(matrixCalculators.size());
228                         
229                 vector<SharedRAbundVector*> subset;
230                 for (int k = pDataArray->start; k < pDataArray->end; k++) { // pass cdd each set of groups to compare
231                         
232                         for (int l = 0; l < k; l++) {
233                                 
234                                 if (k != l) { //we dont need to similiarity of a groups to itself
235                                         subset.clear(); //clear out old pair of sharedrabunds
236                                         //add new pair of sharedrabunds
237                                         subset.push_back(pDataArray->thisLookup[k]); subset.push_back(pDataArray->thisLookup[l]); 
238                                         
239                                         for(int i=0;i<matrixCalculators.size();i++) {
240                                                 
241                                                 //if this calc needs all groups to calculate the pair load all groups
242                                                 if (matrixCalculators[i]->getNeedsAll()) { 
243                                                         //load subset with rest of lookup for those calcs that need everyone to calc for a pair
244                                                         for (int w = 0; w < pDataArray->thisLookup.size(); w++) {
245                                                                 if ((w != k) && (w != l)) { subset.push_back(pDataArray->thisLookup[w]); }
246                                                         }
247                                                 }
248                                                 
249                                                 vector<double> tempdata = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
250                                                 
251                                                 if (pDataArray->m->control_pressed) { return 1; }
252                                                 
253                                                 seqDist temp(l, k, tempdata[0]);
254                                                 pDataArray->calcDists[i].push_back(temp);
255                                         }
256                                 }
257                         }
258                 }
259         
260         for(int i=0;i<matrixCalculators.size();i++){  delete matrixCalculators[i]; }
261                 
262                 return 0;
263                 
264         }
265         catch(exception& e) {
266                 pDataArray->m->errorOut(e, "MatrixOutputCommand", "MyDistSharedThreadFunction");
267                 exit(1);
268         }
269
270 #endif
271         
272 #endif
273