1""" 2Return types from api classes interface for the SOAP kegg api. 3 4""" 5 6from datetime import datetime 7from operator import methodcaller 8from collections import namedtuple 9 10Definition = namedtuple("Definition", ["entry_id", "definition"]) 11 12 13def _Definition_from_items(items): 14 """ Definition 'items' tuple from a list of items 15 """ 16 items_list = [] 17 for name, val in items: 18 if isinstance(name, list): 19 name = name[0] 20 if isinstance(val, list): 21 val = val[0] 22 items_list.append((str(name), str(val))) 23 return Definition(**dict(items_list)) 24 25 26def _Definition_from_str(text): 27 """ 28 Return a `Definition` item by parsing a tab separated string `text` 29 i.e. text must be of the form '<entry_id>\t<definition>' 30 31 """ 32 return Definition(*text.split("\t", 1)) 33 34 35Definition.from_items = staticmethod(_Definition_from_items) 36Definition.from_str = staticmethod(_Definition_from_str) 37 38 39OrganismSummary = namedtuple("OrganismSummary", ["entry_id", "org_code", "name", "lineage"]) 40 41 42def OrganismSummary_from_str(string): 43 # string = string.decode("utf8") 44 return OrganismSummary(*string.split("\t")) 45 46 47OrganismSummary.from_str = staticmethod(OrganismSummary_from_str) 48 49 50BInfo = namedtuple( 51 'BInfo', ["entry_id", "definition", "name", "release", "curator", "contents", "last_update", "supported_formats"] 52) 53 54 55def _BInfo_from_text(text): 56 """ Parse the return string from info into a new BInfo instance. 57 """ 58 lines = text.splitlines() 59 name, definition = lines[0].split(" ", 1) 60 definition = definition.strip() 61 entry_id, release = lines[1].split(" ", 1) 62 _, release = release.strip().split(" ", 1) 63 curator = lines[2].strip() 64 contents = "\n".join(map(methodcaller("strip"), lines[3:])) 65 66 return BInfo(entry_id, definition, name, release, curator, contents, None, None) 67 68 69BInfo.from_text = staticmethod(_BInfo_from_text) 70 71 72Link = namedtuple("Link", ["entry_id1", "entry_id2"]) 73 74 75SSDBRelation = namedtuple( 76 "SSDBRelation", 77 [ 78 "genes_id1", 79 "genes_id2", 80 "sw_score", 81 "bit_score", 82 "identity", 83 "overlap", 84 "start_position1", 85 "end_position1", 86 "start_position2", 87 "end_position2", 88 "best_flag_1to2", 89 "best_flag_2to1", 90 "definition1", 91 "definition2", 92 "length1", 93 "length2", 94 ], 95) 96 97MotifResult = namedtuple( 98 "MotifResult", ["motif_id", "definition", "genes_id", "start_position", "end_position", "score", "evalue"] 99) 100 101LinkDBRelation = namedtuple("LinkDBRelation", ["entry_id1", "entry_id2", "type", "path"]) 102 103PathwayElement = namedtuple("PathwayElement", ["element_id", "type", "names", "components"]) 104 105PathwayElementRelation = namedtuple("PathwayElementRelation", ["element_id1", "element_id2", "type", "subtypes"]) 106 107Subtype = namedtuple("Subtype", ["element_id", "relation", "type"]) 108