2 * sharedSharedListVector.cpp
5 * Created by Sarah Westcott on 1/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sabundvector.hpp"
11 #include "rabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedlistvector.h"
14 #include "sharedordervector.h"
15 #include "sharedutilities.h"
17 /***********************************************************************/
19 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance(); groupmap = NULL; }
21 /***********************************************************************/
23 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance(); groupmap = NULL; }
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
28 globaldata = GlobalData::getInstance();
30 //set up groupmap for later.
31 groupmap = new GroupMap(globaldata->getGroupFile());
38 data.assign(hold, "");
40 for(int i=0;i<hold;i++){
47 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56 /***********************************************************************/
57 void SharedListVector::set(int binNumber, string seqNames){
59 int nNames_old = getNumNames(data[binNumber]);
60 data[binNumber] = seqNames;
61 int nNames_new = getNumNames(seqNames);
63 if(nNames_old == 0) { numBins++; }
64 if(nNames_new == 0) { numBins--; }
65 if(nNames_new > maxRank) { maxRank = nNames_new; }
67 numSeqs += (nNames_new - nNames_old);
70 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 /***********************************************************************/
81 string SharedListVector::get(int index){
85 /***********************************************************************/
87 void SharedListVector::push_back(string seqNames){
89 data.push_back(seqNames);
90 int nNames = getNumNames(seqNames);
94 if(nNames > maxRank) { maxRank = nNames; }
99 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 /***********************************************************************/
110 void SharedListVector::resize(int size){
114 /***********************************************************************/
116 int SharedListVector::size(){
119 /***********************************************************************/
121 void SharedListVector::clear(){
129 /***********************************************************************/
131 void SharedListVector::print(ostream& output){
133 output << label << '\t' << numBins << '\t';
135 for(int i=0;i<data.size();i++){
137 output << data[i] << '\t';
142 catch(exception& e) {
143 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 /***********************************************************************/
155 RAbundVector SharedListVector::getRAbundVector(){
159 for(int i=0;i<data.size();i++){
160 int binSize = getNumNames(data[i]);
161 rav.push_back(binSize);
164 // This was here before to output data in a nice format, but it screws up the name mapping steps
165 // sort(rav.rbegin(), rav.rend());
167 // for(int i=data.size()-1;i>=0;i--){
168 // if(rav.get(i) == 0){ rav.pop_back(); }
177 catch(exception& e) {
178 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 /***********************************************************************/
189 SAbundVector SharedListVector::getSAbundVector(){
191 SAbundVector sav(maxRank+1);
193 for(int i=0;i<data.size();i++){
194 int binSize = getNumNames(data[i]);
195 sav.set(binSize, sav.get(binSize) + 1);
202 catch(exception& e) {
203 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212 /***********************************************************************/
213 SharedOrderVector* SharedListVector::getSharedOrderVector(){
215 string groupName, names, name;
217 SharedOrderVector* order = new SharedOrderVector();
218 order->setLabel(label);
220 for(int i=0;i<numBins;i++){
221 int binSize = getNumNames(get(i)); //find number of individual in given bin
223 while (names.find_first_of(',') != -1) {
224 name = names.substr(0,names.find_first_of(','));
225 names = names.substr(names.find_first_of(',')+1, names.length());
226 groupName = groupmap->getGroup(name);
227 order->push_back(i, binSize, groupName); //i represents what bin you are in
230 groupName = groupmap->getGroup(names);
231 order->push_back(i, binSize, groupName);
234 random_shuffle(order->begin(), order->end());
235 order->updateStats();
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);
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;
294 map<string, SharedRAbundVector*> finder;
295 string group, names, name;
297 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
301 for (int i = 0; i < globaldata->Groups.size(); i++) {
302 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
303 finder[globaldata->Groups[i]] = temp;
304 finder[globaldata->Groups[i]]->setLabel(label);
305 finder[globaldata->Groups[i]]->setGroup(globaldata->Groups[i]);
306 //*temp = getSharedRAbundVector(globaldata->Groups[i]);
307 lookup.push_back(finder[globaldata->Groups[i]]);
311 for(int i=0;i<numBins;i++){
313 while (names.find_first_of(',') != -1) {
314 name = names.substr(0,names.find_first_of(','));
315 names = names.substr(names.find_first_of(',')+1, names.length());
316 group = groupmap->getGroup(name);
317 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
321 group = groupmap->getGroup(names);
322 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
328 catch(exception& e) {
329 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
333 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 /***********************************************************************/
340 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
342 SharedSAbundVector sav;
343 SharedRAbundVector rav;
345 rav = this->getSharedRAbundVector(groupName);
346 sav = rav.getSharedSAbundVector();
350 catch(exception& e) {
351 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
355 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
359 /***********************************************************************/
361 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
364 if(orderMap == NULL){
367 for(int i=0;i<data.size();i++){
368 int binSize = getNumNames(data[i]);
369 for(int j=0;j<binSize;j++){
373 random_shuffle(ov.begin(), ov.end());
381 OrderVector ov(numSeqs);
383 for(int i=0;i<data.size();i++){
384 string listOTU = data[i];
385 int length = listOTU.size();
389 for(int j=0;j<length;j++){
391 if(listOTU[j] != ','){
392 seqName += listOTU[j];
395 if(orderMap->count(seqName) == 0){
396 cerr << seqName << " not found, check *.names file\n";
400 ov.set((*orderMap)[seqName], i);
405 if(orderMap->count(seqName) == 0){
406 cerr << seqName << " not found, check *.names file\n";
409 ov.set((*orderMap)[seqName], i);
418 catch(exception& e) {
419 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
423 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428 /***********************************************************************/