|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +#!/usr/bin/env/ python3 |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import pythoncyc |
| 8 | +import argparse |
| 9 | +import subprocess |
| 10 | +from pythoncyc import PTools as PTools |
| 11 | +from pythoncyc.PTools import PToolsError as PToolsError |
| 12 | +from pythoncyc.PTools import PythonCycError as PythonCycError |
| 13 | + |
| 14 | +#Usage : python3 dev_pythoncyc.py -p META |
| 15 | + |
| 16 | + |
| 17 | +def close_pgdb_wosaving(pgdb): |
| 18 | + try: |
| 19 | + r = PTools.sendQueryToPTools('(close-kb :kb (kb-of-organism "'+pgdb._orgid+'") :save-updates-p nil)') |
| 20 | + except PToolsError as msg: |
| 21 | + raise PythonCycError('Pathway Tools was unable to close KB of organism (orgid) {orgid}. More specifically: {msg}'.format(orgid=pgdb._orgid, msg=msg)) |
| 22 | + return(r) |
| 23 | + |
| 24 | + |
| 25 | +def get_genes_of_reaction(rxn, pgdb): |
| 26 | + genes = set() |
| 27 | + for gene in pgdb.genes_of_reaction(rxn): |
| 28 | + genes.add(gene) |
| 29 | + for enz in pgdb.enzymes_of_reaction(rxn): |
| 30 | + for gene in pgdb.genes_of_protein(enz): |
| 31 | + genes.add(gene) |
| 32 | + return(genes) |
| 33 | + |
| 34 | + |
| 35 | +def write_pathway(pgdb): |
| 36 | + with open("metacyc_pathways.tsv", "w") as pathways: |
| 37 | + with open("metacyc_reactions_by_pathway.tsv", "w") as reactions: |
| 38 | + header1="Pathway_Id\tCommon_name\n" |
| 39 | + pathways.write(header1) |
| 40 | + header2="Pathway_Id\tReaction_Id\tSpontaneous\tOrphan\tOrphan_in_Metacyc\n" |
| 41 | + reactions.write(header2) |
| 42 | + for path in pgdb.all_pathways(selector='all', base=True): |
| 43 | + to_write=path.split("|")[1]+"\t"+pgdb[path].common_name+"\n" |
| 44 | + pathways.write(to_write) |
| 45 | + for rxn in pgdb[path].reaction_list: |
| 46 | + orphan_in_metacyc = "FALSE" |
| 47 | + orphan = "NA" |
| 48 | + spontaneous = "FALSE" |
| 49 | + if pgdb[rxn].spontaneous_p != None and pgdb[rxn].spontaneous_p == True: |
| 50 | + spontaneous = "TRUE" |
| 51 | + if len(get_genes_of_reaction(rxn, pgdb)) == 0 and spontaneous == "FALSE": |
| 52 | + orphan_in_metacyc = "TRUE" |
| 53 | + if pgdb[rxn].orphan_p != None: |
| 54 | + if pgdb[rxn].orphan_p[0] == "|NO|": |
| 55 | + orphan = "FALSE" |
| 56 | + else: |
| 57 | + orphan = "TRUE" |
| 58 | + to_write_r = path.split("|")[1]+"\t"+rxn.split("|")[1]+"\t"+spontaneous+"\t"+orphan+"\t"+orphan_in_metacyc+"\n" |
| 59 | + reactions.write(to_write_r) |
| 60 | + |
| 61 | +def get_pathways_none_spontaneous_reactions(pgdb): |
| 62 | + pathways = dict() |
| 63 | + for path in pgdb.all_pathways(selector='all', base=True): |
| 64 | + pathways[path] = dict() |
| 65 | + pathways[path]['Name'] = pgdb[path].common_name |
| 66 | + pathways[path]['Reactions'] = set() |
| 67 | + for rxn in pgdb[path].reaction_list: |
| 68 | + if pgdb[rxn].spontaneous_p != None: |
| 69 | + if pgdb[rxn].spontaneous_p != True: |
| 70 | + pathways[path]['Reactions'].add(rxn) |
| 71 | + else: |
| 72 | + pathways[path]['Reactions'].add(rxn) |
| 73 | + return(pathways) |
| 74 | + |
| 75 | +def get_pathways_none_spontaneous_orphan_reactions(pgdb): |
| 76 | + pathways = dict() |
| 77 | + for path in pgdb.all_pathways(selector='all', base=True): |
| 78 | + pathways[path] = dict() |
| 79 | + pathways[path]['Name'] = pgdb[path].common_name |
| 80 | + pathways[path]['Reactions'] = set() |
| 81 | + for rxn in pgdb[path].reaction_list: |
| 82 | + is_spontaneous = False |
| 83 | + is_orphan = None |
| 84 | + is_orphan_in_metacyc = False |
| 85 | + |
| 86 | + if pgdb[rxn].spontaneous_p != None and pgdb[rxn].spontaneous_p == True: |
| 87 | + is_spontaneous = True |
| 88 | + |
| 89 | + if is_spontaneous == False: |
| 90 | + if len(get_genes_of_reaction(rxn, pgdb)) == 0: |
| 91 | + is_orphan_in_metacyc = True |
| 92 | + |
| 93 | + if pgdb[rxn].orphan_p != None and pgdb[rxn].orphan_p[0] == "|NO|": |
| 94 | + is_orphan = False |
| 95 | + else: |
| 96 | + is_orphan = True |
| 97 | + |
| 98 | + if not(is_orphan_in_metacyc and (is_orphan == None or is_orphan)) and not is_spontaneous: |
| 99 | + pathways[path]['Reactions'].add(rxn) |
| 100 | + |
| 101 | + return(pathways) |
| 102 | + |
| 103 | +def get_reactions_with_genes(pgdb): |
| 104 | + reactions = set() |
| 105 | + for rxn in pgdb.all_rxns(type_of_reactions = ':all'): |
| 106 | + if len(get_genes_of_reaction(rxn, pgdb)) != 0: |
| 107 | + reactions.add(rxn) |
| 108 | + return(reactions) |
| 109 | + |
| 110 | + |
| 111 | +def get_pathways(pgdb): |
| 112 | + pathways = set() |
| 113 | + for path in pgdb.all_pathways(selector='all', base=True): |
| 114 | + pathways.add(path) |
| 115 | + return(pathways) |
| 116 | + |
| 117 | + |
| 118 | +def write_pgdb_pathway_completion(pgdb, pgdb_name, use_orphan, completion_dict, position): |
| 119 | + print(completion_dict) |
| 120 | + if use_orphan: |
| 121 | + meta_pathways = get_pathways_none_spontaneous_reactions(pgdb) |
| 122 | + file_name = pgdb_name+"_pathway_completion.tsv" |
| 123 | + else: |
| 124 | + meta_pathways = get_pathways_none_spontaneous_orphan_reactions(pgdb) |
| 125 | + file_name = pgdb_name+"_pathway_completion_wo_orphan.tsv" |
| 126 | + |
| 127 | + pgdb_reactions = get_reactions_with_genes(pgdb) |
| 128 | + pgdb_pathways = get_pathways(pgdb) |
| 129 | + |
| 130 | + with open(file_name, "w") as pgdb_write: |
| 131 | + header="PGDB\tPathway\tPathway_name\tIs_predicted\tCompletion\n" |
| 132 | + pgdb_write.write(header) |
| 133 | + for path in meta_pathways: |
| 134 | + path_predicted = path in pgdb_pathways |
| 135 | + if len(meta_pathways[path]['Reactions']) == 0: |
| 136 | + completion = 0.0 |
| 137 | + else: |
| 138 | + completion = len(meta_pathways[path]['Reactions'].intersection(pgdb_reactions))/len(meta_pathways[path]['Reactions']) |
| 139 | + to_write=pgdb_name+"\t"+path.split("|")[1]+"\t"+meta_pathways[path]['Name']+"\t"+str(path_predicted)+"\t"+str(completion)+"\n" |
| 140 | + pgdb_write.write(to_write) |
| 141 | + completion_dict[pgdb[path].common_name][position] = (str(completion)) |
| 142 | + return(completion_dict) |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +def write_completion_matrix(completion_dict, file_name, header): |
| 147 | + with open(file_name, "w") as matrix_file: |
| 148 | + matrix_file.write(header) |
| 149 | + for pathway in completion_dict: |
| 150 | + values_to_write = "\t".join(completion_dict[pathway]) |
| 151 | + to_write = pathway+"\t"+values_to_write+"\n" |
| 152 | + matrix_file.write(to_write) |
| 153 | + |
| 154 | + |
| 155 | +def init_completion_dict(pgdb_list, completion_dict, completion_dict_wo_orphan): |
| 156 | + for p in pgdb_list: |
| 157 | + name = p.strip() |
| 158 | + pgdb_name = '|'+name+'|' |
| 159 | + pgdb = pythoncyc.select_organism(pgdb_name) |
| 160 | + pgdb_pathways = get_pathways(pgdb) |
| 161 | + for path in pgdb_pathways: |
| 162 | + path_name = pgdb[path].common_name |
| 163 | + if path_name not in completion_dict.keys(): |
| 164 | + completion_dict[path_name] = ["0.0"] * len(pgdb_list) |
| 165 | + completion_dict_wo_orphan[path_name] = ["0.0"] * len(pgdb_list) |
| 166 | + return(completion_dict, completion_dict_wo_orphan) |
| 167 | + |
| 168 | + |
| 169 | +def main(): |
| 170 | + parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) |
| 171 | + parser.add_argument("-p", help="Enter the name of the PGDB (example : META)", required=False, type=str) |
| 172 | + parser.add_argument("-l", help="Enter file with list of PGDB ", required=False, type=str) |
| 173 | + parser.add_argument("-m", help="Argument for missing value : O or NA - 0 by default", required=False, type=str) |
| 174 | + args = parser.parse_args() |
| 175 | + ## write completion in separate files |
| 176 | + header = "#NAMES" |
| 177 | + completion_dict = dict() |
| 178 | + completion_dict_wo_orphan = dict() |
| 179 | + with open(args.l) as pgdb_file: |
| 180 | + pgdb_list = [line.strip() for line in pgdb_file] |
| 181 | + (completion_dict, completion_dict_wo_orphan) = init_completion_dict(pgdb_list, completion_dict, completion_dict_wo_orphan) |
| 182 | + if args.l: |
| 183 | + for (name, position) in zip(pgdb_list, range(len(pgdb_list))): |
| 184 | + name = name.strip() |
| 185 | + print(name) |
| 186 | + header = header +"\t"+name |
| 187 | + print(header) |
| 188 | + pgdb_name = '|'+name+'|' |
| 189 | + print(pgdb_name) |
| 190 | + pgdb = pythoncyc.select_organism(pgdb_name) |
| 191 | + print(pgdb) |
| 192 | + #write_reactions_kegg_cross_ref(pgdb) |
| 193 | + write_pathway(pgdb) |
| 194 | + completion_dict = write_pgdb_pathway_completion(pgdb, name, True, completion_dict, position) |
| 195 | + completion_dict_wo_orphan = write_pgdb_pathway_completion(pgdb, name , False, completion_dict_wo_orphan, position) |
| 196 | + #print(pgdb._orgid) |
| 197 | + #close_pgdb_wosaving(pgdb) |
| 198 | + elif args.p: |
| 199 | + pgdb_name = '|'+args.p+'|' |
| 200 | + pgdb = pythoncyc.select_organism(pgdb_name) |
| 201 | + write_pathway(pgdb) |
| 202 | + write_pgdb_pathway_completion(pgdb, meta, args.p, True) |
| 203 | + write_pgdb_pathway_completion(pgdb, meta, args.p, False) |
| 204 | + else: |
| 205 | + sys.exit("Please select an option!") |
| 206 | + header = header+"\n" |
| 207 | + write_completion_matrix(completion_dict, "completion_matrix.txt", header) |
| 208 | + write_completion_matrix(completion_dict_wo_orphan, "completion_matrix_wo_orphan.tsv", header) |
| 209 | + |
| 210 | +if __name__ == "__main__": |
| 211 | + main() |
0 commit comments