]> git.donarmstrong.com Git - mothur.git/blob - venn.cpp
fixed bug in 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 #include "ace.h"
12 #include "sobs.h"
13 #include "chao1.h"
14 #include "sharedchao1.h"
15 #include "sharedsobscollectsummary.h"
16
17
18 //**********************************************************************************************************************
19 Venn::Venn(){
20         try {
21                 globaldata = GlobalData::getInstance();
22                 format = globaldata->getFormat();
23         }
24         catch(exception& e) {
25                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function Venn. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28         catch(...) {
29                 cout << "An unknown error has occurred in the Venn class function Venn. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 void Venn::getPic(SAbundVector* sabund, vector<Calculator*> vCalcs) {
35         try {
36                                 
37                 for(int i=0;i<vCalcs.size();i++){
38                         string filenamesvg = globaldata->inputFileName + ".venn." + sabund->getLabel() + vCalcs[i]->getName() + ".svg";
39                         openOutputFile(filenamesvg, outsvg);
40
41                         vector<double> data = vCalcs[i]->getValues(sabund);
42                         
43                         //svg image
44                         outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 700 700\" >\n";
45                         outsvg << "<g>\n";
46                                 
47                         outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
48                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + sabund->getLabel() + "</text>\n";
49                         outsvg << "<circle fill=\"red\" opacity=\".5\" stroke=\"black\" cx=\"350\" cy=\"200\" r=\"150\"/>"; 
50                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(data[0]).length() / 2)) + "\" y=\"195\">" + toString(data[0]) + "</text>\n";  
51                         
52                         if (data.size() == 3) { 
53                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"200\" y=\"380\">The lower bound of the confidence interval is " + toString(data[1]) + "</text>\n";
54                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"200\" y=\"410\">The upper bound of the confidence interval is " + toString(data[2]) + "</text>\n";
55                         }
56                         
57                         outsvg << "</g>\n</svg>\n";
58                         outsvg.close();
59                 }
60         }
61         catch(exception& e) {
62                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63                 exit(1);
64         }
65         catch(...) {
66                 cout << "An unknown error has occurred in the Venn class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67                 exit(1);
68         }
69 }
70 //**********************************************************************************************************************
71 void Venn::getPic(vector<SharedRAbundVector*> lookup, vector<Calculator*> vCalcs) {
72         try {
73                 
74                 vector<SharedRAbundVector*> subset;
75                 
76                 /******************* 1 Group **************************/
77                 if (lookup.size() == 1) {
78                                         
79                         SAbundVector s;
80                         s = lookup[0]->getSAbundVector();  SAbundVector* sabund = &s;
81                         
82                         //make a file for each calculator
83                         for(int i=0;i<vCalcs.size();i++){
84                                 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg";
85                                 openOutputFile(filenamesvg, outsvg);
86                         
87                                 //in essence you want to run it like a single 
88                                 if (vCalcs[i]->getName() == "sharedsobs") {
89                                         singleCalc = new Sobs();
90                                 }else if (vCalcs[i]->getName() == "sharedchao") {
91                                         singleCalc = new Chao1();
92                                 }else if (vCalcs[i]->getName() == "sharedace") {
93                                         singleCalc = new Ace(10);
94                                 }
95                                 
96                                 vector<double> data = singleCalc->getValues(sabund);
97                         
98                                 //svg image
99                                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 700 700\" >\n";
100                                 outsvg << "<g>\n";
101                                 
102                                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
103                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + lookup[0]->getLabel() + "</text>\n";
104                                 outsvg << "<circle fill=\"red\" opacity=\".5\" stroke=\"black\" cx=\"350\" cy=\"200\" r=\"150\"/>"; 
105                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)lookup[0]->getGroup().length() / 2)) + "\" y=\"165\">" + lookup[0]->getGroup() + "</text>\n";
106                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(data[0]).length() / 2)) + "\" y=\"195\">" + toString(data[0]) + "</text>\n";  
107                         
108                                 if (data.size() == 3) { 
109                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"200\" y=\"380\">The lower bound of the confidence interval is " + toString(data[1]) + "</text>\n";
110                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"200\" y=\"410\">The upper bound of the confidence interval is " + toString(data[2]) + "</text>\n";
111                                 }
112                         
113                                 outsvg << "</g>\n</svg>\n";
114                                 outsvg.close();
115                                 delete singleCalc;
116                                 
117                         }
118                         delete sabund;
119                 /******************* 2 Groups **************************/       
120                 
121                 }else if (lookup.size() == 2) {
122                         //get sabund vector pointers so you can use the single calculators
123                         //one for each group
124                         SAbundVector sA, sB;
125                         SAbundVector* sabundA; SAbundVector* sabundB;
126                         sabundA = new SAbundVector(lookup[0]->getSAbundVector());//  sabundA = &sA;
127                         sabundB = new SAbundVector(lookup[1]->getSAbundVector());//  sabundB = &sB;
128                         
129                         subset.clear();
130                         subset.push_back(lookup[0]); subset.push_back(lookup[1]);
131                         
132                         //make a file for each calculator
133                         for(int i=0;i<vCalcs.size();i++){
134                                 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg";
135                                 openOutputFile(filenamesvg, outsvg);
136                                 
137                                 //get estimates for sharedAB
138                                 vector<double> shared = vCalcs[i]->getValues(subset);
139                                 
140                                 //in essence you want to run it like a single 
141                                 if (vCalcs[i]->getName() == "sharedsobs") {
142                                         singleCalc = new Sobs();
143                                 }else if (vCalcs[i]->getName() == "sharedchao") {
144                                         singleCalc = new Chao1();
145                                 }else if (vCalcs[i]->getName() == "sharedace") {
146                                         singleCalc = new Ace(10);
147                                 }
148                                 
149                                 //get estimates for numA
150                                 vector<double> numA = singleCalc->getValues(sabundA);
151
152                                 //get estimates for numB
153                                 vector<double> numB = singleCalc->getValues(sabundB);
154                                                 
155                                 //image window
156                                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 700 700\" >\n";
157                                 outsvg << "<g>\n";
158
159                                 //draw circles
160                                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
161                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + lookup[0]->getLabel() + "</text>\n";
162                                 outsvg << "<circle fill=\"rgb(255,0,0)\" opacity=\".3\" stroke=\"black\" cx=\"250\" cy=\"200\" r=\"150\"/>"; 
163                                 outsvg << "<circle fill=\"rgb(0,255,0)\" opacity=\".3\" stroke=\"black\" cx=\"435\" cy=\"200\" r=\"150\"/>"; 
164                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(200 - ((int)toString(numA[0]).length() / 2)) + "\" y=\"195\">" + toString(numA[0] - shared[0]) + "</text>\n";
165                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(490 - ((int)toString(numB[0]).length() / 2)) + "\" y=\"195\">" + toString(numB[0] - shared[0]) + "</text>\n"; 
166                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(200 - ((int)lookup[0]->getGroup().length() / 2)) + "\" y=\"175\">" + lookup[0]->getGroup() + "</text>\n";
167                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(490 - ((int)lookup[1]->getGroup().length() / 2)) + "\" y=\"175\">" + lookup[1]->getGroup() + "</text>\n"; 
168                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(shared[0]).length() / 2)) + "\" y=\"195\">" + toString(shared[0]) + "</text>\n";  
169                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"460\">The number of species in group " + globaldata->Groups[0] + " is " + toString(numA[0]);
170                                 if (numA.size() == 3) { 
171                                         outsvg << " the lci is " + toString(numA[1]) + " and the hci is " + toString(numA[2]) + "</text>\n";
172                                 }else { outsvg << "</text>\n"; }
173                 
174                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"480\">The number of species in group " + globaldata->Groups[1] + " is " + toString(numB[0]);
175                                 if (numB.size() == 3) { 
176                                         outsvg << " the lci is " + toString(numB[1]) + " and the hci is " + toString(numB[2]) + "</text>\n";
177                                 }else { outsvg << "</text>\n"; }
178
179                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"500\">The number of sepecies shared between groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString(shared[0]) + "</text>\n";
180                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"520\">Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString((shared[0] / (float)(numA[0] + numB[0] - shared[0]))) + "</text>\n";
181                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"540\">The total richness for all groups is " + toString((float)(numA[0] + numB[0] - shared[0])) + "</text>\n";
182                                 
183                                 //close file
184                                 outsvg << "</g>\n</svg>\n";
185                                 outsvg.close();
186                                 delete singleCalc;
187                         }
188                         delete sabundA;
189                         delete sabundB;
190                 /******************* 3 Groups **************************/
191                                                 
192                 }else if (lookup.size() == 3) {
193                         //get sabund vector pointers so you can use the single calculators
194                         //one for each group
195                         SAbundVector sA, sB, sC;
196                         SAbundVector* sabundA; SAbundVector* sabundB; SAbundVector* sabundC;
197                         sA = lookup[0]->getSAbundVector();  sabundA = &sA;
198                         sB = lookup[1]->getSAbundVector();  sabundB = &sB;
199                         sC = lookup[2]->getSAbundVector();  sabundC = &sC;
200                         
201                         //make a file for each calculator
202                         for(int i=0;i<vCalcs.size();i++){
203                         
204                                 //in essence you want to run it like a single 
205                                 if (vCalcs[i]->getName() == "sharedsobs") {
206                                         singleCalc = new Sobs();
207                                 }else if (vCalcs[i]->getName() == "sharedchao") {
208                                         singleCalc = new Chao1();
209                                 }else if (vCalcs[i]->getName() == "sharedace") {
210                                         singleCalc = new Ace(10);
211                                 }
212                                 
213                                 //get estimates for numA
214                                 vector<double> numA = singleCalc->getValues(sabundA);
215                         
216                                 //get estimates for numB
217                                 vector<double> numB = singleCalc->getValues(sabundB);
218                                 
219                                 //get estimates for numC
220                                 vector<double> numC = singleCalc->getValues(sabundC);
221
222                                 
223                                 string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg";
224                                 openOutputFile(filenamesvg, outsvg);
225
226                                 //get estimates for sharedAB, sharedAC and sharedBC
227                                 subset.clear();
228                                 subset.push_back(lookup[0]); subset.push_back(lookup[1]);
229                                 vector<double> sharedAB = vCalcs[i]->getValues(subset);
230                                 
231                                 subset.clear();
232                                 subset.push_back(lookup[0]); subset.push_back(lookup[2]);
233                                 vector<double> sharedAC = vCalcs[i]->getValues(subset);
234                                 
235                                 subset.clear();
236                                 subset.push_back(lookup[1]); subset.push_back(lookup[2]);
237                                 vector<double> sharedBC = vCalcs[i]->getValues(subset);
238                                 
239                                 vector<double> sharedAwithBC;
240                                 vector<double> sharedBwithAC;
241                                 vector<double> sharedCwithAB;
242                                 
243                                 //find possible sharedABC values
244                                 float sharedABC1, sharedABC2, sharedABC3, sharedABC;
245
246                                 if (vCalcs[i]->getName() != "sharedchao") {
247                                         //merge BC and estimate with shared with A
248                                         SharedRAbundVector* merge = new SharedRAbundVector();
249                                         for (int j = 0; j < lookup[1]->size(); j++) {
250                                                 merge->push_back((lookup[1]->getAbundance(j) + lookup[2]->getAbundance(j)), j, "");
251                                         }
252                                 
253                                         subset.clear();
254                                         subset.push_back(lookup[0]); subset.push_back(merge);
255                                         sharedAwithBC = vCalcs[i]->getValues(subset);
256                         
257                                         delete merge;
258                                         //merge AC and estimate with shared with B
259                                         merge = new SharedRAbundVector();
260                                         for (int j = 0; j < lookup[0]->size(); j++) {
261                                                 merge->push_back((lookup[0]->getAbundance(j) + lookup[2]->getAbundance(j)), j, "");
262                                         }
263                                 
264                                         subset.clear();
265                                         subset.push_back(merge); subset.push_back(lookup[1]);
266                                         sharedBwithAC = vCalcs[i]->getValues(subset);
267                         
268                                         delete merge;
269                                         //merge AB and estimate with shared with C
270                                         merge = new SharedRAbundVector();
271                                         for (int j = 0; j < lookup[0]->size(); j++) {
272                                                 merge->push_back((lookup[0]->getAbundance(j) + lookup[1]->getAbundance(j)), j, "");
273                                         }
274                                 
275                                         subset.clear();
276                                         subset.push_back(lookup[2]); subset.push_back(merge);
277                                         sharedCwithAB = vCalcs[i]->getValues(subset);
278                                         delete merge;
279                                 
280                                         sharedABC1 = sharedAB[0] + sharedAC[0] - sharedAwithBC[0];
281                                         sharedABC2 = sharedAB[0] + sharedBC[0] - sharedBwithAC[0];
282                                         sharedABC3 = sharedAC[0] + sharedBC[0] - sharedCwithAB[0];
283  
284                                         //if any of the possible m's are - throw them out
285                                         if (sharedABC1 < 0.0) { sharedABC1 = 0; }
286                                         if (sharedABC2 < 0.0) { sharedABC2 = 0; }
287                                         if (sharedABC3 < 0.0) { sharedABC3 = 0; }
288                 
289                                         //sharedABC is the minimum of the 3 possibilities
290                                         if ((sharedABC1 < sharedABC2) && (sharedABC1 < sharedABC3)) { sharedABC = sharedABC1; }
291                                         else if ((sharedABC2 < sharedABC1) && (sharedABC2 < sharedABC3)) { sharedABC = sharedABC2; }
292                                         else if ((sharedABC3 < sharedABC1) && (sharedABC3 < sharedABC2)) { sharedABC = sharedABC3; }    
293                                 }else{
294                                         vector<double> data = vCalcs[i]->getValues(lookup);
295                                         sharedABC = data[0];
296                                         sharedAwithBC.push_back(sharedAB[0] + sharedAC[0] - sharedABC);
297                                         sharedBwithAC.push_back(sharedAB[0] + sharedBC[0] - sharedABC);
298                                         sharedCwithAB.push_back(sharedAC[0] + sharedBC[0] - sharedABC);
299                                 }
300                         
301                                 //image window
302                                 outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 800 800\" >\n";
303                                 outsvg << "<g>\n";
304
305                                 //draw circles
306                                 outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"800\" height=\"800\"/>"; 
307                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + lookup[0]->getLabel() + "</text>\n";
308                                 outsvg << "<circle fill=\"rgb(255,0,0)\" opacity=\".3\" stroke=\"black\" cx=\"230\" cy=\"200\" r=\"150\"/>"; 
309                                 outsvg << "<circle fill=\"rgb(0,255,0)\" opacity=\".3\" stroke=\"black\" cx=\"455\" cy=\"200\" r=\"150\"/>"; 
310                                 outsvg << "<circle fill=\"rgb(0,0,255)\" opacity=\".3\" stroke=\"black\" cx=\"343\" cy=\"400\" r=\"150\"/>"; 
311 //cout << "numA = " << numA[0] << " numB = " << numB[0]         << " numC = " << numC[0] << endl;
312 //cout << "sharedAB = " << sharedAB[0] << " sharedAC = " << sharedAC[0] << " sharedBC = " << sharedBC[0] << endl;
313 //cout << "sharedAwithBC = " << sharedAwithBC[0]        << " sharedBwithAC = " << sharedBwithAC[0] << " sharedCwithAB = " << sharedCwithAB[0] << endl;  
314                                 //place labels within overlaps
315                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(200 - ((int)toString(numA[0]-sharedAwithBC[0]).length() / 2)) + "\" y=\"170\">" + toString(numA[0]-sharedAwithBC[0]) + "</text>\n"; 
316                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(200 - ((int)lookup[0]->getGroup().length() / 2)) + "\" y=\"150\">" + lookup[0]->getGroup() + "</text>\n";  
317                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedAB[0] - sharedABC).length() / 2)) + "\"  y=\"170\">" + toString(sharedAB[0] - sharedABC) + "</text>\n";  
318                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(485 - ((int)toString(numB[0]-sharedBwithAC[0]).length() / 2)) + "\"  y=\"170\">" + toString(numB[0]-sharedBwithAC[0]) + "</text>\n";
319                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(485 - ((int)lookup[1]->getGroup().length() / 2)) + "\"  y=\"150\">" + lookup[1]->getGroup() + "</text>\n";  
320                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(268 - ((int)toString(sharedAC[0] - sharedABC).length() / 2)) + "\"  y=\"305\">" + toString(sharedAC[0] - sharedABC) + "</text>\n";  
321                                 outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(343 - ((int)toString(numC[0]-sharedCwithAB[0]).length() / 2)) + "\"   y=\"430\">" + toString(numC[0]-sharedCwithAB[0]) + "</text>\n"; 
322                                 outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(343 - ((int)lookup[2]->getGroup().length() / 2)) + "\"   y=\"410\">" + lookup[2]->getGroup() + "</text>\n"; 
323                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(408 - ((int)toString(sharedBC[0] - sharedABC).length() / 2)) + "\" y=\"305\">" + toString(sharedBC[0] - sharedABC) + "</text>\n";  
324                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedABC).length() / 2)) + "\"  y=\"280\">" + toString(sharedABC) + "</text>\n"; 
325                         
326                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"660\">The number of sepecies shared between groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString(sharedAB[0]) + "</text>\n";
327                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"680\">The number of sepecies shared between groups " + globaldata->Groups[0] + " and " + globaldata->Groups[2] + " is " + toString(sharedAC[0]) + "</text>\n";
328                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"700\">The number of sepecies shared between groups " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString(sharedBC[0]) + "</text>\n";
329                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"720\">The number of sepecies shared between groups " + globaldata->Groups[0] + " and combined groups " + globaldata->Groups[1] + globaldata->Groups[2] + " is " + toString(sharedAwithBC[0]) + "</text>\n";
330                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"740\">The number of sepecies shared between groups " + globaldata->Groups[1] + " and combined groups " + globaldata->Groups[0] + globaldata->Groups[2] + " is " + toString(sharedBwithAC[0]) + "</text>\n";
331                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"760\">The number of sepecies shared between groups " + globaldata->Groups[2] + " and combined groups " + globaldata->Groups[0] + globaldata->Groups[1] + " is " + toString(sharedCwithAB[0]) + "</text>\n";
332                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"580\">The number of species in group " + globaldata->Groups[0] + " is " + toString(numA[0]);
333                                 if (numA.size() == 3) { 
334                                         outsvg << " the lci is " + toString(numA[1]) + " and the hci is " + toString(numA[2]) + "</text>\n";
335                                 }else { outsvg << "</text>\n"; }
336                 
337                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"600\">The number of species in group " + globaldata->Groups[1] + " is " + toString(numB[0]);
338                                 if (numB.size() == 3) { 
339                                         outsvg << " the lci is " + toString(numB[1]) + " and the hci is " + toString(numB[2]) + "</text>\n";
340                                 }else { outsvg << "</text>\n"; }
341                                 
342                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"620\">The number of species in group " + globaldata->Groups[2] + " is " + toString(numC[0]);
343                                 if (numC.size() == 3) { 
344                                         outsvg << " the lci is " + toString(numC[1]) + " and the hci is " + toString(numC[2]) + "</text>\n";
345                                 }else { outsvg << "</text>\n"; }
346
347                                 outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"640\">The total richness of all the groups is " + toString(numA[0] + numB[0] + numC[0] - sharedAB[0] - sharedAC[0] - sharedBC[0] + sharedABC) + "</text>\n";
348                                 
349                                 //close file
350                                 outsvg << "</g>\n</svg>\n";
351                                 outsvg.close();
352                                 delete singleCalc;
353                         }
354                         delete sabundA;
355                         delete sabundB;
356                         delete sabundC;
357                 /******************* 4 Groups **************************/
358                 
359                 }else if (lookup.size() == 4) {
360                         //calc the shared otu
361                         float sharedABCD = 0;
362                         float numA = 0; float numB = 0; float numC = 0; float numD = 0;
363                         float sharedAB = 0; float sharedAC = 0; float sharedBC = 0; float sharedAD = 0; float sharedBD = 0; float sharedCD = 0;
364                         float sharedABC = 0; float sharedACD = 0; float sharedBCD = 0; float sharedABD = 0;
365                         vector<double> data;
366                         //get sabund vector pointers so you can use the single calculators
367                         //one for each group
368                         SAbundVector sA, sB, sC, sD;
369                         SAbundVector* sabundA; SAbundVector* sabundB; SAbundVector* sabundC; SAbundVector* sabundD;
370                         sA = lookup[0]->getSAbundVector();  sabundA = &sA;
371                         sB = lookup[1]->getSAbundVector();  sabundB = &sB;
372                         sC = lookup[2]->getSAbundVector();  sabundC = &sC;
373                         sD = lookup[3]->getSAbundVector();  sabundD = &sD;
374                         
375                         //A = red, B = green, C = blue, D = yellow
376                         
377                         //make a file for each calculator
378                         for(int i=0;i<vCalcs.size();i++){
379                                 
380                                 if ((vCalcs[i]->getName() != "sharedsobs") && (vCalcs[i]->getName() != "sharedchao")) { cout << vCalcs[i]->getName() << " is not a valid calculator with four groups.  It will be disregarded. " << endl; }
381                                 else{
382                                         string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg";
383                                         openOutputFile(filenamesvg, outsvg);
384
385                                 
386                                         //in essence you want to run it like a single 
387                                         if (vCalcs[i]->getName() == "sharedsobs") {
388                                                 singleCalc = new Sobs();
389                                         }else if (vCalcs[i]->getName() == "sharedchao") {
390                                                 singleCalc = new Chao1();
391                                         }
392                                 
393                                         //get estimates for numA
394                                         data = singleCalc->getValues(sabundA);
395                                         numA = data[0];
396                         
397                                         //get estimates for numB
398                                         data = singleCalc->getValues(sabundB);
399                                         numB = data[0];
400                                 
401                                         //get estimates for numC
402                                         data = singleCalc->getValues(sabundC);
403                                         numC = data[0];
404                                 
405                                         //get estimates for numD
406                                         data = singleCalc->getValues(sabundD);
407                                         numD = data[0];
408
409
410                                         //get estimates for pairs
411                                         subset.clear();
412                                         subset.push_back(lookup[0]); subset.push_back(lookup[1]);
413                                         data = vCalcs[i]->getValues(subset);
414                                         sharedAB = data[0];
415                                 
416                                         subset.clear();
417                                         subset.push_back(lookup[0]); subset.push_back(lookup[2]);
418                                         data = vCalcs[i]->getValues(subset);
419                                         sharedAC = data[0];
420                                 
421                                         subset.clear();
422                                         subset.push_back(lookup[0]); subset.push_back(lookup[3]);
423                                         data = vCalcs[i]->getValues(subset);
424                                         sharedAD = data[0];
425                                 
426                                         subset.clear();
427                                         subset.push_back(lookup[1]); subset.push_back(lookup[2]);
428                                         data = vCalcs[i]->getValues(subset);
429                                         sharedBC = data[0];
430                                 
431                                         subset.clear();
432                                         subset.push_back(lookup[1]); subset.push_back(lookup[3]);
433                                         data = vCalcs[i]->getValues(subset);
434                                         sharedBD = data[0];
435                                 
436                                         subset.clear();
437                                         subset.push_back(lookup[2]); subset.push_back(lookup[3]);
438                                         data = vCalcs[i]->getValues(subset);
439                                         sharedCD = data[0];
440                                 
441                                 
442                                         //get estimates for combos of 3
443                                         subset.clear();
444                                         subset.push_back(lookup[0]); subset.push_back(lookup[1]); subset.push_back(lookup[2]);
445                                         data = vCalcs[i]->getValues(subset);
446                                         sharedABC = data[0];
447                                 
448                                         subset.clear();
449                                         subset.push_back(lookup[0]); subset.push_back(lookup[2]); subset.push_back(lookup[3]);
450                                         data = vCalcs[i]->getValues(subset);
451                                         sharedACD = data[0];
452                                 
453                                         subset.clear();
454                                         subset.push_back(lookup[1]); subset.push_back(lookup[2]); subset.push_back(lookup[3]);
455                                         data = vCalcs[i]->getValues(subset);
456                                         sharedBCD = data[0];
457                                 
458                                         subset.clear();
459                                         subset.push_back(lookup[0]); subset.push_back(lookup[1]); subset.push_back(lookup[3]);
460                                         data = vCalcs[i]->getValues(subset);
461                                         sharedABD = data[0];
462
463                                         //get estimate for all four
464                                         data = vCalcs[i]->getValues(lookup);
465                                         sharedABCD = data[0];
466
467                 
468                                         //image window
469                                         outsvg << "<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 700 700\" >\n";
470                                         outsvg << "<g>\n";
471
472                                         //draw circles
473                                         outsvg << "<rect fill=\"white\" stroke=\"white\" x=\"0\" y=\"0\" width=\"700\" height=\"700\"/>"; 
474                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"265\" y=\"30\">Venn Diagram at distance " + lookup[0]->getLabel() + "</text>\n";
475                                         outsvg << "<ellipse fill=\"red\" stroke=\"black\" opacity=\".35\" transform=\"rotate(-45 355 215) \" cx=\"355\" cy=\"215\" rx=\"200\" ry=\"115\"/>\n "; 
476                                         outsvg << "<ellipse fill=\"green\" stroke=\"black\" opacity=\".35\" transform=\"rotate(+45 355 215) \" cx=\"355\" cy=\"215\" rx=\"200\" ry=\"115\"/>\n ";
477                                         outsvg << "<ellipse fill=\"blue\" stroke=\"black\" opacity=\".35\" transform=\"rotate(-40 440 315) \" cx=\"440\" cy=\"315\" rx=\"200\" ry=\"115\"/>\n ";
478                                         outsvg << "<ellipse fill=\"yellow\" stroke=\"black\" opacity=\".35\" transform=\"rotate(+40 270 315) \" cx=\"270\" cy=\"315\" rx=\"200\" ry=\"115\"/>\n ";
479                         
480                                         //A = red, B = green, C = blue, D = yellow
481                         
482                                         //place labels within overlaps
483                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(460 - ((int)toString(numA).length() / 2)) + "\" y=\"110\">" + toString(numA) + "</text>\n"; 
484                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(460 - ((int)lookup[0]->getGroup().length() / 2)) + "\" y=\"90\">" + lookup[0]->getGroup() + "</text>\n";  
485                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(350 - ((int)toString(sharedAB).length() / 2)) + "\"  y=\"160\">" + toString(sharedAB) + "</text>\n";  
486                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(250 - ((int)toString(numB).length() / 2)) + "\"  y=\"110\">" + toString(numB) + "</text>\n";  
487                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(250 - ((int)lookup[1]->getGroup().length() / 2)) + "\"  y=\"90\">" + lookup[1]->getGroup() + "</text>\n"; 
488                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(490 - ((int)toString(sharedAC).length() / 2)) + "\"  y=\"190\">" + toString(sharedAC) + "</text>\n";  
489                                         outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(550 - ((int)toString(numC).length() / 2)) + "\"   y=\"230\">" + toString(numC) + "</text>\n";  
490                                         outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(550 - ((int)lookup[2]->getGroup().length() / 2)) + "\"   y=\"210\">" + lookup[2]->getGroup() + "</text>\n";
491                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(215 - ((int)toString(sharedBC).length() / 2)) + "\" y=\"190\">" + toString(sharedBC) + "</text>\n";  
492                                         outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(150 - ((int)toString(numD).length() / 2)) + "\"   y=\"230\">" + toString(numD) + "</text>\n";  
493                                         outsvg << "<text fill=\"black\" class=\"seri\"  x=\"" + toString(150 - ((int)lookup[3]->getGroup().length() / 2)) + "\"   y=\"210\">" + lookup[3]->getGroup() + "</text>\n"; 
494                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(240 - ((int)toString(sharedBC).length() / 2)) + "\" y=\"325\">" + toString(sharedAD) + "</text>\n"; 
495                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(470 - ((int)toString(sharedBC).length() / 2)) + "\" y=\"325\">" + toString(sharedBD) + "</text>\n";
496                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(350 - ((int)toString(sharedCD).length() / 2)) + "\" y=\"430\">" + toString(sharedCD) + "</text>\n"; 
497                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(275 - ((int)toString(sharedABD).length() / 2)) + "\" y=\"240\">" + toString(sharedABD) + "</text>\n"; 
498                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(400 - ((int)toString(sharedBCD).length() / 2)) + "\" y=\"360\">" + toString(sharedBCD) + "</text>\n";
499                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(305 - ((int)toString(sharedACD).length() / 2)) + "\" y=\"360\">" + toString(sharedACD) + "</text>\n"; 
500                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(440 - ((int)toString(sharedABC).length() / 2)) + "\"  y=\"240\">" + toString(sharedABC) + "</text>\n"; 
501                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(350 - ((int)toString(sharedABCD).length() / 2)) + "\"  y=\"320\">" + toString(sharedABCD) + "</text>\n"; 
502                                         outsvg << "<text fill=\"black\" class=\"seri\" x=\"250\" y=\"490\">The total richness of all the groups is " + toString((float)(numA + numB + numC + numD + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD)) + "</text>\n";
503                         
504                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"510\">Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[2] + " is " + toString(((sharedAC + sharedACD + sharedABC + sharedABCD) / (float)(numA + numC + sharedAB + sharedAC + sharedAD + sharedBC + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
505                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"530\">Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[3] + " is " + toString(((sharedAD + sharedACD + sharedABD + sharedABCD) / (float)(numA + numD + sharedAB + sharedAC + sharedAD + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
506                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"550\">Percentage of species that are shared in groups " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString(((sharedBC + sharedABC + sharedBCD + sharedABCD) / (float)(numB + numC + sharedAB + sharedAC + sharedCD + sharedBD + sharedBC + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
507                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"570\">Percentage of species that are shared in groups " + globaldata->Groups[1] + " and " + globaldata->Groups[3] + " is " + toString(((sharedBD + sharedABD + sharedBCD + sharedABCD) / (float)(numB + numD + sharedAB + sharedAD + sharedCD + sharedBD + sharedBC + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
508                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"590\">Percentage of species that are shared in groups " + globaldata->Groups[2] + " and " + globaldata->Groups[3] + " is " + toString(((sharedCD + sharedBCD + sharedACD + sharedABCD) / (float)(numC + numD + sharedAC + sharedAD + sharedCD + sharedBD + sharedBC + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
509                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"610\">Percentage of species that are shared in groups " + globaldata->Groups[0] + ", " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString(((sharedABC + sharedABCD) / (float)(numA + numB + numC + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
510                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"630\">Percentage of species that are shared in groups " + globaldata->Groups[0] + ", " + globaldata->Groups[1] + " and " + globaldata->Groups[3] + " is " + toString(((sharedABD + sharedABCD) / (float)(numA + numB + numD + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
511                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"650\">Percentage of species that are shared in groups " + globaldata->Groups[0] + ", " + globaldata->Groups[2] + " and " + globaldata->Groups[3] + " is " + toString(((sharedACD + sharedABCD) / (float)(numA + numC + numD + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
512                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"670\">Percentage of species that are shared in groups " + globaldata->Groups[1] + ", " + globaldata->Groups[2] + " and " + globaldata->Groups[3] + " is " + toString(((sharedBCD + sharedABCD) / (float)(numB + numC + numD + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
513                                         //outsvg << "<text fill=\"black\" class=\"seri\" x=\"100\" y=\"690\">Percentage of species that are shared in groups " + globaldata->Groups[0] + ", " + globaldata->Groups[1] + ", " + globaldata->Groups[2] + " and " + globaldata->Groups[3] + " is " + toString((sharedABCD / (float)(numA + numB + numC + numD + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedCD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "</text>\n";
514                 
515                                         outsvg << "</g>\n</svg>\n";
516                                         outsvg.close();
517                                         delete singleCalc;
518                                 }
519                         }
520                         delete sabundA;
521                         delete sabundB;
522                         delete sabundC;
523                         delete sabundD;
524                 }
525                 
526         }
527         catch(exception& e) {
528                 cout << "Standard Error: " << e.what() << " has occurred in the Venn class Function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
529                 exit(1);
530         }
531         catch(...) {
532                 cout << "An unknown error has occurred in the Venn class function getPic. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
533                 exit(1);
534         }
535 }
536
537