]> git.donarmstrong.com Git - mothur.git/blob - coverage.cpp
6b514fbc2e47a40c8312ecfa206bc4d9e04a1e50
[mothur.git] / coverage.cpp
1 /*
2  *  coverage.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 /* This class library coverage at the given distances of the Cramer-von Mises statistic.
11         you may refer to the "Integration of Microbial Ecology and Statistics: A Test To Compare Gene Libraries" 
12         paper in Applied and Environmental Microbiology, Sept. 2004, p. 5485-5492 0099-2240/04/$8.00+0  
13         DOI: 10.1128/AEM.70.9.5485-5492.2004 Copyright 2004 American Society for Microbiology for more information. */
14         
15         
16 #include "coverage.h"
17
18 //**********************************************************************************************************************
19 Coverage::Coverage() {
20                 globaldata = GlobalData::getInstance();
21                 numUserGroups = globaldata->Groups.size();
22                 numGroups = globaldata->gGroupmap->getNumGroups();
23 }
24
25 //**********************************************************************************************************************
26 void Coverage::getValues(FullMatrix* matrix, vector< vector< vector<float> > >& data, vector<float> dist, string mode) {
27         try {
28                 vector<float> min;
29                 vector<string> groups;
30                 
31                 //initialize data
32                 data.resize(dist.size());
33                 for (int l = 0; l < data.size(); l++) {
34                         data[l].resize(numGroups);
35                         for (int k = 0; k < data[l].size(); k++) {
36                                 data[l][k].push_back(0.0);
37                         }
38                 }
39
40                 /**************************************/
41                 //get the minimums for each comparision
42                 /**************************************/
43                 int count = 0;
44                 int a = 0;
45                 int b = 0;
46                 for (int i = 0; i < numGroups; i++) {
47                         for (int j = 0; j < numGroups; j++) {
48                         
49                                 //is this "box" one hte user wants analyzed?
50                                 if ((inUsersGroups(globaldata->gGroupmap->namesOfGroups[i], globaldata->Groups) == true) && (inUsersGroups(globaldata->gGroupmap->namesOfGroups[j], globaldata->Groups) == true)) {
51                                         
52                                         if (mode == "random") {
53                                                 //create random matrix for this comparison
54                                                 matrix->shuffle(globaldata->gGroupmap->namesOfGroups[i], globaldata->gGroupmap->namesOfGroups[j]);
55                                         }
56                         
57                                         min = matrix->getMins(count);  //returns vector of mins for "box" requested ie. groups A, B, 0 = AA, 1 = AB, 2 = BA, 3 = BB;
58
59                                         //find the coverage at this distance
60                                         sort(min.begin(), min.end());
61                                         
62                                         //loop through each distance and fill data
63                                         for (int k = 0; k < data.size(); k++) {
64                                         
65                                                 int index = -1;
66                                                 //find index in min where value is higher than d
67                                                 for (int m = 0; m < min.size(); m++) {
68                                                         if (min[m] > dist[k])   { index = m; break;     }
69                                                 }
70                                         
71                                                 // if you don't find one than all the mins are less than d
72                                                 if (index == -1) { index = min.size();  }
73                                         
74                                                 //save value in data
75                                                 data[k][a][b] = 1.0 - ((min.size()-index)/(float)min.size());
76         
77                                         }
78                                         
79                                         //move to next box
80                                         if (b < numUserGroups-1) {  b++;  }
81                                         else{ //you are moving to a new row of "boxes"
82                                                 b = 0;
83                                                 a++;
84                                         }
85
86                                         count++;
87                                         
88                                         if (mode == "random") {
89                                                 //restore matrix to original form for next shuffle
90                                                 matrix->restore();
91                                         }
92                                 }
93                         }
94                 }
95                 
96         }
97         catch(exception& e) {
98                 cout << "Standard Error: " << e.what() << " has occurred in the Coverage class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }
101         catch(...) {
102                 cout << "An unknown error has occurred in the Coverage class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103                 exit(1);
104         }       
105 }
106 //**********************************************************************************************************************
107
108