Best Python code snippet using lemoncheesecake
extract_pathway_product_annotations.py
Source:extract_pathway_product_annotations.py  
1#!/usr/bin/env python2import os3import argparse4import sys5def get_options():6    parser = argparse.ArgumentParser(description="Script to pull pathway-names, pathway-common-names, rxn-common-name, "7                                                 "num-reactions, num-covered-reactions, orf-count, orf, taxonomy, and "8                                                 "RPKM values for all sequences from various MetaPathways outputs.")9    parser.add_argument("-i", "--hierarchy", required=True,10                        help="The hierarchy text file for mapping components to classes")11    parser.add_argument("-o", "--output", required=True,12                        help="The output file containing mapped values from the hierarchy")13    parser.add_argument("-l", "--comp_list", required=False, default=None,14                        help="An optional list of hierarchy components to return information on")15    parser.add_argument("--pathways", required=False, default=False, action="store_true",16                        help="Flag to indicate the output should be at pathway level [DEFAULT = reaction-level]")17    parser.add_argument("-p", "--pathway_annotations", required=True,18                        help="The output from extract_pathway_table_from_pgdb.pl")19    args = parser.parse_args()20    if not os.path.isfile(args.hierarchy):21        raise IOError("ERROR: " + args.hierarchy + " doesn't exist!")22    return args23def check_inputs(args):24    """25    Checks to make sure the paths and files exist and if the individual files are provided,26    then the metapathways directory is not provided27    :param args: command-line arguments parsed by argparse28    :return: nothing29    """30    # TODO: check input files31    return32def round_up_mp_inputs(args):33    args.sample_id = args.mp_inputs.rstrip(os.sep).split(os.sep)[-1]34    pwy_txt = args.mp_inputs + os.sep + "results" + os.sep + "pgdb" + os.sep + args.sample_id + ".pwy.txt"35    if not os.path.isfile(pwy_txt):36        raise IOError("Unable to find pathway annotations file (pwy.txt) in MetaPathway directory!\n"37                      "Looked for " + pwy_txt + " but file doesn't exist.")38    ptools_input = args.mp_inputs + os.sep + "ptools" + os.sep + "0.pf"39    if not os.path.isfile(ptools_input):40        raise IOError("Unable to find PGDB input file (0.pf) in MetaPathway directory!\n"41                      "Looked for " + ptools_input + " but file doesn't exist.")42    args.pathway_annotations = pwy_txt43    args.pgdb_input = ptools_input44    return args45def get_depth(fields):46    d = 047    while fields[d] == "":48        d += 149    return d50def read_hierarchy(hierarchy):51    prev_depth = -152    path = list()53    paths_dict = dict()54    for line in hierarchy:55        fields = line.rstrip().split('\t')56        curr_depth = get_depth(fields)57        if curr_depth > prev_depth:58            path.append(fields[curr_depth])59        if prev_depth >= curr_depth:60            if path[0] not in paths_dict.keys():61                paths_dict[path[0]] = list()62            paths_dict[path[0]].append(path[1:])63            path = path[0:curr_depth]64            path.append(fields[curr_depth])65            # path.append("--".join(fields[curr_depth:]))66        prev_depth = curr_depth67    paths_dict[path[0]].append(path[1:])68    return paths_dict69def write_long_hierarchy(hierarchy_paths, output):70    for super_class in hierarchy_paths:71        for reaction_path in hierarchy_paths[super_class]:72            output.write(super_class + "\t" + "\t".join(reaction_path) + "\n")73    return74def prune_reactions(hierarchy_paths):75    pathways_dict = dict()76    for super_class in hierarchy_paths:77        if super_class not in pathways_dict.keys():78            pathways_dict[super_class] = list()79        for reaction_path in hierarchy_paths[super_class]:80            pathway_path = reaction_path[0:-1]81            if pathway_path not in pathways_dict[super_class]:82                pathways_dict[super_class].append(pathway_path)83    return pathways_dict84def is_annotated(fields, metacyc=False):85    # orf, contig = fields[0:2]86    annotations = fields[2:]87    for entry in annotations:88        if entry != "" and entry != "\n":89            if metacyc and fields[-1] != "\n":90                return True91            else:92                return False93def get_process_paths(comp_list, hierarchy_paths):94    # Read the list, orf annotations and pathway annotations95    components = list()96    with open(comp_list) as components_list:97        for comp in components_list:98            # print comp99            if "\t" in comp:100                code, common = comp.strip().split("\t")101            else:102                code = comp.strip()103                common = ""104            components.append(code)105    # Map the processes in components to the hierarchy106    process_hierarchy_map = dict()107    for process in components:108        process_hierarchy_map[process] = list()109        for sub_class in hierarchy_paths.values():110            for path in sub_class:111                if process in path:112                    start = path.index(process)113                    process_hierarchy_map[process].append(path[start+1:])114    # Remove processes with no entry in the hierarchy115    no_hierarchy = list()116    for process in process_hierarchy_map:117        if len(process_hierarchy_map[process]) == 0:118            print "No entries found for", process, "in hierarchy"119            no_hierarchy.append(process)120    for process in no_hierarchy:121        process_hierarchy_map.pop(process)122    return process_hierarchy_map123def write_process_annotations(orf_annotations, output, num_processes):124    output.write("Process\tSample\tPathway\tPathway Common Name\tNumber of reactions"125                 "\tNumber covered\tReaction\tORF name\n")126    annotations_found = 0127    for process in orf_annotations.keys():128        if len(orf_annotations[process]) == 0:129            print "No entries found for", process, "in annotations"130        elif len(orf_annotations[process]) > 0:131            annotations_found += 1132            previous_annotation = ""133            process_annotations = sorted(orf_annotations[process])134            for annotation in process_annotations:135                if annotation != previous_annotation:136                    output.write(process + "\t" + "\t".join(annotation) + "\n")137                previous_annotation = annotation138    print annotations_found, "/", num_processes, "processes mapped to annotations"139    return140def search_dict(query_one, query_two, dictionary):141    """142    Matches a word with a key in the dictionary143    :param word: A pathway name144    :param dictionary: A collection of process names (which are anything in the metacyc hierarchy) and all145    processes associated with it from the hierarchy146    :return: a list packing the process and path associated with word or nothing147    """148    process_annotations = list()149    for process, path in dictionary.items():150        for pairs in path:151            if query_two in pairs:152                process_annotations.append([process, pairs[0]])153            if query_one in pairs:154                process_annotations.append([process, path[0]])155    if len(process_annotations) == 0:156        return False157    else:158        return process_annotations159def read_pwy_txt(pathway_annotations, process_hierarchy_map):160    orf_annotations = dict()161    with open(pathway_annotations) as pwy_txt:162        line = pwy_txt.readline()163        while line:164            fields = line.split("\t")165            if not fields[0] == "SAMPLE":166                # Match the pathway name with the process of interest, if possible167                process_annotations = search_dict(fields[1], fields[3], process_hierarchy_map)168                if process_annotations:169                    for path in process_annotations:170                        process = path[0]171                        if process not in orf_annotations.keys():172                            orf_annotations[process] = list()173                        orf = fields[-1].strip()174                        sample = fields[0]175                        n_reactions = fields[5]176                        n_covered = fields[6]177                        pwy_name = fields[1]178                        pwy_common = fields[2]179                        rxn_name = fields[3]180                        orf_annotations[process].append([sample, pwy_name, pwy_common,181                                                         n_reactions, n_covered, rxn_name, orf])182            line = pwy_txt.readline()183    # Add the missing processes to the orf_annotations with empty lists184    for process in process_hierarchy_map:185        if process not in orf_annotations.keys():186            orf_annotations[process] = list()187    return orf_annotations188def main():189    args = get_options()190    check_inputs(args)191    hierarchy = open(args.hierarchy, 'r')192    hierarchy_paths = read_hierarchy(hierarchy)193    hierarchy.close()194    if args.pathways:195        hierarchy_paths = prune_reactions(hierarchy_paths)196    try:197        output = open(args.output, 'w')198    except:199        raise IOError("ERROR: cannot make " + args.output + "!")200    if args.comp_list is None:201        write_long_hierarchy(hierarchy_paths, output)202    else:203        process_hierarchy_map = get_process_paths(args.comp_list, hierarchy_paths)204        orf_annotations = read_pwy_txt(args.pathway_annotations, process_hierarchy_map)205        write_process_annotations(orf_annotations, output, len(process_hierarchy_map.keys()))206    output.close()...get_user_hierarchy_group.py
Source:get_user_hierarchy_group.py  
...56    def hierarchy_group_id(self) -> str:57        return pulumi.get(self, "hierarchy_group_id")58    @property59    @pulumi.getter(name="hierarchyPaths")60    def hierarchy_paths(self) -> Sequence['outputs.GetUserHierarchyGroupHierarchyPathResult']:61        """62        A block that contains information about the levels in the hierarchy group. The `hierarchy_path` block is documented below.63        """64        return pulumi.get(self, "hierarchy_paths")65    @property66    @pulumi.getter67    def id(self) -> str:68        """69        The provider-assigned unique ID for this managed resource.70        """71        return pulumi.get(self, "id")72    @property73    @pulumi.getter(name="instanceId")74    def instance_id(self) -> str:...script_create_excel_TT.py
Source:script_create_excel_TT.py  
1import json2import xlsxwriter3BASE_URL = 'https://datasets.freesound.org'4workbook = xlsxwriter.Workbook('name.xlsx')5worksheet = workbook.add_worksheet('Hyperlinks')6url_format = workbook.add_format({7    'font_color': 'blue',8    'underline':  19})10id_url = json.load(open('id_url.json'))11ontology = json.load(open('ontology/ontology.json', 'rb'))12ontology_by_id = {o['id']:o for o in ontology}13hierarchy_paths = json.load(open('json/hierarchy_paths.json', 'rb'))14categories = ['/m/02jz0l',15 '/m/0l14qv',16 '/m/07rv9rh',17 '/m/0xzly',18 '/m/03qc9zr',19 '/m/0l14_3',20 '/m/0jb2l',21 '/m/0130jx',22 '/m/06rvn',23 '/m/0261r1',24 '/m/0dwsp',25 '/m/02dgv',26 '/t/dd00037',27 '/m/03dnzn',28 '/m/07st88b',29 '/m/081rb',30 '/m/0bm0k',31 '/m/03v3yw',32 '/m/0l15bq',33 '/m/04s8yn',34 '/m/0mkg',35 '/m/01yrx',36 '/m/01p970',37 '/m/07pn_8q',38 '/m/04zjc',39 '/m/02g901',40 '/m/02zsn',41 '/m/01jnbd',42 '/m/02_nn',43 '/t/dd00031',44 '/m/0242l',45 '/t/dd00129',46 '/m/07pggtn',47 '/m/0dwtp',48 '/m/07pjwq1',49 '/m/0ytgt',50 '/t/dd00005',51 '/m/04brg2',52 '/m/06_fw',53 '/m/0fx9l',54 '/m/05jcn',55 '/m/04_sv',56 '/m/02yds9',57 '/m/03k3r',58 '/m/04cvmfc',59 '/m/07qn4z3',60 '/m/03w41f',61 '/t/dd00033',62 '/m/028ght',63 '/m/07plz5l',64 '/m/012xff',65 '/m/03vt0',66 '/m/03wvsk',67 '/t/dd00013',68 '/m/025_jnm',69 '/m/0lyf6',70 '/m/05_wcq',71 '/m/07ppn3j',72 '/m/0fqfqc',73 '/m/015lz1',74 '/m/02hnl',75 '/m/07pbtc8',76 '/m/020bb7',77 '/m/0bm02',78 '/m/01j423',79 '/m/01gp74',80 '/m/0838f',81 '/m/01lsmm',82 '/m/09xqv',83 '/t/dd00003',84 '/m/0zmy2j9',85 '/m/07s0dtb',86 '/m/01g90h',87 '/m/0642b4',88 '/m/09x0r',89 '/t/dd00128',90 '/m/0ltv',91 '/t/dd00018',92 '/m/02rtxlg',93 '/t/dd00134',94 '/m/016622',95 '/t/dd00126',96 '/m/04szw',97 '/m/03qtq',98 '/m/02pjr4',99 '/t/dd00032',100 '/m/01b_21',101 '/m/09l8g',102 '/m/01j3sz',103 '/m/02_41',104 '/m/068zj',105 '/m/05r5c',106 '/m/09b5t',107 '/m/0463cq4',108 '/m/026t6',109 '/m/06h7j',110 '/m/0btp2',111 '/m/0239kh',112 '/m/0l14md',113 '/m/0dv5r',114 '/m/07rn7sz',115 '/m/07qn5dc',116 '/m/011k_j',117 '/m/01h8n0',118 '/m/0g6b5',119 '/m/07brj',120 '/m/023pjk',121 '/m/01dwxx',122 '/m/0ghcn6',123 '/m/0k65p',124 '/t/dd00135',125 '/m/07rpkh9',126 '/m/053hz1',127 '/m/01jwx6',128 '/m/07r_80w',129 '/m/03qtwd',130 '/m/0939n_',131 '/m/0dwt5',132 '/m/01d3sd',133 '/m/09d5_',134 '/m/07pp_mv',135 '/m/0dl9sf8',136 '/m/0brhx',137 '/m/0c2wf',138 '/m/05rj2',139 '/m/07rkbfh',140 '/m/01jg02',141 '/m/0l14jd',142 '/m/015p6',143 '/m/0d31p',144 '/m/0mbct',145 '/m/06bxc',146 '/m/018w8',147 '/t/dd00001',148 '/m/01jt3m',149 '/m/021wwz',150 '/m/0d8_n',151 '/m/0llzx',152 '/m/0316dw',153 '/t/dd00130',154 '/m/0l14gg',155 '/m/03cczk',156 '/m/07sr1lc',157 '/m/03q5_w',158 '/t/dd00002',159 '/m/01xq0k1',160 '/m/05zppz',161 '/m/03qjg',162 '/m/09ld4',163 '/m/01j4z9',164 '/m/07rjwbb',165 '/m/02k_mr',166 '/m/02bk07',167 '/t/dd00112',168 '/t/dd00127',169 '/m/07qrkrw',170 '/m/0jbk',171 '/m/042v_gx',172 '/t/dd00004',173 '/m/0dxrf',174 '/m/05mxj0q',175 '/m/01hsr_',176 '/m/01b9nn',177 '/m/07p6fty',178 '/m/05r5wn',179 '/m/01z5f',180 '/m/01m2v',181 '/m/02bm9n',182 '/m/0ngt1',183 '/m/05tny_',184 '/m/0j45pbj',185 '/m/07qf0zm',186 '/m/07s0s5r',187 '/m/0bt9lr',188 '/t/dd00071',189 '/m/01s0vc',190 '/m/0cfdd']191categories_with_paths = [(node_id, ' > '.join([ontology_by_id[n]['name'] for n in hierarchy_paths[node_id][0]])) for node_id in categories]192coordinates = ['A'+str(1+x) for x in range(len(categories))]193for idx, category in enumerate(categories_with_paths):194    worksheet.write_url(coordinates[idx], BASE_URL+id_url[category[0]].replace('contribute', 'annotate'), url_format, category[1])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
