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);
204 if(groupName == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
206 order->push_back(i, binSize, groupName); //i represents what bin you are in
209 groupName = groupmap->getGroup(names);
210 if(groupName == "not found") { mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
211 order->push_back(i, binSize, groupName);
214 random_shuffle(order->begin(), order->end());
215 order->updateStats();
219 catch(exception& e) {
220 errorOut(e, "SharedListVector", "getSharedOrderVector");
224 /***********************************************************************/
225 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
227 SharedRAbundVector rav(data.size());
228 string group, names, name;
230 for(int i=0;i<numBins;i++){
232 while (names.find_first_of(',') != -1) {
233 name = names.substr(0,names.find_first_of(','));
234 names = names.substr(names.find_first_of(',')+1, names.length());
235 group = groupmap->getGroup(name);
236 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
237 if (group == groupName) { //this name is in the group you want the vector for.
238 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
243 groupName = groupmap->getGroup(names);
244 if(groupName == "not found") { mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
245 if (group == groupName) { //this name is in the group you want the vector for.
246 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
251 rav.setGroup(groupName);
256 catch(exception& e) {
257 errorOut(e, "SharedListVector", "getSharedRAbundVector");
261 /***********************************************************************/
262 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
265 util = new SharedUtil();
266 vector<SharedRAbundVector*> lookup;
267 map<string, SharedRAbundVector*> finder;
268 string group, names, name;
270 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
274 for (int i = 0; i < globaldata->Groups.size(); i++) {
275 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
276 finder[globaldata->Groups[i]] = temp;
277 finder[globaldata->Groups[i]]->setLabel(label);
278 finder[globaldata->Groups[i]]->setGroup(globaldata->Groups[i]);
279 //*temp = getSharedRAbundVector(globaldata->Groups[i]);
280 lookup.push_back(finder[globaldata->Groups[i]]);
284 for(int i=0;i<numBins;i++){
286 while (names.find_first_of(',') != -1) {
287 name = names.substr(0,names.find_first_of(','));
288 names = names.substr(names.find_first_of(',')+1, names.length());
289 group = groupmap->getGroup(name);
290 if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
291 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
295 group = groupmap->getGroup(names);
296 if(group == "not found") { mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); }
297 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
303 catch(exception& e) {
304 errorOut(e, "SharedListVector", "getSharedRAbundVector");
309 /***********************************************************************/
310 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
312 SharedSAbundVector sav;
313 SharedRAbundVector rav;
315 rav = this->getSharedRAbundVector(groupName);
316 sav = rav.getSharedSAbundVector();
320 catch(exception& e) {
321 errorOut(e, "SharedListVector", "getSharedSAbundVector");
325 /***********************************************************************/
327 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
330 if(orderMap == NULL){
333 for(int i=0;i<data.size();i++){
334 int binSize = getNumNames(data[i]);
335 for(int j=0;j<binSize;j++){
339 random_shuffle(ov.begin(), ov.end());
347 OrderVector ov(numSeqs);
349 for(int i=0;i<data.size();i++){
350 string listOTU = data[i];
351 int length = listOTU.size();
355 for(int j=0;j<length;j++){
357 if(listOTU[j] != ','){
358 seqName += listOTU[j];
361 if(orderMap->count(seqName) == 0){
362 mothurOut(seqName + " not found, check *.names file\n");
366 ov.set((*orderMap)[seqName], i);
371 if(orderMap->count(seqName) == 0){
372 mothurOut(seqName + " not found, check *.names file\n");
375 ov.set((*orderMap)[seqName], i);
384 catch(exception& e) {
385 errorOut(e, "SharedListVector", "getOrderVector");
390 /***********************************************************************/