azmfaridee / mothur

This is GSoC2012 fork of 'Mothur'. We are trying to implement a number of 'Feature Selection' algorithms for microbial ecology data and incorporate them into mother's main codebase.
https://github.com/mothur/mothur
GNU General Public License v3.0
3 stars 1 forks source link

Find Mothur's Common Practices #5

Closed azmfaridee closed 12 years ago

azmfaridee commented 12 years ago

Parent Issue: #4

Find the command practices in mothur, they are such as but not limited to:

mothur-westcott commented 12 years ago

Error catching:

The goal is every function in mothur will have a try/catch in it. The catch passes an error message to the logger to let us know where the error occurred and the type of error.

Example:

try{
}catch(exception& e) {
    m->errorOut(e, "ClassName", "functionName");
    exit(1);
}

Signal Capture:

The only signal mothur captures currently is Control C. Normally if the user pressed Control C the program would be terminated, but we want to use it to quit a command. At regular key places in your code you should check for this and if it has been pressed cleanup memory, remove output files and quit the command. If you are in a subclass, return up to the command level cleaning up as you go. The mothurOut class has a listener to check for the signal, so you just need to ask mothurOut if its been pressed. Here's an example from cooccurrencecommand.cpp line 188.

if (m->control_pressed) {
    for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
    delete input; 
    out.close(); 
    m->mothurRemove(outputFileName); 
    return 0;
 }

Logging functions:

The mothurOut class handles the logging. You should never output using bout. The key logging functions are:

void mothurOut(string); //writes to cout and the logfile
void mothurOutEndLine(); //writes to cout and the logfile
void mothurOut(string, ofstream&); //writes to the ofstream, cout and the logfile
void mothurOutEndLine(ofstream&); //writes to the ofstream, cout and the logfile
void mothurOutJustToLog(string);

The mothurOut class also has many useful functions that should be used whenever possible to reduce code duplication. The most frequently used function include: openInputFile, openOutputFile, setGroups, getGroups, currentBinLabels, mothurConvert, mothurRemove and gobble.