@@ -0,0 +1,3064 @@
+#!/usr/bin/env python3%0Aimport sys%0A%0Aassert sys.version_info.major == 3, %22the script requires Python 3%22%0A%0A%0A__author__ = %22Juan Miguel Cejuela (@juanmirocks)%22%0A%0A__help__ = %22%22%22  Simple parse a GO ontology .obo file.%0A%0A                This python file can be used 1) as a script to filter and extract only a desired GO hierarchy, and%0A                2) as a library to parse the parent-children relasionships of the ontology (%60is_a%60 and %60part_of%60).%0A%0A                Note: as a script, the GO Terms are printed to standard output. Redirect to a file if needed.%0A%0A                Note: obsolete terms (%60is_obsolete%60) are written and parsed for backwards-compatibility%0A           %22%22%22%0A%0A%0Adef parse_arguments(argv=%5B%5D):%0A    import argparse%0A%0A    parser = argparse.ArgumentParser(description=__help__)%0A%0A    parser.add_argument('go_file', help='GO ontology file in .obo format, e.g., http://purl.obolibrary.org/obo/go/go-basic.obo')%0A    parser.add_argument('--hierarchy', required=False, default='', choices=%5B'biological_process', 'molecular_function', 'cellular_component'%5D)%0A%0A    args = parser.parse_args(argv)%0A%0A    args.namespace = 'namespace: '+args.hierarchy%0A%0A    return args%0A%0A%0Adef simple_parse(go_file='', args=None, print_out=False, create_dictionary=True):%0A    import re%0A%0A    args = args if args else parse_arguments(%5Bgo_file%5D)%0A%0A    print_out = (lambda line: print(line, end='')) if print_out else (lambda _: None)%0A%0A    dictionary = %7B%7D if create_dictionary else None%0A%0A    state = 'no_term'%0A    go_id = None%0A    regex_go_id = re.compile('GO:%5B0-9%5D+')%0A%0A    with open(args.go_file) as f:%0A        for line in f:%0A            if line.startswith('%5BTerm%5D'):%0A                term_token = line%0A                go_id_line = next(f)%0A                go_id = regex_go_id.search(go_id_line).group()%0A                name_line = next(f)%0A                namespace_line = next(f)%0A%0A                if namespace_line.startswith(args.namespace):%0A                    state = 'print'%0A                    print_out(term_token)%0A                    print_out(go_id_line)%0A                    print_out(name_line)%0A                    print_out(namespace_line)%0A                else:%0A                    state = 'no_print'%0A%0A            elif line == f.newlines:%0A                if state == 'print':%0A                    if create_dictionary and go_id not in dictionary:%0A                            dictionary%5Bgo_id%5D = %5B%5D%0A                    print_out(line)%0A%0A                state = 'no_term'%0A%0A            elif state == 'print':%0A                print_out(line)%0A%0A                if create_dictionary and (line.startswith('is_a: ') or line.startswith('relationship: part_of')):%0A                    parent = regex_go_id.search(line).group()%0A                    parents = dictionary.get(go_id, %5B%5D)%0A                    parents = %5B*parents, parent%5D%0A                    dictionary%5Bgo_id%5D = parents%0A%0A            else:%0A                continue%0A%0A    return dictionary%0A%0A%0Aif __name__ == %22__main__%22:%0A    import sys%0A    args = parse_arguments(sys.argv%5B1:%5D)%0A    simple_parse(args=args, print_out=True, create_dictionary=False)%0A

add parsing of GO .obo file (originally from https://github.com/Rostlab/) — just copied here so far for simplicity (we may change this in a future)