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; countTable = NULL; }
21 /***********************************************************************/
23 SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){ groupmap = NULL; countTable = NULL; }
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
28 groupmap = NULL; countTable = NULL;
29 //set up groupmap for later.
30 if (m->groupMode == "group") {
31 groupmap = new GroupMap(m->getGroupFile());
34 countTable = new CountTable();
35 countTable->readTable(m->getCountTableFile(), true);
42 data.assign(hold, "");
44 for(int i=0;i<hold;i++){
51 m->errorOut(e, "SharedListVector", "SharedListVector");
56 /***********************************************************************/
57 void SharedListVector::set(int binNumber, string seqNames){
59 int nNames_old = m->getNumNames(data[binNumber]);
60 data[binNumber] = seqNames;
61 int nNames_new = m->getNumNames(seqNames);
63 if(nNames_old == 0) { numBins++; }
64 if(nNames_new == 0) { numBins--; }
65 if(nNames_new > maxRank) { maxRank = nNames_new; }
67 numSeqs += (nNames_new - nNames_old);
72 m->errorOut(e, "SharedListVector", "set");
77 /***********************************************************************/
79 string SharedListVector::get(int index){
83 /***********************************************************************/
85 void SharedListVector::push_back(string seqNames){
87 data.push_back(seqNames);
88 int nNames = m->getNumNames(seqNames);
92 if(nNames > maxRank) { maxRank = nNames; }
97 m->errorOut(e, "SharedListVector", "push_back");
102 /***********************************************************************/
104 void SharedListVector::resize(int size){
108 /***********************************************************************/
110 int SharedListVector::size(){
113 /***********************************************************************/
115 void SharedListVector::clear(){
123 /***********************************************************************/
125 void SharedListVector::print(ostream& output){
127 output << label << '\t' << numBins << '\t';
129 for(int i=0;i<data.size();i++){
131 output << data[i] << '\t';
136 catch(exception& e) {
137 m->errorOut(e, "SharedListVector", "print");
143 /***********************************************************************/
145 RAbundVector SharedListVector::getRAbundVector(){
149 for(int i=0;i<data.size();i++){
150 int binSize = m->getNumNames(data[i]);
151 rav.push_back(binSize);
154 // This was here before to output data in a nice format, but it screws up the name mapping steps
155 // sort(rav.rbegin(), rav.rend());
157 // for(int i=data.size()-1;i>=0;i--){
158 // if(rav.get(i) == 0){ rav.pop_back(); }
167 catch(exception& e) {
168 m->errorOut(e, "SharedListVector", "getRAbundVector");
173 /***********************************************************************/
175 SAbundVector SharedListVector::getSAbundVector(){
177 SAbundVector sav(maxRank+1);
179 for(int i=0;i<data.size();i++){
180 int binSize = m->getNumNames(data[i]);
181 sav.set(binSize, sav.get(binSize) + 1);
188 catch(exception& e) {
189 m->errorOut(e, "SharedListVector", "getSAbundVector");
194 /***********************************************************************/
195 SharedOrderVector* SharedListVector::getSharedOrderVector(){
197 SharedOrderVector* order = new SharedOrderVector();
198 order->setLabel(label);
200 for(int i=0;i<numBins;i++){
201 int binSize = m->getNumNames(get(i)); //find number of individual in given bin
202 string names = get(i);
203 vector<string> binNames;
204 m->splitAtComma(names, binNames);
205 if (m->groupMode != "group") {
207 for (int j = 0; j < binNames.size(); j++) { binSize += countTable->getNumSeqs(binNames[i]); }
209 for (int j = 0; j < binNames.size(); j++) {
210 if (m->control_pressed) { return order; }
211 if (m->groupMode == "group") {
212 string groupName = groupmap->getGroup(binNames[i]);
213 if(groupName == "not found") { m->mothurOut("Error: Sequence '" + binNames[i] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
215 order->push_back(i, binSize, groupName); //i represents what bin you are in
217 vector<int> groupAbundances = countTable->getGroupCounts(binNames[i]);
218 vector<string> groupNames = countTable->getNamesOfGroups();
219 for (int k = 0; k < groupAbundances.size(); k++) { //groupAbundances.size() == 0 if there is a file mismatch and m->control_pressed is true.
220 if (m->control_pressed) { return order; }
221 for (int l = 0; l < groupAbundances[k]; l++) { order->push_back(i, binSize, groupNames[k]); }
227 random_shuffle(order->begin(), order->end());
228 order->updateStats();
232 catch(exception& e) {
233 m->errorOut(e, "SharedListVector", "getSharedOrderVector");
237 /***********************************************************************/
238 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
240 SharedRAbundVector rav(data.size());
242 for(int i=0;i<numBins;i++){
243 string names = get(i);
244 vector<string> binNames;
245 m->splitAtComma(names, binNames);
246 for (int j = 0; j < binNames.size(); j++) {
247 if (m->control_pressed) { return rav; }
248 if (m->groupMode == "group") {
249 string group = groupmap->getGroup(binNames[j]);
250 if(group == "not found") { m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
251 if (group == groupName) { //this name is in the group you want the vector for.
252 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
255 int count = countTable->getGroupCount(binNames[j], groupName);
256 rav.set(i, rav.getAbundance(i) + count, groupName);
262 rav.setGroup(groupName);
267 catch(exception& e) {
268 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
272 /***********************************************************************/
273 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
276 util = new SharedUtil();
277 vector<SharedRAbundVector*> lookup; //contains just the groups the user selected
278 vector<SharedRAbundVector*> lookupDelete;
279 map<string, SharedRAbundVector*> finder; //contains all groups in groupmap
281 vector<string> Groups = m->getGroups();
282 vector<string> allGroups;
283 if (m->groupMode == "group") { allGroups = groupmap->getNamesOfGroups(); }
284 else { allGroups = countTable->getNamesOfGroups(); }
285 util->setGroups(Groups, allGroups);
286 m->setGroups(Groups);
289 for (int i = 0; i < allGroups.size(); i++) {
290 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
291 finder[allGroups[i]] = temp;
292 finder[allGroups[i]]->setLabel(label);
293 finder[allGroups[i]]->setGroup(allGroups[i]);
294 if (m->inUsersGroups(allGroups[i], m->getGroups())) { //if this group is in user groups
295 lookup.push_back(finder[allGroups[i]]);
297 lookupDelete.push_back(finder[allGroups[i]]);
302 for(int i=0;i<numBins;i++){
303 string names = get(i);
304 vector<string> binNames;
305 m->splitAtComma(names, binNames);
306 for (int j = 0; j < binNames.size(); j++) {
307 if (m->groupMode == "group") {
308 string group = groupmap->getGroup(binNames[j]);
309 if(group == "not found") { m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
310 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
312 vector<int> counts = countTable->getGroupCounts(binNames[j]);
313 for (int k = 0; k < allGroups.size(); k++) {
314 finder[allGroups[k]]->set(i, finder[allGroups[k]]->getAbundance(i) + counts[k], allGroups[k]);
320 for (int j = 0; j < lookupDelete.size(); j++) { delete lookupDelete[j]; }
324 catch(exception& e) {
325 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
330 /***********************************************************************/
331 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
333 SharedSAbundVector sav;
334 SharedRAbundVector rav;
336 rav = this->getSharedRAbundVector(groupName);
337 sav = rav.getSharedSAbundVector();
341 catch(exception& e) {
342 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
346 /***********************************************************************/
348 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
351 if(orderMap == NULL){
354 for(int i=0;i<data.size();i++){
355 string names = data[i];
356 vector<string> binNames;
357 m->splitAtComma(names, binNames);
358 int binSize = binNames.size();
359 if (m->groupMode != "group") {
361 for (int j = 0; j < binNames.size(); j++) { binSize += countTable->getNumSeqs(binNames[i]); }
363 for(int j=0;j<binSize;j++){
367 random_shuffle(ov.begin(), ov.end());
375 OrderVector ov(numSeqs);
377 for(int i=0;i<data.size();i++){
378 string listOTU = data[i];
379 vector<string> binNames;
380 m->splitAtComma(listOTU, binNames);
381 for (int j = 0; j < binNames.size(); j++) {
382 if(orderMap->count(binNames[j]) == 0){
383 m->mothurOut(binNames[j] + " not found, check *.names file\n");
386 ov.set((*orderMap)[binNames[j]], i);
396 catch(exception& e) {
397 m->errorOut(e, "SharedListVector", "getOrderVector");
402 /***********************************************************************/