]> git.donarmstrong.com Git - mothur.git/blob - coverage.cpp
ba2167a10fbdfd321395446fb1273e67402ca48c
[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 #include "coverage.h"
11
12 //**********************************************************************************************************************
13 Coverage::Coverage() {
14                 globaldata = GlobalData::getInstance();
15                 numUserGroups = globaldata->Groups.size();
16                 numGroups = globaldata->gGroupmap->getNumGroups();
17 }
18 //**********************************************************************************************************************
19 void Coverage::getValues(FullMatrix* matrix, float d, vector< vector<float> >& data) {
20         try {
21                 vector<float> min;
22                 vector<string> groups;
23                 
24                 //initialize data
25                 data.resize(numUserGroups);
26                 for (int l = 0; l < data.size(); l++) {
27                         data[l].push_back(0.0);
28                 }
29
30                 /**************************************/
31                 //get the minimums for each comparision
32                 /**************************************/
33                 int count = 0;
34                 int a = 0;
35                 int b = 0;
36                 for (int i = 0; i < numGroups; i++) {
37                         for (int j = 0; j < numGroups; j++) {
38                                 
39                                 //is this "box" one hte user wants analyzed?
40                                 if ((inUsersGroups(globaldata->gGroupmap->namesOfGroups[i], globaldata->Groups) == true) && (inUsersGroups(globaldata->gGroupmap->namesOfGroups[j], globaldata->Groups) == true)) {
41                                         
42                                         min = matrix->getMins(count); //returns vector of mins for "box" requested ie. groups A, B, 0 = AA, 1 = AB, 2 = BA, 3 = BB;
43                                         
44                                         //find the coverage at this distance
45                                         sort(min.begin(), min.end());
46 //cout << "minvector for : " << globaldata->gGroupmap->namesOfGroups[i] + globaldata->gGroupmap->namesOfGroups[j] << endl;
47 //for(int h = 0; h<min.size(); h++) {
48 // cout << min[h] << " ";
49 //}
50 //cout << endl;
51                                         int index = -1;
52                                         //find index in min where value is higher than d
53                                         for (int m = 0; m < min.size(); m++) {
54                                                 if (min[m] > d) { index = m; break;     }
55                                         }
56                                         
57                                         // if you don't find one than all the mins are less than d
58                                         if (index == -1) { index = min.size();  }
59                                         
60                                         //save value in data
61                                         data[a][b] = 1.0 - ((min.size()-index)/(float)min.size());
62 //cout << "D = " << d << "data " << a << b << " = " << data[a][b] << endl;              
63                                         if (b < numUserGroups-1) {  b++;  }
64                                         else{ //you are moving to a new row of "boxes"
65                                                 b = 0;
66                                                 a++;
67                                         }
68                                 }
69                                 count++;
70                         }
71                 }
72         }
73         catch(exception& e) {
74                 cout << "Standard Error: " << e.what() << " has occurred in the Coverage class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75                 exit(1);
76         }
77         catch(...) {
78                 cout << "An unknown error has occurred in the Coverage class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79                 exit(1);
80         }       
81 }
82 //**********************************************************************************************************************
83 //For the random matrices
84 void Coverage::getValues(FullMatrix* matrix, float d, vector< vector<float> >& data, string r) {
85         try {
86                 vector<float> min;
87                 vector<string> groups;
88                 
89                 //initialize data
90                 data.resize(numUserGroups);
91                 for (int l = 0; l < data.size(); l++) {
92                         data[l].push_back(0.0);
93                 }
94
95                 /**************************************/
96                 //get the minimums for each comparision
97                 /**************************************/
98                 int count = 0;
99                 int a = 0;
100                 int b = 0;
101                 for (int i = 0; i < numGroups; i++) {
102                         for (int j = 0; j < numGroups; j++) {
103                         
104                                 //is this "box" one hte user wants analyzed?
105                                 if ((inUsersGroups(globaldata->gGroupmap->namesOfGroups[i], globaldata->Groups) == true) && (inUsersGroups(globaldata->gGroupmap->namesOfGroups[j], globaldata->Groups) == true)) {
106 cout << "combo " << a << b << endl;
107 cout << "original matrix mins4rows " << endl;
108 matrix->printMinsForRows(cout);                                 
109                                         //create random matrix for this comparison
110                                         matrix->shuffle(count);
111                         
112                                         min = matrix->getMins(count);  //returns vector of mins for "box" requested ie. groups A, B, 0 = AA, 1 = AB, 2 = BA, 3 = BB;
113 cout << "shuffled matrix mins4rows " << endl;
114 matrix->printMinsForRows(cout);
115
116                                         //find the coverage at this distance
117                                         sort(min.begin(), min.end());
118                                         
119                                         int index = -1;
120                                         //find index in min where value is higher than d
121                                         for (int m = 0; m < min.size(); m++) {
122                                                 if (min[m] > d) { index = m; break;     }
123                                         }
124                                         
125                                         // if you don't find one than all the mins are less than d
126                                         if (index == -1) { index = min.size();  }
127                                         
128                                         //save value in data
129                                         data[a][b] = 1.0 - ((min.size()-index)/(float)min.size());
130 cout << "D = " << d << "data " << a << b << " = " << data[a][b] << endl;                
131                                         if (b < numUserGroups-1) {  b++;  }
132                                         else{ //you are moving to a new row of "boxes"
133                                                 b = 0;
134                                                 a++;
135                                         }
136                                 }
137                                 count++;
138                                 
139                                 //restore matrix to original form for next shuffle
140                                 matrix->restore();
141 min = matrix->getMins(count-1); 
142 cout << "restored matrix mins4rows " << endl;
143 matrix->printMinsForRows(cout);
144                         }
145                 }
146         }
147         catch(exception& e) {
148                 cout << "Standard Error: " << e.what() << " has occurred in the Coverage class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149                 exit(1);
150         }
151         catch(...) {
152                 cout << "An unknown error has occurred in the Coverage class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153                 exit(1);
154         }       
155 }
156 //**********************************************************************************************************************
157
158