5 * Created by Pat Schloss on 8/8/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
11 #include "sabundvector.hpp"
12 #include "rabundvector.hpp"
13 #include "ordervector.hpp"
14 #include "listvector.hpp"
16 //sorts highest to lowest
17 /***********************************************************************/
18 inline bool abundNamesSort(string left, string right){
23 for(int i=0;i<left.size();i++){ if(left[i] == ','){ countLeft++; } }
29 for(int i=0;i<right.size();i++){ if(right[i] == ','){ countRight++; } }
32 if (countLeft > countRight) {
38 /***********************************************************************/
40 ListVector::ListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){}
42 /***********************************************************************/
44 ListVector::ListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){}
46 /***********************************************************************/
48 ListVector::ListVector(string id, vector<string> lv) : DataVector(id), data(lv){
50 for(int i=0;i<data.size();i++){
52 int binSize = m->getNumNames(data[i]);
54 if(binSize > maxRank) { maxRank = binSize; }
60 m->errorOut(e, "ListVector", "ListVector");
65 /**********************************************************************/
67 ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
72 data.assign(hold, "");
73 string inputData = "";
75 for(int i=0;i<hold;i++){
82 m->errorOut(e, "ListVector", "ListVector");
87 /***********************************************************************/
89 void ListVector::set(int binNumber, string seqNames){
91 int nNames_old = m->getNumNames(data[binNumber]);
92 data[binNumber] = seqNames;
93 int nNames_new = m->getNumNames(seqNames);
95 if(nNames_old == 0) { numBins++; }
96 if(nNames_new == 0) { numBins--; }
97 if(nNames_new > maxRank) { maxRank = nNames_new; }
99 numSeqs += (nNames_new - nNames_old);
101 catch(exception& e) {
102 m->errorOut(e, "ListVector", "set");
107 /***********************************************************************/
109 string ListVector::get(int index){
113 /***********************************************************************/
115 void ListVector::push_back(string seqNames){
117 data.push_back(seqNames);
118 int nNames = m->getNumNames(seqNames);
122 if(nNames > maxRank) { maxRank = nNames; }
126 catch(exception& e) {
127 m->errorOut(e, "ListVector", "push_back");
132 /***********************************************************************/
134 void ListVector::resize(int size){
138 /***********************************************************************/
140 int ListVector::size(){
143 /***********************************************************************/
145 void ListVector::clear(){
153 /***********************************************************************/
155 void ListVector::print(ostream& output){
157 output << label << '\t' << numBins << '\t';
159 vector<string> hold = data;
160 sort(hold.begin(), hold.end(), abundNamesSort);
162 for(int i=0;i<hold.size();i++){
164 output << hold[i] << '\t';
169 catch(exception& e) {
170 m->errorOut(e, "ListVector", "print");
176 /***********************************************************************/
178 RAbundVector ListVector::getRAbundVector(){
182 for(int i=0;i<data.size();i++){
183 int binSize = m->getNumNames(data[i]);
184 rav.push_back(binSize);
187 // This was here before to output data in a nice format, but it screws up the name mapping steps
188 // sort(rav.rbegin(), rav.rend());
190 // for(int i=data.size()-1;i>=0;i--){
191 // if(rav.get(i) == 0){ rav.pop_back(); }
200 catch(exception& e) {
201 m->errorOut(e, "ListVector", "getRAbundVector");
206 /***********************************************************************/
208 SAbundVector ListVector::getSAbundVector(){
210 SAbundVector sav(maxRank+1);
212 for(int i=0;i<data.size();i++){
213 int binSize = m->getNumNames(data[i]);
214 sav.set(binSize, sav.get(binSize) + 1);
221 catch(exception& e) {
222 m->errorOut(e, "ListVector", "getSAbundVector");
227 /***********************************************************************/
229 OrderVector ListVector::getOrderVector(map<string,int>* orderMap = NULL){
232 if(orderMap == NULL){
235 for(int i=0;i<data.size();i++){
236 int binSize = m->getNumNames(data[i]);
237 for(int j=0;j<binSize;j++){
241 random_shuffle(ov.begin(), ov.end());
249 OrderVector ov(numSeqs);
251 for(int i=0;i<data.size();i++){
252 string listOTU = data[i];
253 int length = listOTU.size();
257 for(int j=0;j<length;j++){
259 if(listOTU[j] != ','){
260 seqName += listOTU[j];
263 if(orderMap->count(seqName) == 0){
264 m->mothurOut(seqName + " not found, check *.names file\n");
268 ov.set((*orderMap)[seqName], i);
273 if(orderMap->count(seqName) == 0){
274 m->mothurOut(seqName + " not found, check *.names file\n");
277 ov.set((*orderMap)[seqName], i);
286 catch(exception& e) {
287 m->errorOut(e, "ListVector", "getOrderVector");
292 /***********************************************************************/