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 //set up groupmap for later.
29 if (m->groupMode == "group") {
30 groupmap = new GroupMap(m->getGroupFile());
33 countTable = new CountTable();
34 countTable->readTable(m->getCountTableFile());
41 data.assign(hold, "");
43 for(int i=0;i<hold;i++){
50 m->errorOut(e, "SharedListVector", "SharedListVector");
55 /***********************************************************************/
56 void SharedListVector::set(int binNumber, string seqNames){
58 int nNames_old = m->getNumNames(data[binNumber]);
59 data[binNumber] = seqNames;
60 int nNames_new = m->getNumNames(seqNames);
62 if(nNames_old == 0) { numBins++; }
63 if(nNames_new == 0) { numBins--; }
64 if(nNames_new > maxRank) { maxRank = nNames_new; }
66 numSeqs += (nNames_new - nNames_old);
71 m->errorOut(e, "SharedListVector", "set");
76 /***********************************************************************/
78 string SharedListVector::get(int index){
82 /***********************************************************************/
84 void SharedListVector::push_back(string seqNames){
86 data.push_back(seqNames);
87 int nNames = m->getNumNames(seqNames);
91 if(nNames > maxRank) { maxRank = nNames; }
96 m->errorOut(e, "SharedListVector", "push_back");
101 /***********************************************************************/
103 void SharedListVector::resize(int size){
107 /***********************************************************************/
109 int SharedListVector::size(){
112 /***********************************************************************/
114 void SharedListVector::clear(){
122 /***********************************************************************/
124 void SharedListVector::print(ostream& output){
126 output << label << '\t' << numBins << '\t';
128 for(int i=0;i<data.size();i++){
130 output << data[i] << '\t';
135 catch(exception& e) {
136 m->errorOut(e, "SharedListVector", "print");
142 /***********************************************************************/
144 RAbundVector SharedListVector::getRAbundVector(){
148 for(int i=0;i<data.size();i++){
149 int binSize = m->getNumNames(data[i]);
150 rav.push_back(binSize);
153 // This was here before to output data in a nice format, but it screws up the name mapping steps
154 // sort(rav.rbegin(), rav.rend());
156 // for(int i=data.size()-1;i>=0;i--){
157 // if(rav.get(i) == 0){ rav.pop_back(); }
166 catch(exception& e) {
167 m->errorOut(e, "SharedListVector", "getRAbundVector");
172 /***********************************************************************/
174 SAbundVector SharedListVector::getSAbundVector(){
176 SAbundVector sav(maxRank+1);
178 for(int i=0;i<data.size();i++){
179 int binSize = m->getNumNames(data[i]);
180 sav.set(binSize, sav.get(binSize) + 1);
187 catch(exception& e) {
188 m->errorOut(e, "SharedListVector", "getSAbundVector");
193 /***********************************************************************/
194 SharedOrderVector* SharedListVector::getSharedOrderVector(){
196 SharedOrderVector* order = new SharedOrderVector();
197 order->setLabel(label);
199 for(int i=0;i<numBins;i++){
200 int binSize = m->getNumNames(get(i)); //find number of individual in given bin
201 string names = get(i);
202 vector<string> binNames;
203 m->splitAtComma(names, binNames);
204 if (m->groupMode != "group") {
206 for (int j = 0; j < binNames.size(); j++) { binSize += countTable->getNumSeqs(binNames[i]); }
208 for (int j = 0; j < binNames.size(); j++) {
209 if (m->control_pressed) { return order; }
210 if (m->groupMode == "group") {
211 string groupName = groupmap->getGroup(binNames[i]);
212 if(groupName == "not found") { m->mothurOut("Error: Sequence '" + binNames[i] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
214 order->push_back(i, binSize, groupName); //i represents what bin you are in
216 vector<int> groupAbundances = countTable->getGroupCounts(binNames[i]);
217 vector<string> groupNames = countTable->getNamesOfGroups();
218 for (int k = 0; k < groupAbundances.size(); k++) { //groupAbundances.size() == 0 if there is a file mismatch and m->control_pressed is true.
219 if (m->control_pressed) { return order; }
220 for (int l = 0; l < groupAbundances[k]; l++) { order->push_back(i, binSize, groupNames[k]); }
226 random_shuffle(order->begin(), order->end());
227 order->updateStats();
231 catch(exception& e) {
232 m->errorOut(e, "SharedListVector", "getSharedOrderVector");
236 /***********************************************************************/
237 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
239 SharedRAbundVector rav(data.size());
241 for(int i=0;i<numBins;i++){
242 string names = get(i);
243 vector<string> binNames;
244 m->splitAtComma(names, binNames);
245 for (int j = 0; j < binNames.size(); j++) {
246 if (m->control_pressed) { return rav; }
247 if (m->groupMode == "group") {
248 string group = groupmap->getGroup(binNames[j]);
249 if(group == "not found") { m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
250 if (group == groupName) { //this name is in the group you want the vector for.
251 rav.set(i, rav.getAbundance(i) + 1, group); //i represents what bin you are in
254 int count = countTable->getGroupCount(binNames[j], groupName);
255 rav.set(i, rav.getAbundance(i) + count, groupName);
261 rav.setGroup(groupName);
266 catch(exception& e) {
267 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
271 /***********************************************************************/
272 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
275 util = new SharedUtil();
276 vector<SharedRAbundVector*> lookup; //contains just the groups the user selected
277 vector<SharedRAbundVector*> lookupDelete;
278 map<string, SharedRAbundVector*> finder; //contains all groups in groupmap
280 vector<string> Groups = m->getGroups();
281 vector<string> allGroups;
282 if (m->groupMode == "group") { allGroups = groupmap->getNamesOfGroups(); }
283 else { allGroups = countTable->getNamesOfGroups(); }
284 util->setGroups(Groups, allGroups);
285 m->setGroups(Groups);
288 for (int i = 0; i < allGroups.size(); i++) {
289 SharedRAbundVector* temp = new SharedRAbundVector(data.size());
290 finder[allGroups[i]] = temp;
291 finder[allGroups[i]]->setLabel(label);
292 finder[allGroups[i]]->setGroup(allGroups[i]);
293 if (m->inUsersGroups(allGroups[i], m->getGroups())) { //if this group is in user groups
294 lookup.push_back(finder[allGroups[i]]);
296 lookupDelete.push_back(finder[allGroups[i]]);
301 for(int i=0;i<numBins;i++){
302 string names = get(i);
303 vector<string> binNames;
304 m->splitAtComma(names, binNames);
305 for (int j = 0; j < binNames.size(); j++) {
306 if (m->groupMode == "group") {
307 string group = groupmap->getGroup(binNames[j]);
308 if(group == "not found") { m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); }
309 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in
311 vector<int> counts = countTable->getGroupCounts(binNames[j]);
312 for (int k = 0; k < allGroups.size(); k++) {
313 finder[allGroups[k]]->set(i, finder[allGroups[k]]->getAbundance(i) + counts[k], allGroups[k]);
319 for (int j = 0; j < lookupDelete.size(); j++) { delete lookupDelete[j]; }
323 catch(exception& e) {
324 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
329 /***********************************************************************/
330 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
332 SharedSAbundVector sav;
333 SharedRAbundVector rav;
335 rav = this->getSharedRAbundVector(groupName);
336 sav = rav.getSharedSAbundVector();
340 catch(exception& e) {
341 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
345 /***********************************************************************/
347 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
350 if(orderMap == NULL){
353 for(int i=0;i<data.size();i++){
354 string names = data[i];
355 vector<string> binNames;
356 m->splitAtComma(names, binNames);
357 int binSize = binNames.size();
358 if (m->groupMode != "group") {
360 for (int j = 0; j < binNames.size(); j++) { binSize += countTable->getNumSeqs(binNames[i]); }
362 for(int j=0;j<binSize;j++){
366 random_shuffle(ov.begin(), ov.end());
374 OrderVector ov(numSeqs);
376 for(int i=0;i<data.size();i++){
377 string listOTU = data[i];
378 vector<string> binNames;
379 m->splitAtComma(listOTU, binNames);
380 for (int j = 0; j < binNames.size(); j++) {
381 if(orderMap->count(binNames[j]) == 0){
382 m->mothurOut(binNames[j] + " not found, check *.names file\n");
385 ov.set((*orderMap)[binNames[j]], i);
395 catch(exception& e) {
396 m->errorOut(e, "SharedListVector", "getOrderVector");
401 /***********************************************************************/