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"
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());
40 data.assign(hold, "");
41 string inputData = "";
43 for(int i=0;i<hold;i++){
49 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58 /***********************************************************************/
59 void SharedListVector::set(int binNumber, string seqNames){
61 int nNames_old = getNumNames(data[binNumber]);
62 data[binNumber] = seqNames;
63 int nNames_new = getNumNames(seqNames);
65 if(nNames_old == 0) { numBins++; }
66 if(nNames_new == 0) { numBins--; }
67 if(nNames_new > maxRank) { maxRank = nNames_new; }
69 numSeqs += (nNames_new - nNames_old);
72 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81 /***********************************************************************/
83 string SharedListVector::get(int index){
87 /***********************************************************************/
89 void SharedListVector::push_back(string seqNames){
91 data.push_back(seqNames);
92 int nNames = getNumNames(seqNames);
96 if(nNames > maxRank) { maxRank = nNames; }
100 catch(exception& e) {
101 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
110 /***********************************************************************/
112 void SharedListVector::resize(int size){
116 /***********************************************************************/
118 int SharedListVector::size(){
121 /***********************************************************************/
123 void SharedListVector::clear(){
131 /***********************************************************************/
133 void SharedListVector::print(ostream& output){
135 output << label << '\t' << numBins << '\t';
137 for(int i=0;i<data.size();i++){
139 output << data[i] << '\t';
144 catch(exception& e) {
145 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155 /***********************************************************************/
157 RAbundVector SharedListVector::getRAbundVector(){
161 for(int i=0;i<data.size();i++){
162 int binSize = getNumNames(data[i]);
163 rav.push_back(binSize);
166 // This was here before to output data in a nice format, but it screws up the name mapping steps
167 // sort(rav.rbegin(), rav.rend());
169 // for(int i=data.size()-1;i>=0;i--){
170 // if(rav.get(i) == 0){ rav.pop_back(); }
179 catch(exception& e) {
180 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 /***********************************************************************/
191 SAbundVector SharedListVector::getSAbundVector(){
193 SAbundVector sav(maxRank+1);
195 for(int i=0;i<data.size();i++){
196 int binSize = getNumNames(data[i]);
197 sav.set(binSize, sav.get(binSize) + 1);
204 catch(exception& e) {
205 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214 /***********************************************************************/
215 SharedOrderVector* SharedListVector::getSharedOrderVector(){
217 string groupName, names, name;
219 SharedOrderVector* order = new SharedOrderVector();
220 order->setLabel(label);
222 for(int i=0;i<numBins;i++){
223 int binSize = getNumNames(get(i)); //find number of individual in given bin
225 while (names.find_first_of(',') != -1) {
226 name = names.substr(0,names.find_first_of(','));
227 names = names.substr(names.find_first_of(',')+1, names.length());
228 groupName = groupmap->getGroup(name);
229 order->push_back(i, binSize, groupName); //i represents what bin you are in
232 groupName = groupmap->getGroup(names);
233 order->push_back(i, binSize, groupName);
235 random_shuffle(order->begin(), order->end());
238 catch(exception& e) {
239 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
243 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 /***********************************************************************/
249 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
251 SharedRAbundVector rav(data.size());
252 string group, names, name;
254 for(int i=0;i<numBins;i++){
256 while (names.find_first_of(',') != -1) {
257 name = names.substr(0,names.find_first_of(','));
258 names = names.substr(names.find_first_of(',')+1, names.length());
259 group = groupmap->getGroup(name);
260 if (group == groupName) { //this name is in the group you want the vector for.
261 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
266 groupName = groupmap->getGroup(names);
267 if (group == groupName) { //this name is in the group you want the vector for.
268 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
273 rav.setGroup(groupName);
277 catch(exception& e) {
278 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /***********************************************************************/
288 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
290 SharedSAbundVector sav;
291 SharedRAbundVector rav;
293 rav = this->getSharedRAbundVector(groupName);
294 sav = rav.getSharedSAbundVector();
298 catch(exception& e) {
299 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307 /***********************************************************************/
309 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
312 if(orderMap == NULL){
315 for(int i=0;i<data.size();i++){
316 int binSize = getNumNames(data[i]);
317 for(int j=0;j<binSize;j++){
321 random_shuffle(ov.begin(), ov.end());
329 OrderVector ov(numSeqs);
331 for(int i=0;i<data.size();i++){
332 string listOTU = data[i];
333 int length = listOTU.size();
337 for(int j=0;j<length;j++){
339 if(listOTU[j] != ','){
340 seqName += listOTU[j];
343 if(orderMap->count(seqName) == 0){
344 cerr << seqName << " not found, check *.names file\n";
348 ov.set((*orderMap)[seqName], i);
353 if(orderMap->count(seqName) == 0){
354 cerr << seqName << " not found, check *.names file\n";
357 ov.set((*orderMap)[seqName], i);
366 catch(exception& e) {
367 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
371 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
376 /***********************************************************************/