File: biominfocommand.cpp

package info (click to toggle)
mothur 1.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 13,684 kB
  • sloc: cpp: 161,854; makefile: 122; sh: 31
file content (347 lines) | stat: -rw-r--r-- 18,157 bytes parent folder | download | duplicates (4)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
//  biominfocommand.cpp
//  Mothur
//
//  Created by Sarah Westcott on 8/5/15.
//  Copyright (c) 2015 Schloss Lab. All rights reserved.
//

#include "biominfocommand.h"

//**********************************************************************************************************************
vector<string> BiomInfoCommand::setParameters(){
    try {
        CommandParameter pbiom("biom", "InputTypes", "", "", "", "", "","",false,true, true); parameters.push_back(pbiom);
        CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
        CommandParameter prelabund("relabund", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(prelabund);
        CommandParameter pbasis("basis", "Multiple", "otu-sequence", "otu", "", "", "","",false,false); parameters.push_back(pbasis);
        CommandParameter pformat("format", "Multiple", "hdf5-simple", "hdf5", "", "", "","",false,false, true); parameters.push_back(pformat);
        CommandParameter pseed("seed", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pseed);
        CommandParameter poutput("output", "Multiple", "simple-detail", "detail", "", "", "","",false,false, true); parameters.push_back(poutput);
        CommandParameter pprintlevel("printlevel", "Number", "", "-1", "", "", "","",false,false); parameters.push_back(pprintlevel);
        CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
        CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
        
        abort = false; calledHelp = false; maxLevel = 0;
        
        vector<string> tempOutNames;
        outputTypes["taxonomy"] = tempOutNames;
        outputTypes["shared"] = tempOutNames;
        outputTypes["constaxonomy"] = tempOutNames;
        outputTypes["taxsummary"] = tempOutNames;
        
        vector<string> myArray;
        for (int i = 0; i < parameters.size(); i++) {	myArray.push_back(parameters[i].name);		}
        return myArray;
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "setParameters");
        exit(1);
    }
}
//**********************************************************************************************************************
string BiomInfoCommand::getHelpString(){
    try {
        string helpString = "";
        helpString += "The biom.info command reads a biom file creates a shared file. If your biom file contains metadata mothur will also create taxonomy or constaxonomy along with tax.summary files.\n";
        helpString += "The biom.info command parameters are " + getCommandParameters() + ". The biom parameter is required.\n";
        helpString += "The format parameter allows you indicate type of biom file you have. Options hdf5 or simple. Default is hdf5, unless you are running a version without HDF5 libraries.\n";
        helpString += "The label parameter allows you to enter a distance label to be used in the shared file created from your biom file.\n";
        helpString += "The relabund parameter allows you to indicate you want the tax.summary file values to be relative abundances rather than raw abundances. Default=F. \n";
        helpString += "The basis parameter allows you indicate what you want the summary file to represent, options are otu and sequence. Default is otu.\n";
        helpString += "The output parameter allows you to specify format of your summary file. Options are simple and detail. The default is detail.\n";
        helpString += "The printlevel parameter allows you to specify taxlevel of your summary file to print to. Options are 1 to the maz level in the file.  The default is -1, meaning max level.  If you select a level greater than the level your sequences classify to, mothur will print to the level your max level. \n";
        helpString += "For example consider the following basis=sequence could give Clostridiales 3 105, where 105 is the total number of sequences whose OTU classified to Clostridiales. ";
        helpString += "Now for basis=otu could give Clostridiales 3 7, where 7 is the number of OTUs that classified to Clostridiales.\n";
        helpString += "The biom.info command should be in the following format: biom.info(biom=test.biom, label=0.03).\n";
        
        getCommonQuestions();
        
        return helpString;
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "getHelpString");
        exit(1);
    }
}
//**********************************************************************************************************************
string BiomInfoCommand::getCommonQuestions(){
    try {
        vector<string> questions, issues, qanswers, ianswers, howtos, hanswers;
        
        string issue = "Cannot convert error. What do I do?"; issues.push_back(issue);
        string ianswer = "\tThis issue is caused by a matrix_element_type mismatch. The biom file contains a field called 'matrix_element_type'. This field tells mothur what form your observation data is in: int or float. Mothur expects 'int' (an interger value) because the shared file contains interger value abundance counts. If your file contains float values mothur will round down to the nearest integer value. But if your matrix_element_type=int and yet the file contains integer counts in float form, (ie. 31.0 instead of 31) you will get this error. You can resolve this issue by setting matrix_element_type=float in the biom file.\n"; ianswers.push_back(ianswer);
        
        issue = "Mothur can't read my biom file. What does this mean?"; issues.push_back(issue);
        ianswer = "\tMothur allows for 2 formats: classic (http://biom-format.org/documentation/format_versions/biom-1.0.html) and hdf5 (http://biom-format.org/documentation/format_versions/biom-2.0.html). NOTE: you can only process hdf5 files if you are using our pre-built version or have built your version of mothur with USEHDF5=yes.\n"; ianswers.push_back(ianswer);
        
        string commonQuestions = util.getFormattedHelp(questions, qanswers, issues, ianswers, howtos, hanswers);
        
        return commonQuestions;
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "getCommonQuestions");
        exit(1);
    }
}

//**********************************************************************************************************************
string BiomInfoCommand::getOutputPattern(string type) {
    try {
        string pattern = "";
        
        if (type == "shared") {  pattern = "[filename],[tag],shared"; }
        else if (type == "constaxonomy") {  pattern = "[filename],[tag],cons.taxonomy"; }
        else if (type == "taxonomy") {  pattern = "[filename],[tag],taxonomy"; }
        else if (type == "taxsummary") {  pattern = "[filename],[tag],[tag2],tax.summary"; } //tag2 = "" for taxonomy tag2 = cons for constaxonomy
        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->setControl_pressed(true);  }
        
        return pattern;
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "getOutputPattern");
        exit(1);
    }
}
//**********************************************************************************************************************
BiomInfoCommand::BiomInfoCommand(string option) : Command()  {
    try {
        maxLevel = 0;
        
        //allow user to run help
        if(option == "help") { help(); abort = true; calledHelp = true; }
        else if(option == "citation") { citation(); abort = true; calledHelp = true;}
        else if(option == "category") {  abort = true; calledHelp = true;  }
        
        else {
            OptionParser parser(option, setParameters());
            map<string, string> parameters = parser.getParameters();
            
            ValidParameters validParameter;
            //check for required parameters
            biomfile = validParameter.validFile(parameters, "biom");
            if (biomfile == "not open") { biomfile = ""; abort = true; }
            else if (biomfile == "not found") { biomfile = ""; m->mothurOut("[ERROR]: You must provide a biom file, please correct.\n");  abort = true;}
            else { current->setBiomFile(biomfile); }
            
            label = validParameter.valid(parameters, "label");
            if (label == "not found") { label = "userLabel"; }
            
            output = validParameter.valid(parameters, "output");		if(output == "not found"){	output = "detail"; }
            if ((output != "simple") && (output != "detail")) { m->mothurOut(output + " is not a valid output form. Options are simple and detail. I will use detail.\n");  output = "detail"; }
            
            string temp = validParameter.valid(parameters, "relabund");
            if (temp == "not found"){	temp = "false";			}
            else { temp = util.getSimpleName(temp); }
            relabund = util.isTrue(temp);
            
            temp = validParameter.valid(parameters, "printlevel");		if (temp == "not found"){	temp = "-1";		}
            util.mothurConvert(temp, printlevel);
            
            basis = validParameter.valid(parameters, "basis");
            if (basis == "not found") { basis = "otu"; }
            
            if ((basis != "otu") && (basis != "sequence")) { m->mothurOut("Invalid option for basis. basis options are otu and sequence, using otu.\n"); }
            
            format = validParameter.valid(parameters, "format");
            if (format == "not found") {
                #ifdef USE_HDF5
                if (!abort) {
                    if (util.isHDF5(biomfile)) { format = "hdf5"; }
                    else { format = "simple"; }
                }
                #else
                    format = "simple";
                #endif
            }
            
            if ((format != "hdf5") && (format != "simple")) {
                 m->mothurOut("Invalid option for format. format options are hdf5 and simple, quitting.\n"); abort = true;
            }
            
            if (format == "hdf5") {
                #ifdef USE_HDF5
                //do nothing we have the api
                #else
                    m->mothurOut("[ERROR]: To read HDF5 biom files, you must have the API installed, quitting.\n"); abort=true;
                #endif
            }
        }
        
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "BiomInfoCommand");
        exit(1);
    }
}
//**********************************************************************************************************************

int BiomInfoCommand::execute(){
    try {
        
        if (abort) { if (calledHelp) { return 0; }  return 2;	}
        
        long start = time(nullptr);
        
        Biom* biom;
        if (format == "hdf5")   { biom = new BiomHDF5(biomfile, label);   }
        else                   { biom = new BiomSimple(biomfile, label);   }
        
        //getting output filename
        string filename = biomfile;
        if (outputdir == "") { outputdir += util.hasPath(filename); }
        fileroot = outputdir + util.getRootName(util.getSimpleName(biomfile));
        
        SharedRAbundVectors* shared = biom->getSharedRAbundVectors();
        if (format == "hdf5")   {  label = shared->getLabel(); }
        if (label == "") { label = "userLabel"; shared->setLabels(label); }
        
        CountTable ct;
        vector< map<string, bool> > otuContainsGroups;
        
        if (shared != nullptr) {
            map<string, string> variables;
            variables["[filename]"] = fileroot;
            variables["[tag]"] = label;
            string sharedFilename = getOutputFileName("shared",variables);
            outputNames.push_back(sharedFilename); outputTypes["shared"].push_back(sharedFilename);
            bool printHeaders = true;
            
            ofstream out; util.openOutputFile(sharedFilename, out);
            shared->print(out, printHeaders);
            out.close();
            
            vector<string> groupNames = shared->getNamesGroups();
            for (int j = 0; j < groupNames.size(); j++) {  ct.addGroup(groupNames[j]); }
                
            int numBins = shared->getNumBins();
            
            for (int i = 0; i < numBins; i++) {
                int total = 0;
                map<string, bool> containsGroup;
                vector<int> abunds;
                for (int j = 0; j < shared->size(); j++) {
                    if (m->getControl_pressed()) { break; }
                    int abund = shared->get(i, groupNames[j]);
                    
                    total += abund;
                    containsGroup[groupNames[j]] = abund;
                    
                    if (basis == "otu") { if (abund > 0) { abund = 1;  } } //count presence in otu
                    abunds.push_back(abund);
                }
                ct.push_back(shared->getOTUName(i), abunds);
                otuContainsGroups.push_back(containsGroup);
            }
        }
        
        //print group taxonomies if given
        map<string, string> groupTaxonomies = biom->getGroupTaxonomies();
        
        if (groupTaxonomies.size() != 0) {
            //write taxonomy file
            map<string, string> variables;
            variables["[filename]"] = fileroot;
            variables["[tag]"] = label;
            string taxFilename = getOutputFileName("taxonomy",variables);
            outputNames.push_back(taxFilename); outputTypes["taxonomy"].push_back(taxFilename);
            ofstream outTax; util.openOutputFile(taxFilename, outTax);
            
            GroupMap* g = nullptr; PhyloSummary taxSum(g, relabund, printlevel);
            
            //print group taxonomy if given
            for (map<string, string>::iterator it = groupTaxonomies.begin(); it!= groupTaxonomies.end(); it++) {
                outTax << it->first << '\t' << it->second << endl;
                taxSum.addSeqToTree(it->first, it->second);
            }
            outTax.close();
            
            //write taxonomy file
            variables["[tag2]"] = "";
            string taxSumFilename = getOutputFileName("taxsummary",variables);
            outputNames.push_back(taxSumFilename); outputTypes["taxsummary"].push_back(taxSumFilename);
            ofstream outTaxSum; util.openOutputFile(taxSumFilename, outTaxSum);
                
            //write tax.summary
            if (relabund)   {   taxSum.print(outTaxSum, relabund);     }
            else            {   taxSum.print(outTaxSum, output);       }
                
            outTaxSum.close();
        }
        
        
        //print consTaxonomy if given
        vector<Taxonomy> consTax = biom->getConsTaxonomies();
        if (consTax.size() != 0) {

            //write taxonomy file
            map<string, string> variables;
            variables["[filename]"] = fileroot;
            variables["[tag]"] = label;
            string taxFilename = getOutputFileName("constaxonomy",variables);
            outputNames.push_back(taxFilename); outputTypes["constaxonomy"].push_back(taxFilename);
            ofstream outTax; util.openOutputFile(taxFilename, outTax);
            
            outTax << "OTU\tSize\tTaxonomy\n";
            
            PhyloSummary consTaxSum(&ct, relabund, printlevel);
            
            for (int i = 0; i < consTax.size(); i++) {
                
                consTax[i].printConsTax(outTax);
                
                if (basis == "sequence")    {  consTaxSum.addSeqToTree(consTax[i].getName(), consTax[i].getConsTaxString());  }
                else                       { consTaxSum.addSeqToTree(consTax[i].getConsTaxString(), otuContainsGroups[i]); } //add otu
            }
            outTax.close();
            
            variables["[tag2]"] = "cons";
            string taxSumFilename = getOutputFileName("taxsummary",variables);
            outputNames.push_back(taxSumFilename); outputTypes["taxsummary"].push_back(taxSumFilename);
            ofstream outTaxSum; util.openOutputFile(taxSumFilename, outTaxSum);
        
            //write tax.summary
            if (relabund)   {   consTaxSum.print(outTaxSum, relabund);     }
            else            {   consTaxSum.print(outTaxSum, output);       }
            
            outTaxSum.close();
        }
        
        delete biom;
        
        m->mothurOut("\nIt took " + toString(time(nullptr) - start) + " create mothur files from your biom file.\n\n");
        
        if (m->getControl_pressed()) { for (int i = 0; i < outputNames.size(); i++) { util.mothurRemove(outputNames[i]); } }
        
        string currentName = "";
        itTypes = outputTypes.find("shared");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setSharedFile(currentName); }
        }
        
        //set taxonomy file as new current taxonomyfile
        itTypes = outputTypes.find("taxonomy");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setTaxonomyFile(currentName); }
        }
        
        //set constaxonomy file as new current constaxonomyfile
        itTypes = outputTypes.find("constaxonomy");
        if (itTypes != outputTypes.end()) {
            if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setConsTaxonomyFile(currentName); }
        }
        
        m->mothurOutEndLine();
        m->mothurOut("Output File Names: \n"); 
        for (int i = 0; i < outputNames.size(); i++) {	m->mothurOut(outputNames[i]); m->mothurOutEndLine();	}
        m->mothurOutEndLine();
        
        return 0;
    }
    catch(exception& e) {
        m->errorOut(e, "BiomInfoCommand", "execute");
        exit(1);
    }
}
//**********************************************************************************************************************