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 errorOut(e, "SharedListVector", "SharedListVector");
52 /***********************************************************************/
53 void SharedListVector::set(int binNumber, string seqNames){
55 int nNames_old = getNumNames(data[binNumber]);
56 data[binNumber] = seqNames;
57 int nNames_new = getNumNames(seqNames);
59 if(nNames_old == 0) { numBins++; }
60 if(nNames_new == 0) { numBins--; }
61 if(nNames_new > maxRank) { maxRank = nNames_new; }
63 numSeqs += (nNames_new - nNames_old);
66 errorOut(e, "SharedListVector", "set");
71 /***********************************************************************/
73 string SharedListVector::get(int index){
77 /***********************************************************************/
79 void SharedListVector::push_back(string seqNames){
81 data.push_back(seqNames);
82 int nNames = getNumNames(seqNames);
86 if(nNames > maxRank) { maxRank = nNames; }
91 errorOut(e, "SharedListVector", "push_back");
96 /***********************************************************************/
98 void SharedListVector::resize(int size){
102 /***********************************************************************/
104 int SharedListVector::size(){
107 /***********************************************************************/
109 void SharedListVector::clear(){
117 /***********************************************************************/
119 void SharedListVector::print(ostream& output){
121 output << label << '\t' << numBins << '\t';
123 for(int i=0;i<data.size();i++){
125 output << data[i] << '\t';
130 catch(exception& e) {
131 errorOut(e, "SharedListVector", "print");
137 /***********************************************************************/
139 RAbundVector SharedListVector::getRAbundVector(){
143 for(int i=0;i<data.size();i++){
144 int binSize = getNumNames(data[i]);
145 rav.push_back(binSize);
148 // This was here before to output data in a nice format, but it screws up the name mapping steps
149 // sort(rav.rbegin(), rav.rend());
151 // for(int i=data.size()-1;i>=0;i--){
152 // if(rav.get(i) == 0){ rav.pop_back(); }
161 catch(exception& e) {
162 errorOut(e, "SharedListVector", "getRAbundVector");
167 /***********************************************************************/
169 SAbundVector SharedListVector::getSAbundVector(){
171 SAbundVector sav(maxRank+1);
173 for(int i=0;i<data.size();i++){
174 int binSize = getNumNames(data[i]);
175 sav.set(binSize, sav.get(binSize) + 1);
182 catch(exception& e) {
183 errorOut(e, "SharedListVector", "getSAbundVector");
188 /***********************************************************************/
189 SharedOrderVector* SharedListVector::getSharedOrderVector(){
191 string groupName, names, name;
193 SharedOrderVector* order = new SharedOrderVector();
194 order->setLabel(label);
196 for(int i=0;i<numBins;i++){
197 int binSize = getNumNames(get(i)); //find number of individual in given bin
199 while (names.find_first_of(',') != -1) {
200 name = names.substr(0,names.find_first_of(','));
201 names = names.substr(names.find_first_of(',')+1, names.length());
202 groupName = groupmap->getGroup(name);
203 order->push_back(i, binSize, groupName); //i represents what bin you are in
206 groupName = groupmap->getGroup(names);
207 order->push_back(i, binSize, groupName);
210 random_shuffle(order->begin(), order->end());
211 order->updateStats();
215 catch(exception& e) {
216 errorOut(e, "SharedListVector", "getSharedOrderVector");
220 /***********************************************************************/
221 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
223 SharedRAbundVector rav(data.size());
224 string group, names, name;
226 for(int i=0;i<numBins;i++){
228 while (names.find_first_of(',') != -1) {
229 name = names.substr(0,names.find_first_of(','));
230 names = names.substr(names.find_first_of(',')+1, names.length());
231 group = groupmap->getGroup(name);
232 if (group == groupName) { //this name is in the group you want the vector for.
233 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
238 groupName = groupmap->getGroup(names);
239 if (group == groupName) { //this name is in the group you want the vector for.
240 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
245 rav.setGroup(groupName);
250 catch(exception& e) {
251 errorOut(e, "SharedListVector", "getSharedRAbundVector");
255 /***********************************************************************/
256 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
259 util = new SharedUtil();
260 vector<SharedRAbundVector*> lookup;
261 map<string, SharedRAbundVector*> finder;
262 string group, names, name;
264 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
268 for (int i = 0; i < globaldata->Groups.size(); i++) {
269 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
270 finder[globaldata->Groups[i]] = temp;
271 finder[globaldata->Groups[i]]->setLabel(label);
272 finder[globaldata->Groups[i]]->setGroup(globaldata->Groups[i]);
273 //*temp = getSharedRAbundVector(globaldata->Groups[i]);
274 lookup.push_back(finder[globaldata->Groups[i]]);
278 for(int i=0;i<numBins;i++){
280 while (names.find_first_of(',') != -1) {
281 name = names.substr(0,names.find_first_of(','));
282 names = names.substr(names.find_first_of(',')+1, names.length());
283 group = groupmap->getGroup(name);
284 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
288 group = groupmap->getGroup(names);
289 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
295 catch(exception& e) {
296 errorOut(e, "SharedListVector", "getSharedRAbundVector");
301 /***********************************************************************/
302 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
304 SharedSAbundVector sav;
305 SharedRAbundVector rav;
307 rav = this->getSharedRAbundVector(groupName);
308 sav = rav.getSharedSAbundVector();
312 catch(exception& e) {
313 errorOut(e, "SharedListVector", "getSharedSAbundVector");
317 /***********************************************************************/
319 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
322 if(orderMap == NULL){
325 for(int i=0;i<data.size();i++){
326 int binSize = getNumNames(data[i]);
327 for(int j=0;j<binSize;j++){
331 random_shuffle(ov.begin(), ov.end());
339 OrderVector ov(numSeqs);
341 for(int i=0;i<data.size();i++){
342 string listOTU = data[i];
343 int length = listOTU.size();
347 for(int j=0;j<length;j++){
349 if(listOTU[j] != ','){
350 seqName += listOTU[j];
353 if(orderMap->count(seqName) == 0){
354 mothurOut(seqName + " not found, check *.names file\n");
358 ov.set((*orderMap)[seqName], i);
363 if(orderMap->count(seqName) == 0){
364 mothurOut(seqName + " not found, check *.names file\n");
367 ov.set((*orderMap)[seqName], i);
376 catch(exception& e) {
377 errorOut(e, "SharedListVector", "getOrderVector");
382 /***********************************************************************/