]> git.donarmstrong.com Git - mothur.git/blob - metastatscommand.h
Merge remote-tracking branch 'mothur/master'
[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 getOutputFileNameTag(string, string);
29         string getHelpString(); 
30         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"; }
31         string getDescription()         { return "detects differentially abundant features in clinical metagenomic samples"; }
32
33         int execute(); 
34         void help() { m->mothurOut(getHelpString()); }  
35         
36 private:
37         struct linePair {
38                 int start;
39                 int num;
40                 linePair(int i, int j) : start(i), num(j) {}
41         };
42         vector<linePair> lines;
43         
44         GroupMap* designMap;
45         InputData* input;
46         vector<SharedRAbundVector*> lookup;
47                 
48         bool abort, allLines, pickedGroups;
49         set<string> labels; //holds labels to be used
50         string groups, label, outputDir, inputDir, designfile, sets, sharedfile;
51         vector<string> Groups, outputNames, Sets;
52         vector< vector<string> > namesOfGroupCombos;
53         int iters, processors;
54         float threshold;
55         
56         int process(vector<SharedRAbundVector*>&);
57         int driver(int, int, vector<SharedRAbundVector*>&);
58 };
59
60 /**************************************************************************************************/
61 //custom data structure for threads to use.
62 // This is passed by void pointer so it can be any data type
63 // that can be passed using a single void pointer (LPVOID).
64 struct metastatsData {
65     vector<SharedRAbundVector*> thisLookUp;
66     vector< vector<string> > namesOfGroupCombos;
67     vector<string> designMapGroups;
68     vector<string> outputNames;
69         int start;
70         int num, iters;
71         float threshold;
72         MothurOut* m;
73         string sharedfile;
74     string outputDir;
75         
76         metastatsData(){}
77         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) {
78                 sharedfile = sf;
79         outputDir = oDir;
80                 m = mout;
81                 start = st;
82                 num = en;
83         namesOfGroupCombos = ns;
84         thisLookUp = lu;
85         designMapGroups = dg;
86         iters = i;
87         threshold = thr;
88         }
89 };
90 /**************************************************************************************************/
91 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
92 #else
93 static DWORD WINAPI MyMetastatsThreadFunction(LPVOID lpParam){ 
94         metastatsData* pDataArray;
95         pDataArray = (metastatsData*)lpParam;
96         
97         try {
98                 
99         //for each combo
100                 for (int c = pDataArray->start; c < (pDataArray->start+pDataArray->num); c++) {
101                         
102                         //get set names
103                         string setA = pDataArray->namesOfGroupCombos[c][0]; 
104                         string setB = pDataArray->namesOfGroupCombos[c][1];
105             
106                         //get filename
107                         string outputFileName = pDataArray->outputDir +  pDataArray->m->getRootName(pDataArray->m->getSimpleName(pDataArray->sharedfile)) + pDataArray->thisLookUp[0]->getLabel() + "." + setA + "-" + setB + ".metastats";
108                         pDataArray->outputNames.push_back(outputFileName); 
109                         
110                         vector< vector<double> > data2; data2.resize(pDataArray->thisLookUp[0]->getNumBins());
111                         
112                         vector<SharedRAbundVector*> subset;
113                         int setACount = 0;
114                         int setBCount = 0;
115                         for (int i = 0; i < pDataArray->thisLookUp.size(); i++) {
116                                 //is this group for a set we want to compare??
117                                 //sorting the sets by putting setB at the back and setA in the front
118                                 if (pDataArray->designMapGroups[i] == setB) {  
119                                         subset.push_back(pDataArray->thisLookUp[i]);
120                                         setBCount++;
121                                 }else if (pDataArray->designMapGroups[i] == setA) {
122                                         subset.insert(subset.begin()+setACount, pDataArray->thisLookUp[i]);
123                                         setACount++;
124                                 }
125                         }
126             
127                         if ((setACount == 0) || (setBCount == 0))  { 
128                                 pDataArray->m->mothurOut("Missing shared info for " + setA + " or " + setB + ". Skipping comparison."); pDataArray->m->mothurOutEndLine(); 
129                                 pDataArray->outputNames.pop_back();
130                         }else {
131                                 //fill data
132                                 for (int j = 0; j < pDataArray->thisLookUp[0]->getNumBins(); j++) {
133                                         data2[j].resize(subset.size(), 0.0);
134                                         for (int i = 0; i < subset.size(); i++) {
135                                                 data2[j][i] = (subset[i]->getAbundance(j));
136                                         }
137                                 }
138                                 
139                                 pDataArray->m->mothurOut("Comparing " + setA + " and " + setB + "..."); pDataArray->m->mothurOutEndLine(); 
140                                 
141                                 pDataArray->m->mothurOutEndLine();
142                                 MothurMetastats mothurMeta(pDataArray->threshold, pDataArray->iters);
143                                 mothurMeta.runMetastats(outputFileName, data2, setACount);
144                                 pDataArray->m->mothurOutEndLine();
145                                 pDataArray->m->mothurOutEndLine(); 
146                         }
147         }
148                 
149                 return 0;
150                 
151         }
152         catch(exception& e) {
153                 pDataArray->m->errorOut(e, "MetaStatsCommand", "MyMetastatsThreadFunction");
154                 exit(1);
155         }
156
157 #endif
158
159
160
161 #endif
162