]> git.donarmstrong.com Git - mothur.git/blob - metastatscommand.h
Revert to previous commit
[mothur.git] / metastatscommand.h
1 #ifndef METASTATSCOMMAND_H
2 #define METASTATSCOMMAND_H
3
4 /*
5  *  metastatscommand.h
6  *  Mothur
7  *
8  *  Created by westcott on 9/16/10.
9  *  Copyright 2010 Schloss Lab. All rights reserved.
10  *
11  */
12  
13 #include "command.hpp"
14 #include "inputdata.h"
15 #include "sharedrabundvector.h"
16 #include "mothurmetastats.h"
17
18 class MetaStatsCommand : public Command {
19
20 public:
21         MetaStatsCommand(string);
22         MetaStatsCommand();
23         ~MetaStatsCommand() {}
24         
25         vector<string> setParameters();
26         string getCommandName()                 { return "metastats";                           }
27         string getCommandCategory()             { return "OTU-Based Approaches";        }
28         string getHelpString(); 
29         string getCitation() { return "White JR, Nagarajan N, Pop M (2009). Statistical methods for detecting differentially abundant features in clinical metagenomic samples. PLoS Comput Biol 5: e1000352. \nhttp://www.mothur.org/wiki/Metastats"; }
30         string getDescription()         { return "detects differentially abundant features in clinical metagenomic samples"; }
31
32         int execute(); 
33         void help() { m->mothurOut(getHelpString()); }  
34         
35 private:
36         struct linePair {
37                 int start;
38                 int num;
39                 linePair(int i, int j) : start(i), num(j) {}
40         };
41         vector<linePair> lines;
42         
43         GroupMap* designMap;
44         InputData* input;
45         vector<SharedRAbundVector*> lookup;
46                 
47         bool abort, allLines, pickedGroups;
48         set<string> labels; //holds labels to be used
49         string groups, label, outputDir, inputDir, designfile, sets, sharedfile;
50         vector<string> Groups, outputNames, Sets;
51         vector< vector<string> > namesOfGroupCombos;
52         int iters, processors;
53         float threshold;
54         
55         int process(vector<SharedRAbundVector*>&);
56         int driver(int, int, vector<SharedRAbundVector*>&);
57 };
58
59 /**************************************************************************************************/
60 //custom data structure for threads to use.
61 // This is passed by void pointer so it can be any data type
62 // that can be passed using a single void pointer (LPVOID).
63 struct metastatsData {
64     vector<SharedRAbundVector*> thisLookUp;
65     vector< vector<string> > namesOfGroupCombos;
66     vector<string> designMapGroups;
67     vector<string> outputNames;
68         int start;
69         int num, iters;
70         float threshold;
71         MothurOut* m;
72         string sharedfile;
73     string outputDir;
74         
75         metastatsData(){}
76         metastatsData(string sf, string oDir, MothurOut* mout, int st, int en, vector< vector<string> > ns, vector<SharedRAbundVector*> lu, vector<string> dg, int i, float thr) {
77                 sharedfile = sf;
78         outputDir = oDir;
79                 m = mout;
80                 start = st;
81                 num = en;
82         namesOfGroupCombos = ns;
83         thisLookUp = lu;
84         designMapGroups = dg;
85         iters = i;
86         threshold = thr;
87         }
88 };
89 /**************************************************************************************************/
90 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
91 #else
92 static DWORD WINAPI MyMetastatsThreadFunction(LPVOID lpParam){ 
93         metastatsData* pDataArray;
94         pDataArray = (metastatsData*)lpParam;
95         
96         try {
97                 
98         //for each combo
99                 for (int c = pDataArray->start; c < (pDataArray->start+pDataArray->num); c++) {
100                         
101                         //get set names
102                         string setA = pDataArray->namesOfGroupCombos[c][0]; 
103                         string setB = pDataArray->namesOfGroupCombos[c][1];
104             
105                         //get filename
106                         string outputFileName = pDataArray->outputDir +  pDataArray->m->getRootName(pDataArray->m->getSimpleName(pDataArray->sharedfile)) + pDataArray->thisLookUp[0]->getLabel() + "." + setA + "-" + setB + ".metastats";
107                         pDataArray->outputNames.push_back(outputFileName); 
108                         
109                         vector< vector<double> > data2; data2.resize(pDataArray->thisLookUp[0]->getNumBins());
110                         
111                         vector<SharedRAbundVector*> subset;
112                         int setACount = 0;
113                         int setBCount = 0;
114                         for (int i = 0; i < pDataArray->thisLookUp.size(); i++) {
115                                 //is this group for a set we want to compare??
116                                 //sorting the sets by putting setB at the back and setA in the front
117                                 if (pDataArray->designMapGroups[i] == setB) {  
118                                         subset.push_back(pDataArray->thisLookUp[i]);
119                                         setBCount++;
120                                 }else if (pDataArray->designMapGroups[i] == setA) {
121                                         subset.insert(subset.begin()+setACount, pDataArray->thisLookUp[i]);
122                                         setACount++;
123                                 }
124                         }
125             
126                         if ((setACount == 0) || (setBCount == 0))  { 
127                                 pDataArray->m->mothurOut("Missing shared info for " + setA + " or " + setB + ". Skipping comparison."); pDataArray->m->mothurOutEndLine(); 
128                                 pDataArray->outputNames.pop_back();
129                         }else {
130                                 //fill data
131                                 for (int j = 0; j < pDataArray->thisLookUp[0]->getNumBins(); j++) {
132                                         data2[j].resize(subset.size(), 0.0);
133                                         for (int i = 0; i < subset.size(); i++) {
134                                                 data2[j][i] = (subset[i]->getAbundance(j));
135                                         }
136                                 }
137                                 
138                                 pDataArray->m->mothurOut("Comparing " + setA + " and " + setB + "..."); pDataArray->m->mothurOutEndLine(); 
139                                 
140                                 pDataArray->m->mothurOutEndLine();
141                                 MothurMetastats mothurMeta(pDataArray->threshold, pDataArray->iters);
142                                 mothurMeta.runMetastats(outputFileName, data2, setACount);
143                                 pDataArray->m->mothurOutEndLine();
144                                 pDataArray->m->mothurOutEndLine(); 
145                         }
146         }
147                 
148                 return 0;
149                 
150         }
151         catch(exception& e) {
152                 pDataArray->m->errorOut(e, "MetaStatsCommand", "MyMetastatsThreadFunction");
153                 exit(1);
154         }
155
156 #endif
157
158
159
160 #endif
161