2 * sharedSharedListVector.cpp
5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
14 #include "sabundvector.hpp"
15 #include "rabundvector.hpp"
16 #include "ordervector.hpp"
17 #include "sharedlistvector.h"
18 #include "sharedordervector.h"
19 #include "sharedutilities.h"
21 /***********************************************************************/
23 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
25 /***********************************************************************/
27 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){};
29 /***********************************************************************/
30 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
32 globaldata = GlobalData::getInstance();
34 //set up groupmap for later.
35 groupmap = new GroupMap(globaldata->getGroupFile());
42 data.assign(hold, "");
44 for(int i=0;i<hold;i++){
51 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60 /***********************************************************************/
61 void SharedListVector::set(int binNumber, string seqNames){
63 int nNames_old = getNumNames(data[binNumber]);
64 data[binNumber] = seqNames;
65 int nNames_new = getNumNames(seqNames);
67 if(nNames_old == 0) { numBins++; }
68 if(nNames_new == 0) { numBins--; }
69 if(nNames_new > maxRank) { maxRank = nNames_new; }
71 numSeqs += (nNames_new - nNames_old);
74 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
78 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83 /***********************************************************************/
85 string SharedListVector::get(int index){
89 /***********************************************************************/
91 void SharedListVector::push_back(string seqNames){
93 data.push_back(seqNames);
94 int nNames = getNumNames(seqNames);
98 if(nNames > maxRank) { maxRank = nNames; }
102 catch(exception& e) {
103 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112 /***********************************************************************/
114 void SharedListVector::resize(int size){
118 /***********************************************************************/
120 int SharedListVector::size(){
123 /***********************************************************************/
125 void SharedListVector::clear(){
133 /***********************************************************************/
135 void SharedListVector::print(ostream& output){
137 output << label << '\t' << numBins << '\t';
139 for(int i=0;i<data.size();i++){
141 output << data[i] << '\t';
146 catch(exception& e) {
147 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157 /***********************************************************************/
159 RAbundVector SharedListVector::getRAbundVector(){
163 for(int i=0;i<data.size();i++){
164 int binSize = getNumNames(data[i]);
165 rav.push_back(binSize);
168 // This was here before to output data in a nice format, but it screws up the name mapping steps
169 // sort(rav.rbegin(), rav.rend());
171 // for(int i=data.size()-1;i>=0;i--){
172 // if(rav.get(i) == 0){ rav.pop_back(); }
181 catch(exception& e) {
182 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
191 /***********************************************************************/
193 SAbundVector SharedListVector::getSAbundVector(){
195 SAbundVector sav(maxRank+1);
197 for(int i=0;i<data.size();i++){
198 int binSize = getNumNames(data[i]);
199 sav.set(binSize, sav.get(binSize) + 1);
206 catch(exception& e) {
207 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
216 /***********************************************************************/
217 SharedOrderVector* SharedListVector::getSharedOrderVector(){
219 string groupName, names, name;
221 SharedOrderVector* order = new SharedOrderVector();
222 order->setLabel(label);
224 for(int i=0;i<numBins;i++){
225 int binSize = getNumNames(get(i)); //find number of individual in given bin
227 while (names.find_first_of(',') != -1) {
228 name = names.substr(0,names.find_first_of(','));
229 names = names.substr(names.find_first_of(',')+1, names.length());
230 groupName = groupmap->getGroup(name);
231 order->push_back(i, binSize, groupName); //i represents what bin you are in
234 groupName = groupmap->getGroup(names);
235 order->push_back(i, binSize, groupName);
237 random_shuffle(order->begin(), order->end());
240 catch(exception& e) {
241 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
250 /***********************************************************************/
251 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
253 SharedRAbundVector rav(data.size());
254 string group, names, name;
256 for(int i=0;i<numBins;i++){
258 while (names.find_first_of(',') != -1) {
259 name = names.substr(0,names.find_first_of(','));
260 names = names.substr(names.find_first_of(',')+1, names.length());
261 group = groupmap->getGroup(name);
262 if (group == groupName) { //this name is in the group you want the vector for.
263 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
268 groupName = groupmap->getGroup(names);
269 if (group == groupName) { //this name is in the group you want the vector for.
270 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
275 rav.setGroup(groupName);
279 catch(exception& e) {
280 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 /***********************************************************************/
289 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
292 util = new SharedUtil();
293 vector<SharedRAbundVector*> lookup;
295 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
296 util->getSharedVectors(globaldata->Groups, lookup, this->getSharedOrderVector());
300 catch(exception& e) {
301 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
305 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 /***********************************************************************/
312 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
314 SharedSAbundVector sav;
315 SharedRAbundVector rav;
317 rav = this->getSharedRAbundVector(groupName);
318 sav = rav.getSharedSAbundVector();
322 catch(exception& e) {
323 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
327 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 /***********************************************************************/
333 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
336 if(orderMap == NULL){
339 for(int i=0;i<data.size();i++){
340 int binSize = getNumNames(data[i]);
341 for(int j=0;j<binSize;j++){
345 random_shuffle(ov.begin(), ov.end());
353 OrderVector ov(numSeqs);
355 for(int i=0;i<data.size();i++){
356 string listOTU = data[i];
357 int length = listOTU.size();
361 for(int j=0;j<length;j++){
363 if(listOTU[j] != ','){
364 seqName += listOTU[j];
367 if(orderMap->count(seqName) == 0){
368 cerr << seqName << " not found, check *.names file\n";
372 ov.set((*orderMap)[seqName], i);
377 if(orderMap->count(seqName) == 0){
378 cerr << seqName << " not found, check *.names file\n";
381 ov.set((*orderMap)[seqName], i);
390 catch(exception& e) {
391 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
395 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
400 /***********************************************************************/