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