]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.h
paralellized unifrac.weighted for windows. added get.metacommunity command. fixed...
[mothur.git] / unifracweightedcommand.h
1 #ifndef UNIFRACWEIGHTEDCOMMAND_H
2 #define UNIFRACWEIGHTEDCOMMAND_H
3
4 /*
5  *  unifracweightedcommand.h
6  *  Mothur
7  *
8  *  Created by Sarah Westcott on 2/9/09.
9  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10  *
11  */
12
13 #include "command.hpp"
14 #include "weighted.h"
15 #include "counttable.h"
16 #include "progress.hpp"
17 #include "sharedutilities.h"
18 #include "fileoutput.h"
19 #include "readtree.h"
20
21 class UnifracWeightedCommand : public Command {
22         
23         public:
24                 UnifracWeightedCommand(string);
25                 UnifracWeightedCommand();
26                 ~UnifracWeightedCommand() {}
27         
28                 vector<string> setParameters();
29                 string getCommandName()                 { return "unifrac.weighted";            }
30                 string getCommandCategory()             { return "Hypothesis Testing";          }
31                 
32         string getHelpString(); 
33     string getOutputPattern(string);    
34                 string getCitation() { return "Lozupone CA, Hamady M, Kelley ST, Knight R (2007). Quantitative and qualitative beta diversity measures lead to different insights into factors that structure microbial communities. Appl Environ Microbiol 73: 1576-85. \nhttp://www.mothur.org/wiki/Unifrac.weighted"; }
35                 string getDescription()         { return "generic tests that describes whether two or more communities have the same structure"; }
36
37                 int execute();
38                 void help() { m->mothurOut(getHelpString()); }
39         
40         private:
41                 struct linePair {
42                         int start;
43                         int num;
44                         linePair(int i, int j) : start(i), num(j) {}
45                 };
46                 vector<linePair> lines;
47         CountTable* ct;
48                 FileOutput* output;
49                 vector<Tree*> T;           //user trees
50                 vector<double> utreeScores;  //user tree unweighted scores
51                 vector<double> WScoreSig;  //tree weighted score signifigance when compared to random trees - percentage of random trees with that score or lower.
52                 vector<string> groupComb; // AB. AC, BC...
53                 string sumFile, outputDir;
54                 int iters, numGroups, numComp, counter;
55                 vector< vector<double> > rScores;  //vector<weighted scores for random trees.> each group comb has an entry
56                 vector< vector<double> > uScores;  //vector<weighted scores for user trees.> each group comb has an entry
57                 vector< map<float, float> > rScoreFreq;  //map <weighted score, number of random trees with that score.> -vector entry for each combination.
58                 vector< map<float, float> > rCumul;  //map <weighted score, cumulative percentage of number of random trees with that score or higher.> -vector entry for each c                                                                
59                 map<float, float>  validScores;  //map contains scores from random
60                 
61                 bool abort, phylip, random, includeRoot, subsample, consensus;
62                 string groups, itersString, outputForm, treefile, groupfile, namefile, countfile;
63                 vector<string> Groups, outputNames; //holds groups to be used
64                 int processors, subsampleSize, subsampleIters;
65                 ofstream outSum;
66                 map<string, string> nameMap;
67                 
68                 void printWSummaryFile();
69                 void printWeightedFile();  
70                 void createPhylipFile();
71                 //void removeValidScoresDuplicates();
72                 int findIndex(float, int);
73                 void calculateFreqsCumuls();
74                 int createProcesses(Tree*,  vector< vector<string> >,  vector< vector<double> >&);
75                 int driver(Tree*, vector< vector<string> >, int, int,  vector< vector<double> >&);
76         int runRandomCalcs(Tree*, vector<double>);
77         vector<Tree*> buildTrees(vector< vector<double> >&, int, CountTable&);
78         int getConsensusTrees(vector< vector<double> >&, int);
79         int getAverageSTDMatrices(vector< vector<double> >&, int);
80                 
81 };
82
83 /***********************************************************************/
84 struct weightedRandomData {
85     int start;
86         int num;
87         MothurOut* m;
88     vector< vector<double> > scores;
89     vector< vector<string> > namesOfGroupCombos;
90     Tree* t;
91     CountTable* ct;
92     bool includeRoot;
93         
94         weightedRandomData(){}
95         weightedRandomData(MothurOut* mout, int st, int en, vector< vector<string> > ngc, Tree* tree, CountTable* count, bool ir, vector< vector<double> > sc) {
96         m = mout;
97                 start = st;
98                 num = en;
99         namesOfGroupCombos = ngc;
100         t = tree;
101         ct = count;
102         includeRoot = ir;
103         scores = sc;
104         }
105 };
106
107 /**************************************************************************************************/
108 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
109 #else
110 static DWORD WINAPI MyWeightedRandomThreadFunction(LPVOID lpParam){
111         weightedRandomData* pDataArray;
112         pDataArray = (weightedRandomData*)lpParam;
113         try {
114         
115         Tree* randT = new Tree(pDataArray->ct);
116         
117         Weighted weighted(pDataArray->includeRoot);
118         
119                 for (int h = pDataArray->start; h < (pDataArray->start+pDataArray->num); h++) {
120             
121                         if (pDataArray->m->control_pressed) { return 0; }
122             
123                         //initialize weighted score
124                         string groupA = pDataArray->namesOfGroupCombos[h][0];
125                         string groupB = pDataArray->namesOfGroupCombos[h][1];
126                         
127                         //copy T[i]'s info.
128                         randT->getCopy(pDataArray->t);
129             
130                         //create a random tree with same topology as T[i], but different labels
131                         randT->assembleRandomUnifracTree(groupA, groupB);
132                         
133                         if (pDataArray->m->control_pressed) { delete randT;  return 0;  }
134             
135                         //get wscore of random tree
136                         EstOutput randomData = weighted.getValues(randT, groupA, groupB);
137             
138                         if (pDataArray->m->control_pressed) { delete randT;  return 0;  }
139             
140                         //save scores
141                         pDataArray->scores[h].push_back(randomData[0]);
142                 }
143         
144                 delete randT;
145         
146         return 0;
147     }
148         catch(exception& e) {
149                 pDataArray->m->errorOut(e, "Weighted", "MyWeightedRandomThreadFunction");
150                 exit(1);
151         }
152 }
153 #endif
154
155
156 #endif