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"
20 /***********************************************************************/
22 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
24 /***********************************************************************/
26 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){};
28 /***********************************************************************/
29 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
31 globaldata = GlobalData::getInstance();
33 //set up groupmap for later.
34 groupmap = new GroupMap(globaldata->getGroupFile());
41 data.assign(hold, "");
43 for(int i=0;i<hold;i++){
50 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
54 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
59 /***********************************************************************/
60 void SharedListVector::set(int binNumber, string seqNames){
62 int nNames_old = getNumNames(data[binNumber]);
63 data[binNumber] = seqNames;
64 int nNames_new = getNumNames(seqNames);
66 if(nNames_old == 0) { numBins++; }
67 if(nNames_new == 0) { numBins--; }
68 if(nNames_new > maxRank) { maxRank = nNames_new; }
70 numSeqs += (nNames_new - nNames_old);
73 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 /***********************************************************************/
84 string SharedListVector::get(int index){
88 /***********************************************************************/
90 void SharedListVector::push_back(string seqNames){
92 data.push_back(seqNames);
93 int nNames = getNumNames(seqNames);
97 if(nNames > maxRank) { maxRank = nNames; }
101 catch(exception& e) {
102 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111 /***********************************************************************/
113 void SharedListVector::resize(int size){
117 /***********************************************************************/
119 int SharedListVector::size(){
122 /***********************************************************************/
124 void SharedListVector::clear(){
132 /***********************************************************************/
134 void SharedListVector::print(ostream& output){
136 output << label << '\t' << numBins << '\t';
138 for(int i=0;i<data.size();i++){
140 output << data[i] << '\t';
145 catch(exception& e) {
146 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 /***********************************************************************/
158 RAbundVector SharedListVector::getRAbundVector(){
162 for(int i=0;i<data.size();i++){
163 int binSize = getNumNames(data[i]);
164 rav.push_back(binSize);
167 // This was here before to output data in a nice format, but it screws up the name mapping steps
168 // sort(rav.rbegin(), rav.rend());
170 // for(int i=data.size()-1;i>=0;i--){
171 // if(rav.get(i) == 0){ rav.pop_back(); }
180 catch(exception& e) {
181 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190 /***********************************************************************/
192 SAbundVector SharedListVector::getSAbundVector(){
194 SAbundVector sav(maxRank+1);
196 for(int i=0;i<data.size();i++){
197 int binSize = getNumNames(data[i]);
198 sav.set(binSize, sav.get(binSize) + 1);
205 catch(exception& e) {
206 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 /***********************************************************************/
216 SharedOrderVector* SharedListVector::getSharedOrderVector(){
218 string groupName, names, name;
220 SharedOrderVector* order = new SharedOrderVector();
221 order->setLabel(label);
223 for(int i=0;i<numBins;i++){
224 int binSize = getNumNames(get(i)); //find number of individual in given bin
226 while (names.find_first_of(',') != -1) {
227 name = names.substr(0,names.find_first_of(','));
228 names = names.substr(names.find_first_of(',')+1, names.length());
229 groupName = groupmap->getGroup(name);
230 order->push_back(i, binSize, groupName); //i represents what bin you are in
233 groupName = groupmap->getGroup(names);
234 order->push_back(i, binSize, groupName);
236 random_shuffle(order->begin(), order->end());
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 /***********************************************************************/
250 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
252 SharedRAbundVector rav(data.size());
253 string group, names, name;
255 for(int i=0;i<numBins;i++){
257 while (names.find_first_of(',') != -1) {
258 name = names.substr(0,names.find_first_of(','));
259 names = names.substr(names.find_first_of(',')+1, names.length());
260 group = groupmap->getGroup(name);
261 if (group == groupName) { //this name is in the group you want the vector for.
262 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
267 groupName = groupmap->getGroup(names);
268 if (group == groupName) { //this name is in the group you want the vector for.
269 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
274 rav.setGroup(groupName);
278 catch(exception& e) {
279 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 /***********************************************************************/
289 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
291 SharedSAbundVector sav;
292 SharedRAbundVector rav;
294 rav = this->getSharedRAbundVector(groupName);
295 sav = rav.getSharedSAbundVector();
299 catch(exception& e) {
300 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308 /***********************************************************************/
310 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
313 if(orderMap == NULL){
316 for(int i=0;i<data.size();i++){
317 int binSize = getNumNames(data[i]);
318 for(int j=0;j<binSize;j++){
322 random_shuffle(ov.begin(), ov.end());
330 OrderVector ov(numSeqs);
332 for(int i=0;i<data.size();i++){
333 string listOTU = data[i];
334 int length = listOTU.size();
338 for(int j=0;j<length;j++){
340 if(listOTU[j] != ','){
341 seqName += listOTU[j];
344 if(orderMap->count(seqName) == 0){
345 cerr << seqName << " not found, check *.names file\n";
349 ov.set((*orderMap)[seqName], i);
354 if(orderMap->count(seqName) == 0){
355 cerr << seqName << " not found, check *.names file\n";
358 ov.set((*orderMap)[seqName], i);
367 catch(exception& e) {
368 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
372 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 /***********************************************************************/