buddy_resources - mendessoares/BuddySuite GitHub Wiki

The buddy_resources.py module is home to classes and functions that are shared among all the main BuddySuite modules, as well as the flags dictionaries used to populate argparse.

##Classes #####class Contributor __init__(first, last, commits=None, github=None)

Attribute Description
first (str) First name
last (str) Last name
commits (int) Total number of contributions to BuddySuite
github (str) URL to GitHub profile page
Method Description
_str_() Recast to a string: Prints name, number of commits, and GitHub URL

#####class CustomHelpFormatter(argparse.RawDescriptionHelpFormatter) This is an extension of the RawDescriptionHelpFormatter class that tidies up the argparse help output. The following code snippet illustrates how to use it:

import argparse

def fmt(prog):
    return br.CustomHelpFormatter(prog)

parser = argparse.ArgumentParser(prog="PhyloBuddy.py", formatter_class=fmt, add_help=False, usage=argparse.SUPPRESS,
                                 description='''\
\033[1mPhyloBuddy\033[m
Put a little bonsai into your phylogeny.

\033[1mUsage examples\033[m:
PhyloBuddy.py "/path/to/tree_file" -<cmd>
PhyloBuddy.py "/path/to/tree_file" -<cmd> | PhyloBuddy.py -<cmd>
PhyloBuddy.py "(A,(B,C));" -f "raw" -<cmd>
''')

br.flags(parser, ("trees", "Supply file path(s) or raw tree string, If piping trees into PhyloBuddy "
                           "this argument can be left blank."), br.pb_flags, br.pb_modifiers, VERSION)

#####class Usage This class keeps track of usage statistics and sends reports when the user is participating in the software improvement program

__init__()

Attribute Description
config (dict) Collection of configuration values parsed from config.ini ('install_path', 'email', 'diagnostics', 'user_hash')
stats (dict) Collection of usage statistics
usage_file_path (str) System path to the actual usage log file
Method Description
clear_stats() Re-initiate self.stats to {"user_hash": self.config["user_hash"]}
increment(buddy, version, tool) Record a buddy tool usage to self.stats
save(send_report=True) Either upload usage stats to the server or save back to usage log file
send_report() Compile a usage report and upload to the server via ftp

#####class Version Stores version information about a given tool with some pretty-print functionality

__init__(name, major, minor, _contributors, release_date=None)

Attribute Description
name (str)
major (int) Major releases may contain significant changes in behavior from previous releases
minor (int or str) Minor releases are for bug fixes, and should not cause changes to 'expected' behavior. Valid values include 'alpha' and 'beta' for pre-release versions, or an integer once the major version has been released
_contributors (list) Items are Contributor objects
release_date (datatime.datetime or None) When this particular version was released to the public (if at all)
Method Description
contributor_string() Returns a string with all contributors neatly organized by their number of commits
short() Returns a string with the version number (e.g., 2.1)
_str_() Return a full version blurb, including license and contributors

##Functions #####def config_values() Reads in the .buddysuite/config.ini and returns key parameters (install_path, email, diagnostics, and user_hash)

#####def error_report(error_msg) Sends an error report to the server via FTP. The message itself must be fed in as an argument, and it should be informative. The send_traceback() function calls error_report(), and should be used when possible.

error_msg = "Program crashed due to ValueError..."
send_error(error_msg)

#####def flags(parser, _positional=None, _flags=None, _modifier=None, version=None) Directly modifies an argparse parser by converting dictionaries of attributes (positional, flags, and modifiers) into argument groups. The 'version' parameter accepts a Version object.

See CustomHelpFormatter for a usage example.

#####def send_traceback(tool, e) Prepares a clean traceback, stripped of personally identifiable information, to be uploaded to the server using error_report() if the user agrees. tool: A string, indicating which BuddySuite tool caused the crash e: The error object caught

##Flags dictionaries All new tools added to the BuddySuite must have a record added to the appropriate flags dictionary. The full tool name is used as the key, and another dictionary of argparse options is provided as the value.

Example:

sb_flags = {"add_feature": {"flag": "af",
                            "nargs": "*",
                            "help": "Add a feature (annotation) to selected sequences Args: <name>, <location (start1-end1,start2-end2...)>, <strand (+|-)>, <qualifiers (foo=bar,hello=world...)>, <regex_pattern>"},
            "ave_seq_length": {"flag": "asl",
                               "action": "append",
                               "nargs": "?",
                               "help": "Return the average length of all sequences. Pass in the word 'clean' to remove gaps etc from the sequences before counting."},
            "back_transcribe": {"flag": "r2d",
                                "action": "store_true",
                                "help": "Convert RNA sequences to DNA"}, ... }

Each module also has an associated modifiers dictionary, but these are very special flags and there needs to be a really compelling reason to add any new ones.

sb_modifiers = {"alpha": {"flag": "a",
                          "action": "store",
                          "help": "If you want the file read with a specific alphabet"},
                "in_format": {"flag": "f",
                              "action": "store",
                              "help": "If SeqBuddy can't guess the file format, just specify it directly"},
                "in_place": {"flag": "i",
                             "action": "store_true",
                             "help": "Rewrite the input file in-place. Be careful!"}, ... }
⚠️ **GitHub.com Fallback** ⚠️