]> git.donarmstrong.com Git - mothur.git/blob - venn.cpp
started venn command
[mothur.git] / venn.cpp
1 /*
2  *  venn.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "venn.h"
11
12 //**********************************************************************************************************************
13 Venn::Venn(){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 format = globaldata->getFormat();
17                 
18         }
19         catch(exception& e) {
20                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function Venn. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
21                 exit(1);
22         }
23         catch(...) {
24                 cout << "An unknown error has occurred in the Venn class function Venn. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
25                 exit(1);
26         }
27 }
28 //**********************************************************************************************************************
29 void Venn::getPic(OrderVector* order) {
30         try {
31                 
32                 rabund = order->getRAbundVector();
33                 
34                 string filenamesvg = globaldata->inputFileName + ".venn." + order->getLabel() + ".svg";
35                 
36                 openOutputFile(filenamesvg, outsvg);
37                 
38                         
39                 //svg image
40                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 200 200\" >\n";
41                 outsvg << "<g>\n";
42                                 
43                 outsvg << "<circle fill=\"red\" stroke=\"black\" cx=\"150\" cy=\"150\" r=\"100\"/>"; 
44                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"140\" y=\"150\">" + toString(rabund.getNumBins()) + "</text>\n";  
45                 outsvg << "</g>\n</svg>\n";
46                 
47                 outsvg.close();
48         }
49         catch(exception& e) {
50                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }
53         catch(...) {
54                 cout << "An unknown error has occurred in the Venn class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59 void Venn::getPic(SharedOrderVector* sharedorder) {
60         try {
61                 
62                 //fills vector of sharedsabunds - lookup
63                 getSharedVectors(sharedorder);
64                                 
65                 string filenamesvg = globaldata->inputFileName + ".venn." + sharedorder->getLabel() + "." + groupComb + ".svg";
66                 openOutputFile(filenamesvg, outsvg);
67                 
68                 //image window
69                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 700 700\" >\n";
70                 outsvg << "<g>\n";
71                                 
72                 if (lookup.size() == 1) {
73                         outsvg << "<circle fill=\"red\" opacity=\".5\" stroke=\"black\" cx=\"150\" cy=\"150\" r=\"100\"/>"; 
74                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"140\" y=\"150\">" + toString(lookup[0]->getNumBins()) + "</text>\n";  
75                         outsvg << "</g>\n</svg>\n";
76                         
77                 }else if (lookup.size() == 2) {
78                         //calc the shared otu
79                         int shared = 0;
80                         int numA = 0;
81                         int numB = 0;
82                         
83                         float rScaler;
84                         
85                         //for each bin
86                         for (int i = 0; i < lookup[0]->size(); i++) {
87                                 if (lookup[0]->getAbundance(i) != 0) { numA++; }
88                                 if (lookup[1]->getAbundance(i) != 0) { numB++; }
89                                 //are they shared
90                                 if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { shared++; }
91                         }
92                         
93                         if (numA > numB) { rScaler = 150 / float(numA); }
94                         else { rScaler = 150 / float(numB); }
95                         
96                         //to determine how far over to overlap b onto a.
97                         float percentOverlap = shared / (float) numA;   
98                         
99                         int bx = 200 + (numA * rScaler) - ((2 * (numA * rScaler)) * percentOverlap) + (numB * rScaler);
100                         int leftedgeB = bx - (numB * rScaler);  //center b - b's radius
101                         int leftedgeA = 200 - (numA * rScaler); //center a - a's radius
102                         int rightedgeB = bx + (numB * rScaler);  //center b + b's radius
103                         int rightedgeA = 200 + (numA * rScaler); //center a + a's radius
104
105                         int mida = leftedgeA + ((leftedgeB - leftedgeA) / 2);
106                         int midb = rightedgeA + ((rightedgeB - rightedgeA) / 2);
107                         int midab = leftedgeB + ((rightedgeA - leftedgeB) / 2);
108                         
109                         //draw circles
110                         outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
111                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(leftedgeA + ((rightedgeB - leftedgeA) / 2) - 70) + "\" y=\"50\">Venn Diagram at distance " + sharedorder->getLabel() + "</text>\n"; 
112                         outsvg << "<circle fill=\"rgb(255,0,0)\" opacity=\".5\" stroke=\"black\" cx=\"200\" cy=\"250\" r=\"" + toString(numA * rScaler) + "\"/>"; 
113                         outsvg << "<circle fill=\"rgb(0,255,0)\" opacity=\".5\" stroke=\"black\" cx=\"" + toString(bx)  + "\" cy=\"250\" r=\"" + toString(numB * rScaler) + "\"/>";
114                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(mida) + "\" y=\"250\">" + toString(numA-shared) + "</text>\n";  
115                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(midab) + "\" y=\"250\">" + toString(shared) + "</text>\n";  
116                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(midb) + "\" y=\"250\">" + toString(numB-shared) + "</text>\n";  
117                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(mida) + "\" y=\"500\">Percentage of species that are shared is " + toString((shared / (float)(numA + numB - shared))) + "</text>\n"; 
118                         
119                 }else if (lookup.size() == 3) {
120                         //calc the shared otu
121                         int sharedABC = 0;
122                         int numA = 0; int numB = 0; int numC = 0;
123                         int sharedAB = 0; int sharedAC = 0; int sharedBC = 0;
124                         
125                         //float scalerB;
126                         
127                         //for each bin
128                         for (int i = 0; i < lookup[0]->size(); i++) {
129                                 if (lookup[0]->getAbundance(i) != 0) { numA++; }
130                                 if (lookup[1]->getAbundance(i) != 0) { numB++; }
131                                 if (lookup[2]->getAbundance(i) != 0) { numC++; }
132                                 //are they shared by 2
133                                 if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { sharedAB++; }
134                                 if ((lookup[0]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0)) { sharedAC++; }
135                                 if ((lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { sharedBC++; }
136                                 
137                                 //are they shared by all
138                                 if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0)) { sharedABC++; }
139                         }
140                                                 
141                         //draw circles
142                         outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
143                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + sharedorder->getLabel() + "</text>\n";
144                         outsvg << "<circle fill=\"rgb(255,0,0)\" opacity=\".3\" stroke=\"black\" cx=\"230\" cy=\"200\" r=\"150\"/>"; 
145                         outsvg << "<circle fill=\"rgb(0,255,0)\" opacity=\".3\" stroke=\"black\" cx=\"455\" cy=\"200\" r=\"150\"/>"; 
146                         outsvg << "<circle fill=\"rgb(0,0,255)\" opacity=\".3\" stroke=\"black\" cx=\"343\" cy=\"400\" r=\"150\"/>"; 
147                         
148                         //place labels within overlaps
149                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(200 - ((int)toString(numA-sharedAB-sharedAC+sharedABC).length() / 2)) + "\" y=\"170\">" + toString(numA-sharedAB-sharedAC+sharedABC) + "</text>\n";  
150                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedAB).length() / 2)) + "\"  y=\"170\">" + toString(sharedAB) + "</text>\n";  
151                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(485 - ((int)toString(numB-sharedAB-sharedBC+sharedABC).length() / 2)) + "\"  y=\"170\">" + toString(numB-sharedAB-sharedBC+sharedABC) + "</text>\n";  
152                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(268 - ((int)toString(sharedAC).length() / 2)) + "\"  y=\"305\">" + toString(sharedAC) + "</text>\n";  
153                         outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(343 - ((int)toString(numC-sharedAC-sharedBC+sharedABC).length() / 2)) + "\"   y=\"430\">" + toString(numC-sharedAC-sharedBC+sharedABC) + "</text>\n";  
154                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(408 - ((int)toString(sharedBC).length() / 2)) + "\" y=\"305\">" + toString(sharedBC) + "</text>\n";  
155                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedABC).length() / 2)) + "\"  y=\"280\">" + toString(sharedABC) + "</text>\n"; 
156                         
157                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"580\">Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString((sharedAB / (float)(numA + numB - sharedAB))) + "</text>\n";
158                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"610\">Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[2] + " is " + toString((sharedAC / (float)(numA + numC - sharedAC))) + "</text>\n";
159                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"640\">Percentage of species that are shared in groups " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString((sharedBC / (float)(numB + numC - sharedBC))) + "</text>\n";
160                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"670\">Percentage of species that are shared in groups " + globaldata->Groups[0] + ", " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString((sharedABC / (float)(numA + numB + numC - sharedAB - sharedAC - sharedBC - (2 * sharedABC)))) + "</text>\n";
161                 }
162                 
163                 outsvg << "</g>\n</svg>\n";
164                 outsvg.close();
165
166                 
167                 
168         }
169         catch(exception& e) {
170                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171                 exit(1);
172         }
173         catch(...) {
174                 cout << "An unknown error has occurred in the Venn class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175                 exit(1);
176         }
177 }
178 //**********************************************************************************************************************
179 void Venn::getSharedVectors(SharedOrderVector* order){
180         try {
181         
182                 //delete lookup
183                 for (int j = 0; j < lookup.size(); j++) {
184                         delete lookup[j];
185                 }
186
187                 lookup.clear();
188                 
189                 groupComb = "";
190                 
191                 //create and initialize vector of sharedvectors, one for each group
192                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
193                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
194                         temp->setLabel(order->getLabel());
195                         temp->setGroup(globaldata->Groups[i]);
196                         groupComb += globaldata->Groups[i];
197                         lookup.push_back(temp);
198                 }
199                 
200                 int numSeqs = order->size();
201                 //sample all the members
202                 for(int i=0;i<numSeqs;i++){
203                         //get first sample
204                         individual chosen = order->get(i);
205                         int abundance; 
206                                         
207                         //set info for sharedvector in chosens group
208                         for (int j = 0; j < lookup.size(); j++) { 
209                                 if (chosen.group == lookup[j]->getGroup()) {
210                                          abundance = lookup[j]->getAbundance(chosen.bin);
211                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
212                                          break;
213                                 }
214                         }
215                 }
216                 
217         }
218         catch(exception& e) {
219                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220                 exit(1);
221         }
222         catch(...) {
223                 cout << "An unknown error has occurred in the Venn class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
224                 exit(1);
225         }
226
227 }
228 //**********************************************************************************************************************
229
230
231