File: sharedrabundvector.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (314 lines) | stat: -rwxr-xr-x 9,346 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
//  sharedrabundvector.cpp
//  Mothur
//
//  Created by Sarah Westcott on 7/24/17.
//  Copyright © 2017 Schloss Lab. All rights reserved.
//

#include "sharedrabundvector.hpp"


/***********************************************************************/

SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0), group("") {}

/***********************************************************************/

SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), data(n,0) , maxRank(0), numBins(n), numSeqs(0), group("") {}

/***********************************************************************/

SharedRAbundVector::SharedRAbundVector(vector<int> rav) :  DataVector(), maxRank(0), numBins(rav.size()), numSeqs(0), group("")  {
    try {
        data.assign(numBins, 0);
        for(int i=0;i<rav.size();i++){ set(i, rav[i]); }
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
        exit(1);
    }
}

/***********************************************************************/

SharedRAbundVector::SharedRAbundVector(vector<int> rav, int mr, int nb, int ns) :  DataVector(), group(""){
    try {
        numBins = nb;
        maxRank = mr;
        numSeqs = ns;
        data = rav;
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
        exit(1);
    }
}
/***********************************************************************/


SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
    try {
        f >> label >> group >> numBins;
        
        data.assign(numBins, 0);
        int inputData;
        
        for(int i=0;i<numBins;i++){
            f >> inputData;
            set(i, inputData);
        }
        
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
        exit(1);
    }
}

/***********************************************************************/
SharedRAbundVector::SharedRAbundVector(ifstream& f, string l, string g, int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
    try {
        label = l;
        group = g;
        data.assign(numBins, 0);
        
        string otuCountsData = util.getline(f);
        
        vector<string> o = util.splitWhiteSpace(otuCountsData);
        
        if (o.size() != n) { m->mothurOut("[ERROR] : group " + group + " contains otu data for " + toString(o.size()) + " otus, but your sharedfile indicates you have " + toString(n) + " otus. Please correct.\n");  m->setControl_pressed(true);  }
        else {
            int inputData;
            for(int i=0;i<o.size();i++){
                util.mothurConvert(o[i], inputData);
                set(i, inputData);
            }
        }
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
        exit(1);
    }
}

/***********************************************************************/

SharedRAbundVector::~SharedRAbundVector() { }

/***********************************************************************/

void SharedRAbundVector::set(int binNumber, int newBinSize){
    try {
        int oldBinSize = data[binNumber];
        data[binNumber] = newBinSize;
        
        if(newBinSize > maxRank)	{	maxRank = newBinSize;	}
        
        numSeqs += (newBinSize - oldBinSize);
    }
    catch(exception& e) {
        m->errorOut(e, "RAbundVector", "set");
        exit(1);
    }
}
/***********************************************************************/

int SharedRAbundVector::increment(int binNumber){
    try {
        data[binNumber]++;
        int newBinSize = data[binNumber];
        
        if(newBinSize > maxRank)	{	maxRank = newBinSize;	}
        
        numSeqs++;
        
        return newBinSize;
    }
    catch(exception& e) {
        m->errorOut(e, "RAbundVector", "increment");
        exit(1);
    }
}

/***********************************************************************/

int SharedRAbundVector::get(int index){ return data[index]; }
/***********************************************************************/

void SharedRAbundVector::clear(){
    numBins = 0;
    maxRank = 0;
    numSeqs = 0;
    group = "";
    data.clear();
}
/***********************************************************************/

void SharedRAbundVector::push_back(int binSize){
    try {
        data.push_back(binSize);
        numBins++;
        
        if(binSize > maxRank){ maxRank = binSize; }
        
        numSeqs += binSize;
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "push_back");
        exit(1);
    }
}
/***********************************************************************/

int SharedRAbundVector::remove(int bin){
    try {
        int abund = data[bin];
        data.erase(data.begin()+bin);
        numBins--;
        
        if(abund == maxRank){
            vector<int>::iterator it = max_element(data.begin(), data.end());
            maxRank = *it;
        }
        
        numSeqs -= abund;
        
        return abund;
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "remove");
        exit(1);
    }
}
/***********************************************************************/

int SharedRAbundVector::remove(vector<int> bins){
    try {
        if (bins.size() == 0) { return 0; }
        
        int numRemoved = 0;
        vector<int> newData; int binIndex = 0;
        for (int i = 0; i < data.size(); i++) {
            if (m->getControl_pressed()) { break; }
            
            if (i != bins[binIndex]) {
                newData.push_back(data[i]);
            }else if (i == bins[binIndex]) {
                binIndex++;
                numRemoved += data[i];
                if (binIndex > bins.size()) { //removed all bins
                    newData.insert(newData.end(), data.begin()+i, data.end()); //add rest of good bins
                    break;
                }
            }
        }
        
        data = newData;
        numBins = data.size();
        
        vector<int>::iterator it = max_element(data.begin(), data.end());
        maxRank = *it;
        
        numSeqs -= numRemoved;
        
        return numRemoved;
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "remove");
        exit(1);
    }
}

/***********************************************************************/
void SharedRAbundVector::resize(int size){
    data.resize(size);
    
    vector<int>::iterator it = max_element(data.begin(), data.end());
    maxRank = *it;
    numSeqs = util.sum(data);
    numBins = size;
}
/***********************************************************************/
int SharedRAbundVector::size(){ return data.size(); }
/***********************************************************************/
void SharedRAbundVector::print(ostream& output){
    try {
        output << label;
        output << '\t' << group << '\t' << numBins;
        
        for(int i=0;i<numBins;i++){		output  << '\t' << int(data[i]);		}
        output << endl;
    }
    catch(exception& e) {
        m->errorOut(e, "SharedRAbundVector", "nonSortedPrint");
        exit(1);
    }
}
/***********************************************************************/
int SharedRAbundVector::getNumBins(){
    return numBins;
}

/***********************************************************************/

int SharedRAbundVector::getNumSeqs(){
    return numSeqs;
}

/***********************************************************************/

int SharedRAbundVector::getMaxRank(){
    return maxRank;
}
/***********************************************************************/

RAbundVector SharedRAbundVector::getRAbundVector(){
    RAbundVector rav;
    for(int i = 0; i < data.size(); i++) { if (data[i] != 0) {  rav.push_back(int(data[i])); } }
    rav.setLabel(label);
    return rav;
}
/***********************************************************************/

RAbundFloatVector SharedRAbundVector::getRAbundFloatVector(){
    RAbundFloatVector rav;
    for(int i = 0; i < data.size(); i++) { if (data[i] != 0) {  rav.push_back(float(data[i])); } }
    rav.setLabel(label);
    rav.setGroup(group);
    return rav;
}
/***********************************************************************/

SAbundVector SharedRAbundVector::getSAbundVector() {
    try {
        SAbundVector sav(int(maxRank+1));
        
        for(int i=0;i<data.size();i++){
            int abund = data[i];
            sav.set(abund, int(sav.get(abund)) + 1);
        }
        sav.set(0, 0);
        sav.setLabel(label);
        return sav;
    }
    catch(exception& e) {
        m->errorOut(e, "RAbundVector", "getSAbundVector");
        exit(1);
    }
}

/***********************************************************************/

OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = nullptr) {
    try {
    m->mothurOut("[ERROR]: can not convert SharedRAbundVectors to an ordervector, ordervectors assume no zero OTUS.\n"); m->setControl_pressed(true);
            OrderVector o; return o;
    }
    catch(exception& e) {
        m->errorOut(e, "RAbundVector", "getOrderVector");
        exit(1);
    }
}

/***********************************************************************/