2 * sharedSharedListVector.cpp
5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
15 #include "sabundvector.hpp"
16 #include "rabundvector.hpp"
17 #include "ordervector.hpp"
18 #include "datavector.hpp"
19 #include "utilities.hpp"
20 #include "sharedlistvector.h"
21 #include "sharedordervector.h"
23 /***********************************************************************/
25 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
27 /***********************************************************************/
29 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){};
31 /***********************************************************************/
32 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
34 globaldata = GlobalData::getInstance();
36 //set up groupmap for later.
37 groupmap = new GroupMap(globaldata->getGroupFile());
43 data.assign(hold, "");
44 string inputData = "";
46 for(int i=0;i<hold;i++){
52 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61 /***********************************************************************/
62 void SharedListVector::set(int binNumber, string seqNames){
64 int nNames_old = getNumNames(data[binNumber]);
65 data[binNumber] = seqNames;
66 int nNames_new = getNumNames(seqNames);
68 if(nNames_old == 0) { numBins++; }
69 if(nNames_new == 0) { numBins--; }
70 if(nNames_new > maxRank) { maxRank = nNames_new; }
72 numSeqs += (nNames_new - nNames_old);
75 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84 /***********************************************************************/
86 string SharedListVector::get(int index){
90 /***********************************************************************/
92 void SharedListVector::push_back(string seqNames){
94 data.push_back(seqNames);
95 int nNames = getNumNames(seqNames);
99 if(nNames > maxRank) { maxRank = nNames; }
103 catch(exception& e) {
104 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113 /***********************************************************************/
115 void SharedListVector::resize(int size){
119 /***********************************************************************/
121 int SharedListVector::size(){
124 /***********************************************************************/
126 void SharedListVector::clear(){
134 /***********************************************************************/
136 void SharedListVector::print(ostream& output){
138 output << label << '\t' << numBins << '\t';
140 for(int i=0;i<data.size();i++){
142 output << data[i] << '\t';
147 catch(exception& e) {
148 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158 /***********************************************************************/
160 RAbundVector SharedListVector::getRAbundVector(){
164 for(int i=0;i<data.size();i++){
165 int binSize = getNumNames(data[i]);
166 rav.push_back(binSize);
169 // This was here before to output data in a nice format, but it screws up the name mapping steps
170 // sort(rav.rbegin(), rav.rend());
172 // for(int i=data.size()-1;i>=0;i--){
173 // if(rav.get(i) == 0){ rav.pop_back(); }
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 /***********************************************************************/
194 SAbundVector SharedListVector::getSAbundVector(){
196 SAbundVector sav(maxRank+1);
198 for(int i=0;i<data.size();i++){
199 int binSize = getNumNames(data[i]);
200 sav.set(binSize, sav.get(binSize) + 1);
207 catch(exception& e) {
208 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217 /***********************************************************************/
218 SharedOrderVector* SharedListVector::getSharedOrderVector(){
220 string groupName, names, name;
222 SharedOrderVector* order = new SharedOrderVector();
223 order->setLabel(label);
225 for(int i=0;i<numBins;i++){
226 int binSize = getNumNames(get(i)); //find number of individual in given bin
228 while (names.find_first_of(',') != -1) {
229 name = names.substr(0,names.find_first_of(','));
230 names = names.substr(names.find_first_of(',')+1, names.length());
231 groupName = groupmap->getGroup(name);
232 order->push_back(i, binSize, groupName); //i represents what bin you are in
235 groupName = groupmap->getGroup(names);
236 order->push_back(i, binSize, groupName);
238 random_shuffle(order->begin(), order->end());
241 catch(exception& e) {
242 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251 /***********************************************************************/
252 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
254 SharedRAbundVector rav(data.size());
255 string group, names, name;
257 for(int i=0;i<numBins;i++){
259 while (names.find_first_of(',') != -1) {
260 name = names.substr(0,names.find_first_of(','));
261 names = names.substr(names.find_first_of(',')+1, names.length());
262 group = groupmap->getGroup(name);
263 if (group == groupName) { //this name is in the group you want the vector for.
264 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
269 groupName = groupmap->getGroup(names);
270 if (group == groupName) { //this name is in the group you want the vector for.
271 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
276 rav.setGroup(groupName);
280 catch(exception& e) {
281 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290 /***********************************************************************/
291 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
293 SharedSAbundVector sav;
294 SharedRAbundVector rav;
296 rav = this->getSharedRAbundVector(groupName);
297 sav = rav.getSharedSAbundVector();
301 catch(exception& e) {
302 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310 /***********************************************************************/
312 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
315 if(orderMap == NULL){
318 for(int i=0;i<data.size();i++){
319 int binSize = getNumNames(data[i]);
320 for(int j=0;j<binSize;j++){
324 random_shuffle(ov.begin(), ov.end());
332 OrderVector ov(numSeqs);
334 for(int i=0;i<data.size();i++){
335 string listOTU = data[i];
336 int length = listOTU.size();
340 for(int j=0;j<length;j++){
342 if(listOTU[j] != ','){
343 seqName += listOTU[j];
346 if(orderMap->count(seqName) == 0){
347 cerr << seqName << " not found, check *.names file\n";
351 ov.set((*orderMap)[seqName], i);
356 if(orderMap->count(seqName) == 0){
357 cerr << seqName << " not found, check *.names file\n";
360 ov.set((*orderMap)[seqName], i);
369 catch(exception& e) {
370 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
379 /***********************************************************************/