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){ groupmap = NULL; }
21 /***********************************************************************/
23 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){ groupmap = NULL; }
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
28 //set up groupmap for later.
29 groupmap = new GroupMap(m->getGroupFile());
36 data.assign(hold, "");
38 for(int i=0;i<hold;i++){
45 m->errorOut(e, "SharedListVector", "SharedListVector");
50 /***********************************************************************/
51 void SharedListVector::set(int binNumber, string seqNames){
53 int nNames_old = m->getNumNames(data[binNumber]);
54 data[binNumber] = seqNames;
55 int nNames_new = m->getNumNames(seqNames);
57 if(nNames_old == 0) { numBins++; }
58 if(nNames_new == 0) { numBins--; }
59 if(nNames_new > maxRank) { maxRank = nNames_new; }
61 numSeqs += (nNames_new - nNames_old);
66 m->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 = m->getNumNames(seqNames);
86 if(nNames > maxRank) { maxRank = nNames; }
91 m->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 m->errorOut(e, "SharedListVector", "print");
137 /***********************************************************************/
139 RAbundVector SharedListVector::getRAbundVector(){
143 for(int i=0;i<data.size();i++){
144 int binSize = m->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 m->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 = m->getNumNames(data[i]);
175 sav.set(binSize, sav.get(binSize) + 1);
182 catch(exception& e) {
183 m->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 = m->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") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->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") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->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 m->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") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->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") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->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 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
261 /***********************************************************************/
262 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
265 util = new SharedUtil();
266 vector<SharedRAbundVector*> lookup; //contains just the groups the user selected
267 map<string, SharedRAbundVector*> finder; //contains all groups in groupmap
268 string group, names, name;
270 vector<string> Groups = m->getGroups();
271 vector<string> allGroups = groupmap->getNamesOfGroups();
272 util->setGroups(Groups, allGroups);
273 m->setGroups(Groups);
276 for (int i = 0; i < allGroups.size(); i++) {
277 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
278 finder[allGroups[i]] = temp;
279 finder[allGroups[i]]->setLabel(label);
280 finder[allGroups[i]]->setGroup(allGroups[i]);
281 if (m->inUsersGroups(allGroups[i], m->getGroups())) { //if this group is in user groups
282 lookup.push_back(finder[allGroups[i]]);
287 for(int i=0;i<numBins;i++){
289 int nameLength = names.size();
292 for(int j=0;j<nameLength;j++){
294 group = groupmap->getGroup(seqName);
295 if(group == "not found") { m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
296 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
304 group = groupmap->getGroup(seqName);
305 if(group == "not found") { m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
306 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
310 // while (names.find_first_of(',') != -1) {
311 // name = names.substr(0,names.find_first_of(','));
312 // names = names.substr(names.find_first_of(',')+1, names.length());
313 // group = groupmap->getGroup(name);
314 // if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
315 // finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
319 // group = groupmap->getGroup(names);
320 // if(group == "not found") { m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
321 // finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
327 catch(exception& e) {
328 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
333 /***********************************************************************/
334 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
336 SharedSAbundVector sav;
337 SharedRAbundVector rav;
339 rav = this->getSharedRAbundVector(groupName);
340 sav = rav.getSharedSAbundVector();
344 catch(exception& e) {
345 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
349 /***********************************************************************/
351 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
354 if(orderMap == NULL){
357 for(int i=0;i<data.size();i++){
358 int binSize = m->getNumNames(data[i]);
359 for(int j=0;j<binSize;j++){
363 random_shuffle(ov.begin(), ov.end());
371 OrderVector ov(numSeqs);
373 for(int i=0;i<data.size();i++){
374 string listOTU = data[i];
375 int length = listOTU.size();
379 for(int j=0;j<length;j++){
381 if(listOTU[j] != ','){
382 seqName += listOTU[j];
385 if(orderMap->count(seqName) == 0){
386 m->mothurOut(seqName + " not found, check *.names file\n");
390 ov.set((*orderMap)[seqName], i);
395 if(orderMap->count(seqName) == 0){
396 m->mothurOut(seqName + " not found, check *.names file\n");
399 ov.set((*orderMap)[seqName], i);
408 catch(exception& e) {
409 m->errorOut(e, "SharedListVector", "getOrderVector");
414 /***********************************************************************/