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 m->errorOut(e, "SharedListVector", "SharedListVector");
52 /***********************************************************************/
53 void SharedListVector::set(int binNumber, string seqNames){
55 int nNames_old = m->getNumNames(data[binNumber]);
56 data[binNumber] = seqNames;
57 int nNames_new = m->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);
68 m->errorOut(e, "SharedListVector", "set");
73 /***********************************************************************/
75 string SharedListVector::get(int index){
79 /***********************************************************************/
81 void SharedListVector::push_back(string seqNames){
83 data.push_back(seqNames);
84 int nNames = m->getNumNames(seqNames);
88 if(nNames > maxRank) { maxRank = nNames; }
93 m->errorOut(e, "SharedListVector", "push_back");
98 /***********************************************************************/
100 void SharedListVector::resize(int size){
104 /***********************************************************************/
106 int SharedListVector::size(){
109 /***********************************************************************/
111 void SharedListVector::clear(){
119 /***********************************************************************/
121 void SharedListVector::print(ostream& output){
123 output << label << '\t' << numBins << '\t';
125 for(int i=0;i<data.size();i++){
127 output << data[i] << '\t';
132 catch(exception& e) {
133 m->errorOut(e, "SharedListVector", "print");
139 /***********************************************************************/
141 RAbundVector SharedListVector::getRAbundVector(){
145 for(int i=0;i<data.size();i++){
146 int binSize = m->getNumNames(data[i]);
147 rav.push_back(binSize);
150 // This was here before to output data in a nice format, but it screws up the name mapping steps
151 // sort(rav.rbegin(), rav.rend());
153 // for(int i=data.size()-1;i>=0;i--){
154 // if(rav.get(i) == 0){ rav.pop_back(); }
163 catch(exception& e) {
164 m->errorOut(e, "SharedListVector", "getRAbundVector");
169 /***********************************************************************/
171 SAbundVector SharedListVector::getSAbundVector(){
173 SAbundVector sav(maxRank+1);
175 for(int i=0;i<data.size();i++){
176 int binSize = m->getNumNames(data[i]);
177 sav.set(binSize, sav.get(binSize) + 1);
184 catch(exception& e) {
185 m->errorOut(e, "SharedListVector", "getSAbundVector");
190 /***********************************************************************/
191 SharedOrderVector* SharedListVector::getSharedOrderVector(){
193 string groupName, names, name;
195 SharedOrderVector* order = new SharedOrderVector();
196 order->setLabel(label);
198 for(int i=0;i<numBins;i++){
199 int binSize = m->getNumNames(get(i)); //find number of individual in given bin
201 while (names.find_first_of(',') != -1) {
202 name = names.substr(0,names.find_first_of(','));
203 names = names.substr(names.find_first_of(',')+1, names.length());
204 groupName = groupmap->getGroup(name);
206 if(groupName == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
208 order->push_back(i, binSize, groupName); //i represents what bin you are in
211 groupName = groupmap->getGroup(names);
212 if(groupName == "not found") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
213 order->push_back(i, binSize, groupName);
216 random_shuffle(order->begin(), order->end());
217 order->updateStats();
221 catch(exception& e) {
222 m->errorOut(e, "SharedListVector", "getSharedOrderVector");
226 /***********************************************************************/
227 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
229 SharedRAbundVector rav(data.size());
230 string group, names, name;
232 for(int i=0;i<numBins;i++){
234 while (names.find_first_of(',') != -1) {
235 name = names.substr(0,names.find_first_of(','));
236 names = names.substr(names.find_first_of(',')+1, names.length());
237 group = groupmap->getGroup(name);
238 if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
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 groupName = groupmap->getGroup(names);
246 if(groupName == "not found") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
247 if (group == groupName) { //this name is in the group you want the vector for.
248 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
253 rav.setGroup(groupName);
258 catch(exception& e) {
259 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
263 /***********************************************************************/
264 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
267 util = new SharedUtil();
268 vector<SharedRAbundVector*> lookup; //contains just the groups the user selected
269 map<string, SharedRAbundVector*> finder; //contains all groups in groupmap
270 string group, names, name;
272 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
275 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
276 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
277 finder[globaldata->gGroupmap->namesOfGroups[i]] = temp;
278 finder[globaldata->gGroupmap->namesOfGroups[i]]->setLabel(label);
279 finder[globaldata->gGroupmap->namesOfGroups[i]]->setGroup(globaldata->gGroupmap->namesOfGroups[i]);
280 if (m->inUsersGroups(globaldata->gGroupmap->namesOfGroups[i], globaldata->Groups)) { //if this group is in user groups
281 lookup.push_back(finder[globaldata->gGroupmap->namesOfGroups[i]]);
286 for(int i=0;i<numBins;i++){
288 int nameLength = names.size();
291 for(int j=0;j<nameLength;j++){
293 group = groupmap->getGroup(seqName);
294 if(group == "not found") { m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
295 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
303 group = groupmap->getGroup(seqName);
304 if(group == "not found") { m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
305 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
309 // while (names.find_first_of(',') != -1) {
310 // name = names.substr(0,names.find_first_of(','));
311 // names = names.substr(names.find_first_of(',')+1, names.length());
312 // group = groupmap->getGroup(name);
313 // if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
314 // finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
318 // group = groupmap->getGroup(names);
319 // if(group == "not found") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
320 // finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
326 catch(exception& e) {
327 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
332 /***********************************************************************/
333 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
335 SharedSAbundVector sav;
336 SharedRAbundVector rav;
338 rav = this->getSharedRAbundVector(groupName);
339 sav = rav.getSharedSAbundVector();
343 catch(exception& e) {
344 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
348 /***********************************************************************/
350 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
353 if(orderMap == NULL){
356 for(int i=0;i<data.size();i++){
357 int binSize = m->getNumNames(data[i]);
358 for(int j=0;j<binSize;j++){
362 random_shuffle(ov.begin(), ov.end());
370 OrderVector ov(numSeqs);
372 for(int i=0;i<data.size();i++){
373 string listOTU = data[i];
374 int length = listOTU.size();
378 for(int j=0;j<length;j++){
380 if(listOTU[j] != ','){
381 seqName += listOTU[j];
384 if(orderMap->count(seqName) == 0){
385 m->mothurOut(seqName + " not found, check *.names file\n");
389 ov.set((*orderMap)[seqName], i);
394 if(orderMap->count(seqName) == 0){
395 m->mothurOut(seqName + " not found, check *.names file\n");
398 ov.set((*orderMap)[seqName], i);
407 catch(exception& e) {
408 m->errorOut(e, "SharedListVector", "getOrderVector");
413 /***********************************************************************/