]> git.donarmstrong.com Git - mothur.git/blob - sparcccommand.h
added modify names parameter to set.dir
[mothur.git] / sparcccommand.h
1 //
2 //  sparcccommand.h
3 //  Mothur
4 //
5 //  Created by SarahsWork on 5/10/13.
6 //  Copyright (c) 2013 Schloss Lab. All rights reserved.
7 //
8
9 #ifndef Mothur_sparcccommand_h
10 #define Mothur_sparcccommand_h
11
12 #include "command.hpp"
13 #include "inputdata.h"
14 #include "calcsparcc.h"
15
16 /**************************************************************************************************/
17
18 class SparccCommand : public Command {
19 public:
20     SparccCommand(string);
21     SparccCommand();
22     ~SparccCommand(){}
23     
24     vector<string> setParameters();
25     string getCommandName()                     { return "sparcc";                      }
26     string getCommandCategory()         { return "OTU-Based Approaches";                }
27     
28     string getOutputPattern(string);
29     //commmand category choices: Sequence Processing, OTU-Based Approaches, Hypothesis Testing, Phylotype Analysis, General, Clustering and Hidden
30         string getHelpString();
31     string getCitation() { return "Friedman J, Alm EJ (2012) Inferring Correlation Networks from Genomic Survey Data. PLoS Comput Biol 8(9): e1002687. doi:10.1371/journal.pcbi.1002687 http://www.mothur.org/wiki/Sparcc"; }
32     string getDescription()             { return "brief description"; }
33     
34     int execute();
35     void help() { m->mothurOut(getHelpString()); }
36     
37 private:
38     bool abort, allLines;
39     string outputDir, sharedfile, normalizeMethod;
40     int numSamplings, maxIterations, numPermutations, processors;
41     set<string> labels;
42     vector<string> Groups;
43     vector<string> outputNames;
44     
45     int process(vector<SharedRAbundVector*>&);
46     vector<vector<float> > createProcesses(vector<vector<float> >&, vector<vector<float> >&);
47     vector<vector<float> > driver(vector<vector<float> >&, vector<vector<float> >&, int);
48     vector<vector<float> > shuffleSharedVector(vector<vector<float> >&);
49 };
50
51 /**************************************************************************************************/
52
53 struct sparccData {
54         MothurOut* m;
55     int numPerms;
56     vector< vector<float> > sharedVector;
57     vector< vector<float> > origCorrMatrix;
58     vector<vector<float> > pValues;
59     int numSamplings, maxIterations, numPermutations;
60     string normalizeMethod;
61         
62         sparccData(){}
63         sparccData(MothurOut* mout, int it, vector< vector<float> > cs, vector< vector<float> > co, int ns, int mi, int np, string nm) {
64                 m = mout;
65         numPerms = it;
66         sharedVector = cs;
67         origCorrMatrix = co;
68         numSamplings = ns;
69         maxIterations = mi;
70         numPermutations = np;
71         normalizeMethod = nm;
72     }
73 };
74 /**************************************************************************************************/
75 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
76 #else
77 static DWORD WINAPI MySparccThreadFunction(LPVOID lpParam){
78         sparccData* pDataArray;
79         pDataArray = (sparccData*)lpParam;
80         
81         try {
82         
83         int numOTUs = pDataArray->sharedVector[0].size();
84         vector<vector<float> > sharedShuffled = pDataArray->sharedVector;
85         pDataArray->pValues.resize(numOTUs);
86         for(int i=0;i<numOTUs;i++){ pDataArray->pValues[i].assign(numOTUs, 0);  }
87         
88         for(int i=0;i<pDataArray->numPerms;i++){
89             if (pDataArray->m->control_pressed) { return 0; }
90             
91             //sharedShuffled = shuffleSharedVector(sharedVector);
92             //////////////////////////////////////////////////////////
93             int numGroups = (int)pDataArray->sharedVector.size();
94             sharedShuffled = pDataArray->sharedVector;
95             
96             for(int k=0;k<numGroups;k++){
97                 for(int j=0;j<numOTUs;j++){
98                     sharedShuffled[k][j] = pDataArray->sharedVector[rand()%numGroups][j];
99                 }
100             }
101             /////////////////////////////////////////////////////////
102             
103             CalcSparcc permutedData(sharedShuffled, pDataArray->maxIterations, pDataArray->numSamplings, pDataArray->normalizeMethod);
104             vector<vector<float> > permuteCorrMatrix = permutedData.getRho();
105             
106             for(int j=0;j<numOTUs;j++){
107                 for(int k=0;k<j;k++){
108                     double randValue = permuteCorrMatrix[j][k];
109                     double observedValue = pDataArray->origCorrMatrix[j][k];
110                     if(observedValue >= 0 &&  randValue > observedValue)   { pDataArray->pValues[j][k]++; }//this method seems to deflate the
111                     else if(observedValue < 0 && randValue < observedValue){ pDataArray->pValues[j][k]++; }//pvalues of small rho values
112                 }
113             }
114             if((i+1) % (int)(pDataArray->numPermutations * 0.05) == 0){ cout << i+1 << endl;  }
115         }
116         
117         return 0;
118                 
119         }
120         catch(exception& e) {
121                 pDataArray->m->errorOut(e, "SparccCommand", "MySparccThreadFunction");
122                 exit(1);
123         }
124 }
125 #endif
126
127
128 /**************************************************************************************************/
129
130
131
132
133 #endif