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);
238 random_shuffle(order->begin(), order->end());
239 order->updateStats();
243 catch(exception& e) {
244 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253 /***********************************************************************/
254 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
256 SharedRAbundVector rav(data.size());
257 string group, names, name;
259 for(int i=0;i<numBins;i++){
261 while (names.find_first_of(',') != -1) {
262 name = names.substr(0,names.find_first_of(','));
263 names = names.substr(names.find_first_of(',')+1, names.length());
264 group = groupmap->getGroup(name);
265 if (group == groupName) { //this name is in the group you want the vector for.
266 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
271 groupName = groupmap->getGroup(names);
272 if (group == groupName) { //this name is in the group you want the vector for.
273 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
278 rav.setGroup(groupName);
282 catch(exception& e) {
283 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 /***********************************************************************/
292 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
295 util = new SharedUtil();
296 vector<SharedRAbundVector*> lookup;
298 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
300 for (int i = 0; i < globaldata->Groups.size(); i++) {
301 SharedRAbundVector* temp = new SharedRAbundVector();
302 *temp = getSharedRAbundVector(globaldata->Groups[i]);
303 lookup.push_back(temp);
308 catch(exception& e) {
309 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319 /***********************************************************************/
320 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
322 SharedSAbundVector sav;
323 SharedRAbundVector rav;
325 rav = this->getSharedRAbundVector(groupName);
326 sav = rav.getSharedSAbundVector();
330 catch(exception& e) {
331 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
335 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 /***********************************************************************/
341 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
344 if(orderMap == NULL){
347 for(int i=0;i<data.size();i++){
348 int binSize = getNumNames(data[i]);
349 for(int j=0;j<binSize;j++){
353 random_shuffle(ov.begin(), ov.end());
361 OrderVector ov(numSeqs);
363 for(int i=0;i<data.size();i++){
364 string listOTU = data[i];
365 int length = listOTU.size();
369 for(int j=0;j<length;j++){
371 if(listOTU[j] != ','){
372 seqName += listOTU[j];
375 if(orderMap->count(seqName) == 0){
376 cerr << seqName << " not found, check *.names file\n";
380 ov.set((*orderMap)[seqName], i);
385 if(orderMap->count(seqName) == 0){
386 cerr << seqName << " not found, check *.names file\n";
389 ov.set((*orderMap)[seqName], i);
398 catch(exception& e) {
399 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
408 /***********************************************************************/