def create_gene_terms(): print('Creating gene terms') handle = os.path.join(FIXTURE_DIR, 'hgnc.json') with open(handle, 'r', encoding='utf-8') as f: hgnc_json = f.read() g = obograph_util.convert_json_object(json.loads(hgnc_json)) ont = Ontology(handle=handle, payload=g) gene_terms = [] for n_id in ont.nodes(): n_dict = ont.node(n_id) if 'type' in n_dict: if ont.node_type(n_id) == 'CLASS': for t in n_dict['meta']['basicPropertyValues']: if t['pred'] == 'http://ncicb.nci.nih.gov/xml/owl/EVS/Hugo.owl#Approved_Symbol': symbol = t['val'] if not symbol.endswith('~withdrawn'): # print('{} {}'.format(n_id, symbol)) gene_terms.append( GeneTerm(term_id=n_id, label=symbol)) break GeneTerm.objects.bulk_create(gene_terms)
def get_all_paths_to_root(node_id: str, ontology: Ontology, min_distance_from_root: int = 0, relations: List[str] = None, nodeids_blacklist: List[str] = None, previous_path: Union[None, List[str]] = None, root_node=None) -> Set[Tuple[str]]: """get all possible paths connecting a go term to its root terms Args: node_id (str): a valid GO id for the starting term ontology (Ontology): the go ontology min_distance_from_root (int): return only terms at a specified minimum distance from root terms relations (List[str]): the list of relations to be used nodeids_blacklist (List[str]): a list of node ids to exclude from the paths previous_path (Union[None, List[str]]): the path to get to the current node Returns: Set[Tuple[str]]: the set of paths connecting the specified term to its root terms, each of which contains a sequence of terms ids """ if previous_path is None: previous_path = [] new_path = previous_path[:] if not nodeids_blacklist or node_id not in nodeids_blacklist: new_path.append(node_id) parents = [ parent for parent in ontology.parents(node=node_id, relations=relations) if ontology.node(parent)["depth"] >= min_distance_from_root ] parents_same_root = [] if root_node: for parent in parents: parent_node = ontology.node(parent) parent_root = None if "meta" in parent_node and "basicPropertyValues" in parent_node[ "meta"]: for basic_prop_val in parent_node["meta"][ "basicPropertyValues"]: if basic_prop_val["pred"] == "OIO:hasOBONamespace": parent_root = basic_prop_val["val"] if parent_root and parent_root == root_node: parents_same_root.append(parent) parents = parents_same_root if len(parents) > 0: # go up the tree, following a depth first visit paths_to_return = set() for parent in parents: for path in TrimmingAlgorithmNaive.get_all_paths_to_root( node_id=parent, ontology=ontology, previous_path=new_path, min_distance_from_root=min_distance_from_root, relations=relations, nodeids_blacklist=nodeids_blacklist, root_node=root_node): paths_to_return.add(path) return paths_to_return if len(new_path) == 0: return {(node_id, )} else: return {tuple(new_path)}