File: taxonomy.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 (209 lines) | stat: -rw-r--r-- 6,078 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
//
//  constaxonomy.cpp
//  Mothur
//
//  Created by Sarah Westcott on 1/13/20.
//  Copyright © 2020 Schloss Lab. All rights reserved.
//

#include "taxonomy.hpp"

/***********************************************************************/
Taxonomy::Taxonomy(){
    m = MothurOut::getInstance();
    containsConfidence = false;
}
/***********************************************************************/
Taxonomy::Taxonomy(string otuname, string consensusTax, int num) {
    try {
        m = MothurOut::getInstance();
        containsConfidence = false;
        
        name = otuname;
        numReps = num;
        taxonomy = parseTax(consensusTax);
        
    }
    catch(exception& e) {
        m->errorOut(e, "Taxonomy", "Taxonomy");
        exit(1);
    }
}
/***********************************************************************/
Taxonomy::Taxonomy(string otuname, string consensusTax) {
    try {
        m = MothurOut::getInstance();
        containsConfidence = false;
        
        name = otuname;
        numReps = 1;
        taxonomy = parseTax(consensusTax);
        
    }
    catch(exception& e) {
        m->errorOut(e, "Taxonomy", "Taxonomy");
        exit(1);
    }
}
/***********************************************************************/
Taxonomy::Taxonomy(ifstream& in) {
    try {
        m = MothurOut::getInstance();
        containsConfidence = false;
        
        string otu = ""; string consensusTax = "unknown";
        int size = 0;

        in >> otu; gobble(in);
        in >> size; gobble(in);
        consensusTax = util.getline(in); gobble(in);
        
        name = otu;
        numReps = size;
        taxonomy = parseTax(consensusTax);
        
    }
    catch(exception& e) {
        m->errorOut(e, "Taxonomy", "Taxonomy");
        exit(1);
    }
}
/***********************************************************************/
void Taxonomy::setTaxons(string consensusTax){
    try {
        
        taxonomy = parseTax(consensusTax);
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "setTaxons");
            exit(1);
    }
}
/***********************************************************************/
string Taxonomy::getInlineConsTaxonomy(){
    try {
        string otuConsensus = "";
        
        otuConsensus += name + '\t' + toString(numReps) + '\t' + getConsTaxString(true);
        
        return otuConsensus;
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "getInlineConsTaxonomy");
            exit(1);
    }
}
/***********************************************************************/
vector<string> Taxonomy::getSimpleTaxons(bool includeConfidence) { //pass in true to include confidences
    try {
        
        if (!containsConfidence) { includeConfidence = false; }
        vector<string> items;
        
        for (int i = 0; i < taxonomy.size(); i++) {
            if (m->getControl_pressed()) { break; }
            
            string conTax = taxonomy[i].name;
            
            if (includeConfidence) { conTax += "(" + toString(taxonomy[i].confidence) + ")"; }
            
            items.push_back(conTax);
        }
        
        return items;
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "getSimpleTaxons");
            exit(1);
    }
}
/***********************************************************************/
string Taxonomy::getConsTaxString(bool includeConfidence) { //pass in true to include confidences
    try {
        
        string conTax = "";
        if (!containsConfidence) { includeConfidence = false; }
        
        for (int i = 0; i < taxonomy.size(); i++) {
            if (m->getControl_pressed()) { break; }
            
            conTax += taxonomy[i].name;
            
            if (includeConfidence) { conTax += "(" + toString(taxonomy[i].confidence) + ")"; }
            
            conTax += ";";
        }
        
        return conTax;
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "getConsTaxString");
            exit(1);
    }
}
/***********************************************************************/
vector<Taxon> Taxonomy::parseTax(string tax){
    try {
        string taxon = "";
        vector<Taxon> consTaxs;
        
        for(int i=0;i<tax.length();i++){
            
            if (m->getControl_pressed()) { break; }
            
            if(tax[i] == ';'){
                
                string newtaxon = taxon; float confidence = 0;
                containsConfidence = util.hasConfidenceScore(newtaxon, confidence);
                
                Taxon thisTax(newtaxon, confidence);
                consTaxs.push_back(thisTax);
                
                taxon = "";
            }
            else{ taxon += tax[i]; }
        }
        
        return consTaxs;
       
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "parseTax");
            exit(1);
    }
}
/***********************************************************************/
void Taxonomy::printConsTax(ostream& out){
    try {
        
        out << getInlineConsTaxonomy() << endl;
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "printConsTax");
            exit(1);
    }
}
/***********************************************************************/
void Taxonomy::printConsTax(OutputWriter* out){
    try {
        
        out->write(getInlineConsTaxonomy()+"\n");
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "printConsTax");
            exit(1);
    }
}
/***********************************************************************/
void Taxonomy::printConsTaxNoConfidence(ostream& out){
    try {
        
        string otuConsensus = name + '\t' + toString(numReps) + '\t' + getConsTaxString(false);
        
        out << otuConsensus << endl;
        
    }catch(exception& e) {
            m->errorOut(e, "Taxonomy", "printConsTaxNoConfidence");
            exit(1);
    }
}
/***********************************************************************/