From 38fd0bcfcb1a77ce58a0d1c8ec7c8c8541cf3892 Mon Sep 17 00:00:00 2001 From: westcott Date: Thu, 2 Apr 2009 15:21:36 +0000 Subject: [PATCH] venn command with up to 4 groups --- validparameter.cpp | 2 +- venn.cpp | 162 ++++++++++++++++++++++++++++++++------------- venncommand.cpp | 20 ++++-- 3 files changed, 130 insertions(+), 54 deletions(-) diff --git a/validparameter.cpp b/validparameter.cpp index 4ba29a7..084068d 100644 --- a/validparameter.cpp +++ b/validparameter.cpp @@ -286,7 +286,7 @@ void ValidParameters::initCommandParameters() { string heatmapArray[] = {"group","line","label","sorted"}; commandParameters["heatmap"] = addParameters(heatmapArray, sizeof(heatmapArray)/sizeof(string)); - string vennArray[] = {"group","line","label"}; + string vennArray[] = {"groups","line","label"}; commandParameters["venn"] = addParameters(vennArray, sizeof(vennArray)/sizeof(string)); string quitArray[] = {}; diff --git a/venn.cpp b/venn.cpp index 282ca98..98c63a1 100644 --- a/venn.cpp +++ b/venn.cpp @@ -37,11 +37,13 @@ void Venn::getPic(OrderVector* order) { //svg image - outsvg << "\n"; + outsvg << "\n"; outsvg << "\n"; - outsvg << ""; - outsvg << "" + toString(rabund.getNumBins()) + "\n"; + outsvg << ""; + outsvg << "Venn Diagram at distance " + order->getLabel() + "\n"; + outsvg << ""; + outsvg << "" + toString(rabund.getNumBins()) + "\n"; outsvg << "\n\n"; outsvg.close(); @@ -70,9 +72,17 @@ void Venn::getPic(SharedOrderVector* sharedorder) { outsvg << "\n"; if (lookup.size() == 1) { - outsvg << ""; - outsvg << "" + toString(lookup[0]->getNumBins()) + "\n"; - outsvg << "\n\n"; + int numA = 0; + //for each bin + for (int i = 0; i < lookup[0]->size(); i++) { + //are they only in one + if (lookup[0]->getAbundance(i) != 0) { numA++; } + } + + outsvg << ""; + outsvg << "Venn Diagram at distance " + sharedorder->getLabel() + "\n"; + outsvg << ""; + outsvg << "" + toString(numA) + "\n"; }else if (lookup.size() == 2) { //calc the shared otu @@ -80,42 +90,25 @@ void Venn::getPic(SharedOrderVector* sharedorder) { int numA = 0; int numB = 0; - float rScaler; - //for each bin for (int i = 0; i < lookup[0]->size(); i++) { - if (lookup[0]->getAbundance(i) != 0) { numA++; } - if (lookup[1]->getAbundance(i) != 0) { numB++; } + //are they only in one + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0)) { numA++; } + if ((lookup[1]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0)) { numB++; } //are they shared if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { shared++; } } - if (numA > numB) { rScaler = 150 / float(numA); } - else { rScaler = 150 / float(numB); } - - //to determine how far over to overlap b onto a. - float percentOverlap = shared / (float) numA; - - int bx = 200 + (numA * rScaler) - ((2 * (numA * rScaler)) * percentOverlap) + (numB * rScaler); - int leftedgeB = bx - (numB * rScaler); //center b - b's radius - int leftedgeA = 200 - (numA * rScaler); //center a - a's radius - int rightedgeB = bx + (numB * rScaler); //center b + b's radius - int rightedgeA = 200 + (numA * rScaler); //center a + a's radius - - int mida = leftedgeA + ((leftedgeB - leftedgeA) / 2); - int midb = rightedgeA + ((rightedgeB - rightedgeA) / 2); - int midab = leftedgeB + ((rightedgeA - leftedgeB) / 2); - //draw circles outsvg << ""; - outsvg << "Venn Diagram at distance " + sharedorder->getLabel() + "\n"; - outsvg << ""; - outsvg << ""; - outsvg << "" + toString(numA-shared) + "\n"; - outsvg << "" + toString(shared) + "\n"; - outsvg << "" + toString(numB-shared) + "\n"; - outsvg << "Percentage of species that are shared is " + toString((shared / (float)(numA + numB - shared))) + "\n"; - + outsvg << "Venn Diagram at distance " + sharedorder->getLabel() + "\n"; + outsvg << ""; + outsvg << ""; + outsvg << "" + toString(numA) + "\n"; + outsvg << "" + toString(numB) + "\n"; + outsvg << "" + toString(shared) + "\n"; + outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString((shared / (float)(numA + numB + shared))) + "\n"; + }else if (lookup.size() == 3) { //calc the shared otu int sharedABC = 0; @@ -126,13 +119,14 @@ void Venn::getPic(SharedOrderVector* sharedorder) { //for each bin for (int i = 0; i < lookup[0]->size(); i++) { - if (lookup[0]->getAbundance(i) != 0) { numA++; } - if (lookup[1]->getAbundance(i) != 0) { numB++; } - if (lookup[2]->getAbundance(i) != 0) { numC++; } + //are they only in one + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[2]->getAbundance(i) == 0)) { numA++; } + if ((lookup[1]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0) && (lookup[2]->getAbundance(i) == 0)) { numB++; } + if ((lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[0]->getAbundance(i) == 0)) { numC++; } //are they shared by 2 - if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { sharedAB++; } - if ((lookup[0]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0)) { sharedAC++; } - if ((lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0)) { sharedBC++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) == 0)) { sharedAB++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0)) { sharedAC++; } + if ((lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0)) { sharedBC++; } //are they shared by all if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0)) { sharedABC++; } @@ -146,19 +140,93 @@ void Venn::getPic(SharedOrderVector* sharedorder) { outsvg << ""; //place labels within overlaps - outsvg << "" + toString(numA-sharedAB-sharedAC+sharedABC) + "\n"; + outsvg << "" + toString(numA) + "\n"; outsvg << "" + toString(sharedAB) + "\n"; - outsvg << "" + toString(numB-sharedAB-sharedBC+sharedABC) + "\n"; + outsvg << "" + toString(numB) + "\n"; outsvg << "" + toString(sharedAC) + "\n"; - outsvg << "" + toString(numC-sharedAC-sharedBC+sharedABC) + "\n"; + outsvg << "" + toString(numC) + "\n"; outsvg << "" + toString(sharedBC) + "\n"; outsvg << "" + toString(sharedABC) + "\n"; - outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString((sharedAB / (float)(numA + numB - sharedAB))) + "\n"; - outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[2] + " is " + toString((sharedAC / (float)(numA + numC - sharedAC))) + "\n"; - outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString((sharedBC / (float)(numB + numC - sharedBC))) + "\n"; - outsvg << "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)))) + "\n"; + outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString(((sharedAB + sharedABC) / (float)(numA + numB + sharedAB + sharedABC))) + "\n"; + outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[2] + " is " + toString(((sharedAC + sharedABC) / (float)(numA + numC + sharedAC + sharedABC))) + "\n"; + outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[1] + " and " + globaldata->Groups[2] + " is " + toString(((sharedBC + sharedABC) / (float)(numB + numC + sharedBC + sharedABC))) + "\n"; + outsvg << "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 + sharedABC))) + "\n"; + + }else if (lookup.size() == 4) { + //calc the shared otu + int sharedABCD = 0; + int numA = 0; int numB = 0; int numC = 0; int numD = 0; + int sharedAB = 0; int sharedAC = 0; int sharedBC = 0; int sharedAD = 0; int sharedBD = 0; int sharedCD = 0; + int sharedABC = 0; int sharedACD = 0; int sharedBCD = 0; int sharedABD = 0; + + //A = red, B = green, C = blue, D = yellow + + //float scalerB; + + //for each bin + for (int i = 0; i < lookup[0]->size(); i++) { + //are they only in one + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[2]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { numA++; } + if ((lookup[1]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0) && (lookup[2]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { numB++; } + if ((lookup[2]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { numC++; } + if ((lookup[3]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[2]->getAbundance(i) == 0)) { numD++; } + //are they shared by 2 + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { sharedAB++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { sharedAC++; } + if ((lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0) && (lookup[3]->getAbundance(i) == 0)) { sharedBC++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) == 0) && (lookup[1]->getAbundance(i) == 0)) { sharedAD++; } + if ((lookup[3]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) == 0) && (lookup[0]->getAbundance(i) == 0)) { sharedBD++; } + if ((lookup[2]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0) && (lookup[0]->getAbundance(i) == 0)) { sharedCD++; } + //are they shared by 3 + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) == 0)) { sharedABC++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) == 0)) { sharedACD++; } + if ((lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0) && (lookup[0]->getAbundance(i) == 0)) { sharedBCD++; } + if ((lookup[0]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) == 0)) { sharedABD++; } + //are they shared by all + if ((lookup[0]->getAbundance(i) != 0) && (lookup[1]->getAbundance(i) != 0) && (lookup[2]->getAbundance(i) != 0) && (lookup[3]->getAbundance(i) != 0)) { sharedABCD++; } + } + + //draw circles + outsvg << ""; + outsvg << "Venn Diagram at distance " + sharedorder->getLabel() + "\n"; + outsvg << "\n "; + outsvg << "\n "; + outsvg << "\n "; + outsvg << "\n "; + + //A = red, B = green, C = blue, D = yellow + + //place labels within overlaps + outsvg << "" + toString(numA) + "\n"; + outsvg << "" + toString(sharedAB) + "\n"; + outsvg << "" + toString(numB) + "\n"; + outsvg << "" + toString(sharedAC) + "\n"; + outsvg << "" + toString(numC) + "\n"; + outsvg << "" + toString(sharedBC) + "\n"; + outsvg << "" + toString(numD) + "\n"; + outsvg << "" + toString(sharedAD) + "\n"; + outsvg << "" + toString(sharedBD) + "\n"; + outsvg << "" + toString(sharedCD) + "\n"; + outsvg << "" + toString(sharedABD) + "\n"; + outsvg << "" + toString(sharedBCD) + "\n"; + outsvg << "" + toString(sharedACD) + "\n"; + outsvg << "" + toString(sharedABC) + "\n"; + outsvg << "" + toString(sharedABCD) + "\n"; + + outsvg << "Percentage of species that are shared in groups " + globaldata->Groups[0] + " and " + globaldata->Groups[1] + " is " + toString(((sharedAB + sharedABD + sharedABC + sharedABCD) / (float)(numA + numB + sharedAB + sharedAC + sharedAD + sharedBC + sharedBD + sharedABC + sharedABD + sharedACD + sharedBCD + sharedABCD))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; + outsvg << "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))) + "\n"; } + outsvg << "\n\n"; outsvg.close(); diff --git a/venncommand.cpp b/venncommand.cpp index c8a7dbf..85dcd93 100644 --- a/venncommand.cpp +++ b/venncommand.cpp @@ -141,10 +141,18 @@ void VennCommand::setGroups() { //if the user only entered invalid groups if (globaldata->Groups.size() == 0) { - cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using the first three groups in your groupfile." << endl; - for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) { - globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); + if (globaldata->gGroupmap->namesOfGroups.size() > 4) { + cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using the first four groups in your groupfile." << endl; + for (int i = 0; i < 4; i++) { + globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); + } + }else { + cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; + for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) { + globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]); + } } + } }else{//user has enter "all" and wants the default groups globaldata->Groups.clear(); @@ -161,9 +169,9 @@ void VennCommand::setGroups() { //check to make sure their are only 3 groups - if (globaldata->Groups.size() > 3) { - cout << "You may only use 3 groups at a time with this command. I will choose the first three and disregard the rest." << endl; - for (int i = 3; i < globaldata->Groups.size(); i++) { + if (globaldata->Groups.size() > 4) { + cout << "You may only use up to 4 groups at a time with this command. I will choose the first four and disregard the rest." << endl; + for (int i = 4; i < globaldata->Groups.size(); i++) { globaldata->Groups.erase(globaldata->Groups.begin()+i); } } -- 2.39.2