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());
44 data.assign(hold, "");
46 for(int i=0;i<hold;i++){
53 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62 /***********************************************************************/
63 void SharedListVector::set(int binNumber, string seqNames){
65 int nNames_old = getNumNames(data[binNumber]);
66 data[binNumber] = seqNames;
67 int nNames_new = getNumNames(seqNames);
69 if(nNames_old == 0) { numBins++; }
70 if(nNames_new == 0) { numBins--; }
71 if(nNames_new > maxRank) { maxRank = nNames_new; }
73 numSeqs += (nNames_new - nNames_old);
76 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85 /***********************************************************************/
87 string SharedListVector::get(int index){
91 /***********************************************************************/
93 void SharedListVector::push_back(string seqNames){
95 data.push_back(seqNames);
96 int nNames = getNumNames(seqNames);
100 if(nNames > maxRank) { maxRank = nNames; }
104 catch(exception& e) {
105 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
114 /***********************************************************************/
116 void SharedListVector::resize(int size){
120 /***********************************************************************/
122 int SharedListVector::size(){
125 /***********************************************************************/
127 void SharedListVector::clear(){
135 /***********************************************************************/
137 void SharedListVector::print(ostream& output){
139 output << label << '\t' << numBins << '\t';
141 for(int i=0;i<data.size();i++){
143 output << data[i] << '\t';
148 catch(exception& e) {
149 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159 /***********************************************************************/
161 RAbundVector SharedListVector::getRAbundVector(){
165 for(int i=0;i<data.size();i++){
166 int binSize = getNumNames(data[i]);
167 rav.push_back(binSize);
170 // This was here before to output data in a nice format, but it screws up the name mapping steps
171 // sort(rav.rbegin(), rav.rend());
173 // for(int i=data.size()-1;i>=0;i--){
174 // if(rav.get(i) == 0){ rav.pop_back(); }
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /***********************************************************************/
195 SAbundVector SharedListVector::getSAbundVector(){
197 SAbundVector sav(maxRank+1);
199 for(int i=0;i<data.size();i++){
200 int binSize = getNumNames(data[i]);
201 sav.set(binSize, sav.get(binSize) + 1);
208 catch(exception& e) {
209 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218 /***********************************************************************/
219 SharedOrderVector* SharedListVector::getSharedOrderVector(){
221 string groupName, names, name;
223 SharedOrderVector* order = new SharedOrderVector();
224 order->setLabel(label);
226 for(int i=0;i<numBins;i++){
227 int binSize = getNumNames(get(i)); //find number of individual in given bin
229 while (names.find_first_of(',') != -1) {
230 name = names.substr(0,names.find_first_of(','));
231 names = names.substr(names.find_first_of(',')+1, names.length());
232 groupName = groupmap->getGroup(name);
233 order->push_back(i, binSize, groupName); //i represents what bin you are in
236 groupName = groupmap->getGroup(names);
237 order->push_back(i, binSize, groupName);
239 random_shuffle(order->begin(), order->end());
242 catch(exception& e) {
243 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 /***********************************************************************/
253 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
255 SharedRAbundVector rav(data.size());
256 string group, names, name;
258 for(int i=0;i<numBins;i++){
260 while (names.find_first_of(',') != -1) {
261 name = names.substr(0,names.find_first_of(','));
262 names = names.substr(names.find_first_of(',')+1, names.length());
263 group = groupmap->getGroup(name);
264 if (group == groupName) { //this name is in the group you want the vector for.
265 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
270 groupName = groupmap->getGroup(names);
271 if (group == groupName) { //this name is in the group you want the vector for.
272 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
277 rav.setGroup(groupName);
281 catch(exception& e) {
282 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 /***********************************************************************/
292 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
294 SharedSAbundVector sav;
295 SharedRAbundVector rav;
297 rav = this->getSharedRAbundVector(groupName);
298 sav = rav.getSharedSAbundVector();
302 catch(exception& e) {
303 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 /***********************************************************************/
313 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
316 if(orderMap == NULL){
319 for(int i=0;i<data.size();i++){
320 int binSize = getNumNames(data[i]);
321 for(int j=0;j<binSize;j++){
325 random_shuffle(ov.begin(), ov.end());
333 OrderVector ov(numSeqs);
335 for(int i=0;i<data.size();i++){
336 string listOTU = data[i];
337 int length = listOTU.size();
341 for(int j=0;j<length;j++){
343 if(listOTU[j] != ','){
344 seqName += listOTU[j];
347 if(orderMap->count(seqName) == 0){
348 cerr << seqName << " not found, check *.names file\n";
352 ov.set((*orderMap)[seqName], i);
357 if(orderMap->count(seqName) == 0){
358 cerr << seqName << " not found, check *.names file\n";
361 ov.set((*orderMap)[seqName], i);
370 catch(exception& e) {
371 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
375 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
380 /***********************************************************************/