code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
digraph = networkx.MultiDiGraph()
from rdflib.namespace import RDF
label_map = {}
for c in rg.subjects(RDF.type, OWL.Class):
cid = contract_uri_wrap(c)
logging.info("C={}".format(cid))
for lit in rg.objects(c, RDFS.label):
label_map[cid] = lit.value
d... | def rdfgraph_to_ontol(rg) | Return an Ontology object from an rdflib graph object
Status: Incomplete | 4.374811 | 4.473859 | 0.977861 |
if entities is None:
entities = []
results = search_associations(subjects=entities,
subject_direct=True,
rows=0,
facet_fields=[M.IS_DEFINED_BY, M.SUBJECT_TAXON, M.SUBJECT_CATEGORY],
... | def get_counts(entities=None,
object_category=None,
min_count=1,
**kwargs) | given a set of entities (genes, diseases, etc), finds the number of entities associated with each descriptor in a given category.
The result is a tuple (cmap, results), where cmap is a dict of TERM:COUNT | 4.788345 | 5.056205 | 0.947024 |
if sample_entities is None:
sample_entites = []
(sample_counts, sample_results) = get_counts(entities=sample_entities,
object_category=object_category,
min_count=2,
... | def find_enriched(sample_entities=None,
background_entities=None,
object_category=None,
**kwargs) | Given a sample set of sample_entities (e.g. overexpressed genes) and a background set (e.g. all genes assayed), and a category of descriptor (e.g. phenotype, function),
return enriched descriptors/classes | 4.355319 | 4.428957 | 0.983373 |
results = search_associations(id=id, **kwargs)
assoc = results['associations'][0] if len(results['associations']) > 0 else {}
return assoc | def get_association(id, **kwargs) | Fetch an association object by ID | 3.187003 | 3.24258 | 0.98286 |
logging.info("CREATING_GOLR_QUERY {}".format(kwargs))
q = GolrAssociationQuery(**kwargs)
return q.exec() | def search_associations(**kwargs) | Fetch a set of association objects based on a query. | 10.516428 | 11.028956 | 0.953529 |
searchresult = search_associations(subject=subject,
fetch_objects=True,
rows=0,
object_category=object_category,
relation=relation,
... | def get_objects_for_subject(subject=None,
object_category=None,
relation=None,
**kwargs) | Convenience method: Given a subject (e.g. gene, disease, variant), return all associated objects (phenotypes, functions, interacting genes, etc) | 5.75712 | 5.38828 | 1.068452 |
searchresult = search_associations(object=object,
fetch_subjects=True,
rows=0,
subject_category=subject_category,
subject_taxon=subject_taxon,
... | def get_subjects_for_object(object=None,
subject_category=None,
subject_taxon=None,
relation=None,
**kwargs) | Convenience method: Given a object (e.g. ontology term like phenotype or GO; interacting gene; disease; pathway etc), return all associated subjects (genes, variants, pubs, etc) | 4.32998 | 4.305616 | 1.005659 |
logging.info("SLIM SUBJECTS:{} SLIM:{} CAT:{}".format(subjects,slim,kwargs.get('category')))
searchresult = search_associations(subjects=subjects,
slim=slim,
facet_fields=[],
**kwargs
)
... | def map2slim(subjects, slim, **kwargs) | Maps a set of subjects (e.g. genes) to a set of slims
Result is a list of unique subject-class pairs, with
a list of source assocations | 3.757544 | 3.840617 | 0.97837 |
assert subject_category is not None
assert object_category is not None
time.sleep(1)
logging.info("Bulk query: {} {} {}".format(subject_category, object_category, taxon))
assocs = search_associations_compact(subject_category=subject_category,
object_cate... | def bulk_fetch(subject_category, object_category, taxon, rows=MAX_ROWS, **kwargs) | Fetch associations for a species and pair of categories in bulk.
Arguments:
- subject_category: String (not None)
- object_category: String (not None)
- taxon: String
- rows: int
Additionally, any argument for search_associations can be passed | 2.676638 | 2.809132 | 0.952835 |
if facet_pivot_fields is None:
facet_pivot_fields = []
results = search_associations(rows=0,
facet_fields=[facet],
#facet_pivot_fields=facet_pivot_fields + [facet],
facet_pivot_fields=facet_pivot_f... | def pivot_query(facet=None, facet_pivot_fields=None, **kwargs) | Pivot query | 3.121475 | 3.121576 | 0.999968 |
if facet_pivot_fields is None:
facet_pivot_fields = []
logging.info("Additional args: {}".format(kwargs))
fp = search_associations(rows=0,
facet_fields=[facet],
facet_pivot_fields=facet_pivot_fields,
**kwargs... | def pivot_query_as_matrix(facet=None, facet_pivot_fields=None, **kwargs) | Pivot query | 2.869816 | 2.871862 | 0.999288 |
go_golr_url = "http://golr.geneontology.org/solr/"
go_solr = pysolr.Solr(go_golr_url, timeout=5)
go_solr.get_session().headers['User-Agent'] = get_user_agent(caller_name=__name__)
return search_associations(subject_category,
object_category,
... | def search_associations_go(
subject_category=None,
object_category=None,
relation=None,
subject=None,
**kwargs) | Perform association search using Monarch golr | 3.792314 | 3.592713 | 1.055557 |
results = search_associations(rows=0,
select_fields=[],
facet_field_limits = {
distinct_field : -1
},
facet_fields=[distinct_field],
... | def select_distinct(distinct_field=None, **kwargs) | select distinct values for a given field for a given a query | 9.542306 | 10.157862 | 0.939401 |
am = self.assocmodel
a1 = am.inferred_types(s1)
a2 = am.inferred_types(s2)
num_union = len(a1 | a2)
if num_union == 0:
return 0.0
return len(a1 & a2) / num_union | def pw_score_jaccard(self, s1 : ClassId, s2 : ClassId) -> SimScore | Calculate jaccard index of inferred associations of two subjects
|ancs(s1) /\ ancs(s2)|
---
|ancs(s1) \/ ancs(s2)| | 3.456918 | 3.294554 | 1.049282 |
df = self.assoc_df
slice1 = df.loc[s1].values
slice2 = df.loc[s2].values
return 1 - cosine(slice1, slice2) | def pw_score_cosine(self, s1 : ClassId, s2 : ClassId) -> SimScore | Cosine similarity of two subjects
Arguments
---------
s1 : str
class id
Return
------
number
A number between 0 and 1 | 3.970795 | 4.50141 | 0.882123 |
logging.info("Calculating all class ICs")
df = self.assoc_df
freqs = df.sum(axis=0)
n_subjects, _ = df.shape
ics = freqs.apply(lambda x: -math.log(x / n_subjects)/math.log(2))
self.ics = ics
logging.info("DONE calculating all class ICs")
return ic... | def calculate_all_information_content(self) -> pd.Series | Calculate the Information Content (IC) value of every class
Sets the internal icmap cache and returns an array
Return
------
Series
a pandas Series indexed by class id and with IC as value | 4.680089 | 4.041819 | 1.157917 |
G = self.G
# reflexive ancestors
ancs1 = self._ancestors(c1) | {c1}
ancs2 = self._ancestors(c2) | {c2}
common_ancestors = ancs1 & ancs2
redundant = set()
for a in common_ancestors:
redundant = redundant | nx.ancestors(G, a)
return comm... | def calculate_mrcas(self, c1 : ClassId, c2 : ClassId) -> Set[ClassId] | Calculate the MRCA for a class pair | 3.383085 | 3.253169 | 1.039935 |
G = self.G
ics = self._information_content_frame()
classes = list(dfs.dfs_preorder_nodes(G))
#mica_df = pd.DataFrame(index=classes, columns=classes)
#mica_ic_df = pd.DataFrame(index=classes, columns=classes)
ncs = len(classes)
ic_grid = np.empty([ncs,ncs]... | def calculate_all_micas(self) | Calculate the MICA (Most Informative Common Ancestor) of every class-pair | 2.537143 | 2.454915 | 1.033496 |
pairs = self.mica_ic_df.loc[cset1, cset2]
max0 = pairs.max(axis=0)
max1 = pairs.max(axis=1)
idxmax0 = pairs.idxmax(axis=0)
idxmax1 = pairs.idxmax(axis=1)
mean0 = max0.mean()
mean1 = max1.mean()
return (mean0+mean1)/2, mean0, mean1 | def pw_compare_class_sets(self, cset1: Set[ClassId], cset2: Set[ClassId]) -> Tuple[ICValue, ICValue, ICValue] | Compare two class profiles | 3.152204 | 3.027482 | 1.041197 |
raise NotImplementedError | def search(self,
id_list: Set,
negated_classes: Set,
method: Optional) -> List[SimResult] | def search(self, cset: Set[ClassId]):
slice = self.mica_ic_df.loc[cset]
am = self.assocmodel
for i in am.subjects:
pass # TODO | 790.639771 | 521.812866 | 1.515179 |
raise NotImplementedError | def compare(self,
query_classes: Set,
reference_classes: Set,
method: Optional) -> SimResult | Given two lists of entites (classes, individual)
return their similarity | 2,542.900391 | 458.191376 | 5.549865 |
rdfgraph = rdflib.Graph()
if format is None:
if filename.endswith(".ttl"):
format='turtle'
elif filename.endswith(".rdf"):
format='xml'
rdfgraph.parse(filename, format=format)
return self.process_rdfgraph(rdfgraph) | def process_file(self,filename=None, format=None) | Parse a file into an ontology object, using rdflib | 2.792871 | 2.475906 | 1.12802 |
# TODO: ontology metadata
if ont is None:
ont = Ontology()
subjs = list(rg.subjects(RDF.type, SKOS.ConceptScheme))
if len(subjs) == 0:
logging.warning("No ConceptScheme")
else:
ont.id = self._uri2id(subjs[0])
... | def process_rdfgraph(self, rg, ont=None) | Transform a skos terminology expressed in an rdf graph into an Ontology object
Arguments
---------
rg: rdflib.Graph
graph object
Returns
-------
Ontology | 2.662982 | 2.690495 | 0.989774 |
owlsim_url = url + 'searchByAttributeSet'
params = {
'a': profile,
'limit': limit,
'target': namespace_filter
}
return requests.get(owlsim_url, params=params, timeout=TIMEOUT).json() | def search_by_attribute_set(
url: str,
profile: Tuple[str],
limit: Optional[int] = 100,
namespace_filter: Optional[str]=None) -> Dict | Given a list of phenotypes, returns a ranked list of individuals
individuals can be filtered by namespace, eg MONDO, MGI, HGNC
:returns Dict with the structure: {
'unresolved' : [...]
'query_IRIs' : [...]
'results': {...}
}
:raises JSONDecodeError: If the response body does not c... | 5.16679 | 4.960226 | 1.041644 |
owlsim_url = url + 'compareAttributeSets'
params = {
'a': profile_a,
'b': profile_b,
}
return requests.get(owlsim_url, params=params, timeout=TIMEOUT).json() | def compare_attribute_sets(
url: str,
profile_a: Tuple[str],
profile_b: Tuple[str]) -> Dict | Given two phenotype profiles, returns their similarity
:returns Dict with the structure: {
'unresolved' : [...]
'query_IRIs' : [...]
'target_IRIs': [...]
'results': {...}
} | 4.01807 | 3.489137 | 1.151594 |
owlsim_url = url + 'getAttributeInformationProfile'
params = {
'a': profile,
'r': categories
}
return requests.get(owlsim_url, params=params, timeout=TIMEOUT).json() | def get_attribute_information_profile(
url: str,
profile: Optional[Tuple[str]]=None,
categories: Optional[Tuple[str]]=None) -> Dict | Get the information content for a list of phenotypes
and the annotation sufficiency simple and
and categorical scores if categories are provied
Ref: https://zenodo.org/record/834091#.W8ZnCxhlCV4
Note that the simple score varies slightly from the pub in that
it uses max_max_ic instead of mean_max_i... | 6.449217 | 7.075823 | 0.911444 |
scigraph = OntologyFactory().create('scigraph:ontology')
category_stats = {}
categories = [enum.value for enum in HpoUpperLevel]
sim_response = get_attribute_information_profile(url, categories=tuple(categories))
try:
global_stats = IcStatistic(
mean_mean_ic=float(sim_respo... | def get_owlsim_stats(url) -> Tuple[IcStatistic, Dict[str, IcStatistic]] | :return Tuple[IcStatistic, Dict[str, IcStatistic]]
:raises JSONDecodeError: If the response body does not contain valid json | 2.578211 | 2.574678 | 1.001372 |
return self.filtered_search(
id_list=id_list,
negated_classes=negated_classes,
limit=limit,
taxon_filter=None,
category_filter=None,
method=method
) | def search(
self,
id_list: List,
negated_classes: List,
limit: Optional[int] = 100,
method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM) -> SimResult | Owlsim2 search, calls search_by_attribute_set, and converts to SimResult object
:raises JSONDecodeError: If the owlsim response is not valid json. | 3.415732 | 3.390711 | 1.007379 |
if len(negated_classes) > 0:
logging.warning("Owlsim2 does not support negation, ignoring neg classes")
namespace_filter = self._get_namespace_filter(taxon_filter, category_filter)
owlsim_results = search_by_attribute_set(self.url, tuple(id_list), limit, namespace_filter)
... | def filtered_search(
self,
id_list: List,
negated_classes: List,
limit: Optional[int] = 100,
taxon_filter: Optional[int] = None,
category_filter: Optional[str] = None,
method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM) -> SimResul... | Owlsim2 filtered search, resolves taxon and category to a namespace,
calls search_by_attribute_set, and converts to SimResult object | 6.134144 | 3.735488 | 1.642126 |
owlsim_results = compare_attribute_sets(self.url, tuple(reference_classes), tuple(query_classes))
return self._simcompare_to_simresult(owlsim_results, method) | def compare(self,
reference_classes: List,
query_classes: List,
method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM) -> SimResult | Owlsim2 compare, calls compare_attribute_sets, and converts to SimResult object
:return: SimResult object | 9.186742 | 5.448285 | 1.686171 |
return [
SimAlgorithm.PHENODIGM,
SimAlgorithm.JACCARD,
SimAlgorithm.SIM_GIC,
SimAlgorithm.RESNIK,
SimAlgorithm.SYMMETRIC_RESNIK
] | def matchers() -> List[SimAlgorithm] | Matchers in owlsim2 | 8.416718 | 8.545638 | 0.984914 |
sim_response = get_attribute_information_profile(self.url, tuple(profile))
profile_ic = {}
try:
for cls in sim_response['input']:
profile_ic[cls['id']] = cls['IC']
except JSONDecodeError as json_exc:
raise JSONDecodeError(
... | def get_profile_ic(self, profile: List) -> Dict | Given a list of individuals, return their information content | 5.587141 | 5.313993 | 1.051402 |
sim_ids = get_nodes_from_ids(sim_resp['query_IRIs'])
sim_resp['results'] = OwlSim2Api._rank_results(sim_resp['results'], method)
# get id type map:
ids = [result['j']['id'] for result in sim_resp['results']]
id_type_map = get_id_type_map(ids)
matches = []
... | def _simsearch_to_simresult(self, sim_resp: Dict, method: SimAlgorithm) -> SimResult | Convert owlsim json to SimResult object
:param sim_resp: owlsim response from search_by_attribute_set()
:param method: SimAlgorithm
:return: SimResult object | 4.479536 | 4.392605 | 1.01979 |
pairwise_matches = []
for pairwise_match in result['matches']:
pairwise_matches.append(
PairwiseMatch(
reference=ICNode(**pairwise_match['a']),
match=ICNode(**pairwise_match['b']),
lcs=ICNode(**pairwise_matc... | def _make_pairwise_matches(result: Dict) -> List[PairwiseMatch] | Make a list of match object from owlsim results
:param result: Single owlsim result
:return: List of SimMatch objects | 3.288114 | 3.565718 | 0.922146 |
# https://stackoverflow.com/a/73050
sorted_results = sorted(
results, reverse=True, key=lambda k: k[OwlSim2Api.method2key[method]]
)
if len(sorted_results) > 0:
rank = 1
previous_score = sorted_results[0][OwlSim2Api.method2key[method]]
... | def _rank_results(results: List[Dict], method: SimAlgorithm) -> List[Dict] | Ranks results - for phenodigm results are ranks but ties need to accounted for
for other methods, results need to be reranked
:param results: Results from search_by_attribute_set()['results'] or
compare_attribute_sets()['results']
:param meth... | 2.800009 | 2.835686 | 0.987419 |
namespace_filter = None
taxon_category_default = {
10090: 'gene',
9606: 'disease',
7227: 'gene',
6239: 'gene',
7955: 'gene'
}
if category_filter is not None and taxon_filter is None:
raise ValueError("Mu... | def _get_namespace_filter(
taxon_filter: Optional[int]=None,
category_filter: Optional[str]=None) -> Union[None, str] | Given either a taxon and/or category, return the correct namespace
:raises ValueError: If category is provided without a taxon | 3.537073 | 3.223225 | 1.097371 |
if 'facet_fields' not in fcs:
return {}
ffs = fcs['facet_fields']
rs={}
for (facet, facetresults) in ffs.items():
if invert_subject_object:
for (k,v) in INVERT_FIELDS_MAP.items():
if facet == k:
facet = v
break
... | def translate_facet_field(fcs, invert_subject_object = False) | Translates solr facet_fields results into something easier to manipulate
A solr facet field looks like this: [field1, count1, field2, count2, ..., fieldN, countN]
We translate this to a dict {f1: c1, ..., fn: cn}
This has slightly higher overhead for sending over the wire, but is easier to use | 3.322618 | 3.329014 | 0.998079 |
return {
M.SUBJECT: 'bioentity',
M.SUBJECT_CLOSURE: 'bioentity',
## In the GO AmiGO instance, the type field is not correctly populated
## See above in the code for hack that restores this for planteome instance
## M.SUBJECT_CATEGORY: 'type',
M.SUBJECT_CATEGORY: ... | def goassoc_fieldmap(relationship_type=ACTS_UPSTREAM_OF_OR_WITHIN) | Returns a mapping of canonical monarch fields to amigo-golr.
See: https://github.com/geneontology/amigo/blob/master/metadata/ann-config.yaml | 3.736805 | 3.594516 | 1.039585 |
if m is None:
return fn
if fn in m:
return m[fn]
else:
return fn | def map_field(fn, m) | Maps a field name, given a mapping file.
Returns input if fieldname is unmapped. | 4.22427 | 3.105083 | 1.360437 |
query = solrInstance.value + "select?q=*:*&fq=document_category:\"" + category.value + "\"&fq=id:\"" + id + "\"&fl=" + fields + "&wt=json&indent=on"
response = requests.get(query)
return response.json()['response']['docs'][0] | def run_solr_on(solrInstance, category, id, fields) | Return the result of a solr query on the given solrInstance (Enum ESOLR), for a certain document_category (ESOLRDoc) and id | 3.142931 | 2.748322 | 1.143582 |
if optionals == None:
optionals = ""
query = solrInstance.value + "select?q=" + q + "&qf=" + qf + "&fq=document_category:\"" + category.value + "\"&fl=" + fields + "&wt=json&indent=on" + optionals
# print("QUERY: ", query)
response = requests.get(query)
return response.json()['response... | def run_solr_text_on(solrInstance, category, q, qf, fields, optionals) | Return the result of a solr query on the given solrInstance (Enum ESOLR), for a certain document_category (ESOLRDoc) and id | 3.259766 | 2.985462 | 1.09188 |
merged = []
for i in range(0, len(json[firstField])):
merged.append({ json[firstField][i] : json[secondField][i] })
return merged | def merge(json, firstField, secondField) | merge two fields of a json into an array of { firstField : secondField } | 2.776627 | 2.460368 | 1.128541 |
merged = []
for i in range(0, len(json[firstField])):
merged.append({ firstFieldLabel : json[firstField][i],
secondFieldLabel : json[secondField][i] })
return merged | def mergeWithLabels(json, firstField, firstFieldLabel, secondField, secondFieldLabel) | merge two fields of a json into an array of { firstFieldLabel : firstFieldLabel, secondFieldLabel : secondField } | 2.426786 | 2.163764 | 1.121558 |
params = self.solr_params()
logging.info("PARAMS=" + str(params))
results = self.solr.search(**params)
logging.info("Docs found: {}".format(results.hits))
return self._process_search_results(results) | def search(self) | Execute solr search query | 5.108134 | 4.378016 | 1.166769 |
self.facet = False
params = self.solr_params()
logging.info("PARAMS=" + str(params))
results = self.solr.search(**params)
logging.info("Docs found: {}".format(results.hits))
return self._process_autocomplete_results(results) | def autocomplete(self) | Execute solr autocomplete | 6.468285 | 5.626682 | 1.149573 |
# map go-golr fields to standard
for doc in results.docs:
if 'entity' in doc:
doc['id'] = doc['entity']
doc['label'] = doc['entity_label']
highlighting = {
doc['id']: self._process_highlight(results, doc)._asdict()
fo... | def _process_search_results(self,
results: pysolr.Results) -> SearchResults | Convert solr docs to biolink object
:param results: pysolr.Results
:return: model.GolrResults.SearchResults | 5.13575 | 5.163779 | 0.994572 |
# map go-golr fields to standard
for doc in results.docs:
if 'entity' in doc:
doc['id'] = doc['entity']
doc['label'] = doc['entity_label']
docs = []
for doc in results.docs:
if results.highlighting:
hl = se... | def _process_autocomplete_results(
self,
results: pysolr.Results) -> Dict[str, List[AutocompleteResult]] | Convert results to biolink autocomplete object
:param results: pysolr.Results
:return: {'docs': List[AutocompleteResult]} | 3.351672 | 3.230224 | 1.037598 |
len_dict = OrderedDict()
for hl in highlights:
# dummy tags to make it valid xml
dummy_xml = "<p>" + hl + "</p>"
try:
element_tree = ET.fromstring(dummy_xml)
hl_length = 0
for emph in element_tree.findall('em'):... | def _get_longest_hl(self, highlights) | Given a list of highlighted text, returns the
longest highlight
For example:
[
"<em>Muscle</em> <em>atrophy</em>, generalized",
"Generalized <em>muscle</em> degeneration",
"Diffuse skeletal <em>">muscle</em> wasting"
]
and returns:
... | 3.949328 | 3.99338 | 0.988969 |
# dummy tags to make it valid xml
dummy_xml = "<p>" + highlight + "</p>"
try:
element_tree = ET.fromstring(dummy_xml)
except ET.ParseError:
raise ET.ParseError
return "".join(list(element_tree.itertext())) | def _hl_as_string(self, highlight) | Given a solr string of highlighted text, returns the
str representations
For example:
"Foo <em>Muscle</em> bar <em>atrophy</em>, generalized"
Returns:
"Foo Muscle bar atrophy, generalized"
:return: str | 6.252608 | 6.339857 | 0.986238 |
params = self.set_lay_params()
logging.info("PARAMS="+str(params))
results = self.solr.search(**params)
logging.info("Docs found: {}".format(results.hits))
return self._process_layperson_results(results) | def autocomplete(self) | Execute solr query for autocomplete | 9.764201 | 8.045691 | 1.213594 |
payload = {
'results': []
}
for doc in results.docs:
hl = self._process_highlight(results, doc)
highlight = {
'id': doc['id'],
'highlight': hl.highlight,
'label': doc['label'],
'matched... | def _process_layperson_results(self, results) | Convert pysolr.Results to biolink object
:param results:
:return: | 4.940706 | 4.878573 | 1.012736 |
logging.info("Attempting category inference on id={}".format(id))
toks = id.split(":")
idspace = toks[0]
c = None
if idspace == 'DOID':
c='disease'
if c is not None:
logging.info("Inferred category: {} based on id={}".format(c, id))
... | def infer_category(self, id) | heuristic to infer a category from an id, e.g. DOID:nnn --> disease | 5.071438 | 3.967789 | 1.278152 |
if id is not None:
for (k,v) in PREFIX_NORMALIZATION_MAP.items():
s = k+':'
if id.startswith(s):
return id.replace(s,v+':')
return id | def make_canonical_identifier(self,id) | E.g. MGI:MGI:nnnn --> MGI:nnnn | 4.291788 | 4.166883 | 1.029975 |
if fname not in d:
# TODO: consider adding arg for failure on null
return None
#lf = M.label_field(fname)
v = d[fname]
if not isinstance(v,list):
v = [v]
objs = [{'id': idval} for idval in v]
# todo - labels
return o... | def translate_objs(self,d,fname) | Translate a field whose value is expected to be a list | 10.788476 | 9.987783 | 1.080167 |
if fname not in d:
# TODO: consider adding arg for failure on null
return None
lf = M.label_field(fname)
id = d[fname]
id = self.make_canonical_identifier(id)
#if id.startswith('MGI:MGI:'):
# id = id.replace('MGI:MGI:','MGI:')
... | def translate_obj(self,d,fname) | Translate a field value from a solr document.
This includes special logic for when the field value
denotes an object, here we nest it | 6.100399 | 6.26014 | 0.974483 |
if field_mapping is not None:
self.map_doc(d, field_mapping)
subject = self.translate_obj(d, M.SUBJECT)
obj = self.translate_obj(d, M.OBJECT)
# TODO: use a more robust method; we need equivalence as separate field in solr
if map_identifiers is not None:
... | def translate_doc(self, d, field_mapping=None, map_identifiers=None, **kwargs) | Translate a solr document (i.e. a single result row) | 3.126755 | 3.080554 | 1.014998 |
for d in ds:
self.map_doc(d, {}, self.invert_subject_object)
return [self.translate_doc(d, **kwargs) for d in ds] | def translate_docs(self, ds, **kwargs) | Translate a set of solr results | 7.9879 | 8.002921 | 0.998123 |
amap = {}
logging.info("Translating docs to compact form. Slim={}".format(slim))
for d in ds:
self.map_doc(d, field_mapping, invert_subject_object=invert_subject_object)
subject = d[M.SUBJECT]
subject_label = d[M.SUBJECT_LABEL]
# TODO: u... | def translate_docs_compact(self, ds, field_mapping=None, slim=None, map_identifiers=None, invert_subject_object=False, **kwargs) | Translate golr association documents to a compact representation | 3.09137 | 3.081394 | 1.003238 |
prefixc = prefix + ':'
ids = [eid for eid in closure_list if eid.startswith(prefixc)]
# TODO: add option to fail if no mapping, or if >1 mapping
if len(ids) == 0:
# default to input
return id
return ids[0] | def map_id(self,id, prefix, closure_list) | Map identifiers based on an equivalence closure list. | 6.744778 | 6.531178 | 1.032705 |
meta = AssociationSetMetadata(subject_category=subject_category,
object_category=object_category,
taxon=taxon)
if file is not None:
return self.create_from_file(file=file,
... | def create(self, ontology=None,subject_category=None,object_category=None,evidence=None,taxon=None,relation=None, file=None, fmt=None, skim=True) | creates an AssociationSet
Currently, this uses an eager binding to a `ontobio.golr` instance. All compact associations for the particular combination
of parameters are fetched.
Arguments
---------
ontology: an `Ontology` object
subject_category: string represen... | 3.044979 | 3.134846 | 0.971333 |
amap = {}
subject_label_map = {}
for a in tuples:
subj = a[0]
subject_label_map[subj] = a[1]
if subj not in amap:
amap[subj] = []
amap[subj].append(a[2])
aset = AssociationSet(subject_label_map=subject_label_map, a... | def create_from_tuples(self, tuples, **args) | Creates from a list of (subj,subj_name,obj) tuples | 2.863269 | 2.619595 | 1.09302 |
amap = defaultdict(list)
subject_label_map = {}
for a in assocs:
subj = a['subject']
subj_id = subj['id']
subj_label = subj['label']
subject_label_map[subj_id] = subj_label
if not a['negated']:
amap[subj_id].app... | def create_from_assocs(self, assocs, **args) | Creates from a list of association objects | 2.183576 | 2.198201 | 0.993347 |
if fmt is not None and not fmt.startswith('.'):
fmt = '.{}'.format(fmt)
d = {
'.gaf' : GafParser,
'.gpad' : GpadParser,
'.hpoa' : HpoaParser,
}
if fmt is None:
filename = file if isinstance(file, str) else file.name
... | def create_from_file(self, file=None, fmt='gaf', skim=True, **args) | Creates from a file. If fmt is set to None then the file suffixes will
be used to choose a parser.
Arguments
---------
file : str or file
input file or filename
fmt : str
name of format e.g. gaf | 2.936506 | 2.878464 | 1.020164 |
import requests
url = "http://snapshot.geneontology.org/annotations/{}.gaf.gz".format(group)
r = requests.get(url, stream=True, headers={'User-Agent': get_user_agent(modules=[requests], caller_name=__name__)})
p = GafParser()
results = p.skim(r.raw)
return self.c... | def create_from_remote_file(self, group, snapshot=True, **args) | Creates from remote GAF | 5.28143 | 4.871509 | 1.084147 |
if args.slim.find('m') > -1:
logging.info("SLIMMING")
g = get_minimal_subgraph(g, query_ids)
w = GraphRenderer.create(args.to)
if args.showdefs:
w.config.show_text_definition = True
if args.render:
if 'd' in args.render:
logging.info("Showing text defs")
... | def render(ont, query_ids, args) | Writes or displays graph | 6.184359 | 5.997313 | 1.031188 |
parser = argparse.ArgumentParser(
description='Command line interface to python-ontobio.golr library'
,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-A', '--associations', dest='associations', action='store_true', default=False,
help=... | def main() | Wrapper for OGR | 2.6845 | 2.705157 | 0.992364 |
results = search_associations(subject=subject,
object_category=object_category,
select_fields=[],
facet_fields=[M.OBJECT_CLOSURE],
facet_limit=-1,
... | def get_object_closure(subject, object_category=None, **kwargs) | Find all terms used to annotate subject plus ancestors | 5.045338 | 4.726793 | 1.067391 |
set1 = get_object_closure(subject1,
object_category=object_category,
**kwargs)
set2 = get_object_closure(subject2,
object_category=object_category,
**kwargs)
return len(set1.intersect... | def subject_pair_overlap(subject1, subject2, object_category=None, **kwargs) | Jaccard similarity | 2.155009 | 2.203837 | 0.977844 |
i, u = subject_pair_overlap(subject1, subject2, **kwargs)
if i==0:
return 0.0
return i / u | def subject_pair_simj(subject1, subject2, **kwargs) | Jaccard similarity | 4.911732 | 4.583309 | 1.071656 |
if assoc.get("header", False):
return
subj = assoc['subject']
db, db_object_id = self._split_prefix(subj)
rel = assoc['relation']
qualifier = rel['id']
if assoc['negated']:
qualifier = 'NOT|' + qualifier
goid = assoc['object'][... | def write_assoc(self, assoc) | Write a single association to a line in the output file | 5.886081 | 5.713414 | 1.030221 |
# Handle comment 'associations'
if assoc.get("header", False):
# Skip incoming gaf-version headers, as we created the version above already
if re.match("![\s]*gaf.?version", assoc["line"]):
return
self._write(assoc["line"] + "\n")
... | def write_assoc(self, assoc) | Write a single association to a line in the output file | 5.957502 | 5.825386 | 1.022679 |
human_taxon = Node(
id='NCBITaxon:9606',
label='Homo sapiens'
)
return {
'MGI': Node(
id='NCBITaxon:10090',
label='Mus musculus'
),
'MONDO': human_taxon,
'OMIM': human_taxon,
'MONARCH': human_taxon,
'HGNC': ... | def namespace_to_taxon() -> Dict[str, Node] | namespace to taxon mapping | 2.023603 | 2.021732 | 1.000925 |
scigraph = OntologyFactory().create('scigraph:data')
chunks = [id_list[i:i + 400] for i in range(0, len(list(id_list)), 400)]
for chunk in chunks:
params = {
'id': chunk,
'depth': 0
}
try:
result_graph = scigraph._neighbors_graph(**params)
... | def get_scigraph_nodes(id_list)-> Iterator[Dict] | Queries scigraph neighbors to get a list of nodes back
We use the scigraph neighbors function because ids can be sent in batch
which is faster than iteratively querying solr search
or the scigraph graph/id function
:return: json decoded result from scigraph_ontology._neighbors_graph
:raises ValueE... | 5.257563 | 4.863532 | 1.081017 |
type_map = {}
filter_out_types = [
'cliqueLeader',
'Class',
'Node',
'Individual',
'quality',
'sequence feature'
]
for node in get_scigraph_nodes(id_list):
type_map[node['id']] = [typ.lower() for typ in node['meta']['types']
... | def get_id_type_map(id_list: Iterable[str]) -> Dict[str, List[str]] | Given a list of ids return their types
:param id_list: list of ids
:return: dictionary where the id is the key and the value is a list of types | 6.365052 | 6.921514 | 0.919604 |
node_list = []
for result in get_scigraph_nodes(id_list):
if 'lbl' in result:
label = result['lbl']
else:
label = None # Empty string or None?
node_list.append(Node(result['id'], label))
return node_list | def get_nodes_from_ids(id_list: Iterable[str]) -> List[Node] | Given a list of ids return their types
:param id_list: list of ids
:return: dictionary where the id is the key and the value is a list of types | 4.14834 | 4.843112 | 0.856544 |
taxon = None
namespace = id.split(":")[0]
if namespace in namespace_to_taxon():
taxon = namespace_to_taxon()[namespace]
return taxon | def get_taxon(id: str) -> Optional[Node] | get taxon for id
Currently via hardcoding, should replace when scigraph when
taxa are more universally annotated (having these as node
properties would also be more performant)
:param id: curie formatted id
:return: Node where id is the NCBITaxon curie and label is the scientific name | 4.062889 | 5.00545 | 0.811693 |
filter_out_types = [
'cliqueLeader',
'Class',
'Node',
'Individual',
'quality',
'sequence feature'
]
node = next(get_scigraph_nodes([id]))
if 'lbl' in node:
label = node['lbl']
else:
label = None # Empty string or None?
types... | def typed_node_from_id(id: str) -> TypedNode | Get typed node from id
:param id: id as curie
:return: TypedNode object | 6.85899 | 6.572224 | 1.043633 |
curie_map = {}
response = requests.get(url)
if response.status_code == 200:
curie_map = response.json()
else:
curie_map = {}
return curie_map | def get_curie_map(url) | Get CURIE prefix map from SciGraph cypher/curies endpoint | 1.954912 | 1.872867 | 1.043807 |
return self.reporter.json(self.n_lines, self.n_assocs, self.skipped) | def to_report_json(self) | Generate a summary in json format | 23.10018 | 17.071882 | 1.353113 |
json = self.to_report_json()
# summary = json['summary']
s = "# Group: {group} - Dataset: {dataset}\n".format(group=json["group"], dataset=json["dataset"])
s += "\n## SUMMARY\n\n"
s += "This report generated on {}\n\n".format(datetime.date.today())
s += " * Ass... | def to_markdown(self) | Generate a summary in markdown format | 3.209638 | 3.188005 | 1.006786 |
associations = self.association_generator(file, skipheader=skipheader, outfile=outfile)
a = list(associations)
return a | def parse(self, file, skipheader=False, outfile=None) | Parse a line-oriented association file into a list of association dict objects
Note the returned list is of dict objects. TODO: These will
later be specified using marshmallow and it should be possible
to generate objects
Arguments
---------
file : file or string
... | 6.066866 | 6.621714 | 0.916208 |
file = self._ensure_file(file)
for line in file:
parsed_result = self.parse_line(line)
self.report.report_parsed_result(parsed_result, outfile, self.config.filtered_evidence_file, self.config.filter_out_evidence)
for association in parsed_result.associations:... | def association_generator(self, file, skipheader=False, outfile=None) -> Dict | Returns a generator that yields successive associations from file
Yields
------
association | 6.482161 | 6.624885 | 0.978456 |
if subset is not None:
logging.info("Creating mapping for subset: {}".format(subset))
class_map = ontology.create_slim_mapping(subset=subset, relations=relations)
if class_map is None:
raise ValueError("Neither class_map not subset is set")
col = sel... | def map_to_subset(self, file, outfile=None, ontology=None, subset=None, class_map=None, relations=None) | Map a file to a subset, writing out results
You can pass either a subset name (e.g. goslim_generic) or a dictionary with ready-made mappings
Arguments
---------
file: file
Name or file object for input assoc file
outfile: file
Name or file object for out... | 3.382523 | 3.433395 | 0.985183 |
parser = argparse.ArgumentParser(
description='Command line interface to python-ontobio.golr library'
,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-o', '--outfile', type=str, required=False,
help='Path to output file')
parser.ad... | def main() | Wrapper for OGR | 3.15307 | 3.191636 | 0.987917 |
if session.config is None:
path = session.default_config_path
if os.path.isfile(path):
logging.info("LOADING FROM: {}".format(path))
session.config = load_config(path)
else:
session.config = Config()
logging.info("using default session: {}... | def get_config() | Return configuration for current session.
When called for the first time, this will create a config object, using
whatever is the default load path to find the config yaml | 3.891671 | 3.753497 | 1.036812 |
logging.info("LOADING FROM: {}".format(path))
session.config = load_config(path)
return session.config | def set_config(path) | Set configuration for current session. | 7.179733 | 7.346335 | 0.977322 |
url = self.endpoint_url(self.solr_search)
if use_amigo:
url = self.endpoint_url(self.amigo_solr_search)
return url | def get_solr_search_url(self, use_amigo=False) | Return solr URL to be used for lexical entity searches
A solr search URL is used to search entities/concepts based on a limited set of parameters.
Arguments
---------
use_amigo : bool
If true, get the URL for the GO/AmiGO instance of GOlr. This is typically used for categor... | 2.809652 | 3.884622 | 0.723276 |
url = self.endpoint_url(self.solr_assocs)
if use_amigo:
url = self.endpoint_url(self.amigo_solr_assocs)
return url | def get_solr_assocs_url(self, use_amigo=False) | Return solr URL to be used for assocation (enhanced triple) queries
A solr assocs URL is used to query triple-patterns in Solr, ie subject-relation-object
There are two possible schemas: Monarch and AmiGO. The AmiGO schema is used for
querying the GO and Planteome Golr instances | 2.633345 | 3.243351 | 0.811921 |
gaf_urls = [ (data, data["source"]) for data in group_metadata["datasets"] if data["type"] == "gaf" and data["dataset"] not in exclusions ]
# List of dataset metadata to gaf download url
click.echo("Found {}".format(", ".join( [ kv[0]["dataset"] for kv in gaf_urls ] )))
downloaded_paths = []
f... | def download_source_gafs(group_metadata, target_dir, exclusions=[], base_download_url=None) | This looks at a group metadata dictionary and downloads each GAF source that is not in the exclusions list.
For each downloaded file, keep track of the path of the file. If the file is zipped, it will unzip it here.
This function returns a list of tuples of the dataset dictionary mapped to the downloaded source... | 5.474878 | 5.258955 | 1.041058 |
if categories is None:
categories = [enum.value for enum in HpoUpperLevel]
ic_map = self.ic_store.get_profile_ic(profile + negated_classes)
# Simple score is the weighted average of the present and
# explicitly stated negative/absent phenotypes
#
# ... | def get_annotation_sufficiency(
self,
profile: List[str],
negated_classes: List[str],
categories: Optional[List] = None,
negation_weight: Optional[float] = .25,
category_weight: Optional[float] = .5) -> AnnotationSufficiency | Given a list of individuals, return the simple, scaled, and categorical scores | 4.544084 | 4.275937 | 1.062711 |
if ic_map is None:
ic_map = self.ic_store.get_profile_ic(profile + negated_classes)
pos_map = {cls: ic for cls, ic in ic_map.items() if cls in profile}
neg_map = {cls: ic for cls, ic in ic_map.items() if cls in negated_classes}
mean_ic = mean(pos_map.values()) if l... | def _get_simple_score(self,
profile: List[str],
negated_classes: List[str],
bg_mean_pic: float,
bg_mean_max_pic: float,
bg_mean_sum_pic: float,
negation_weight: Opt... | Simple score is the average of the relative
mean ic, max ic, and sum ic (relative to global stats)
:param ic_map: dictionary of class - information content mappings
:param bg_mean_pic: the average of the average IC in
the background profile annotations
:param... | 1.931862 | 1.956863 | 0.987224 |
return np.average(
[simple_score, categorical_score], weights=[1, category_weight]
) | def _get_scaled_score(
simple_score: float,
categorical_score: float,
category_weight: Optional[float] = .5) -> float | Scaled score is the weighted average of the simple score and
categorical score | 4.767496 | 4.067319 | 1.172147 |
if ic_map is None:
ic_map = self.ic_store.get_profile_ic(profile + negated_classes)
scores = []
for cat in categories:
if cat not in self.ic_store.category_statistics:
raise ValueError("statistics for {} not indexed".format(cat))
p... | def _get_categorical_score(
self,
profile: List,
negated_classes: List,
categories: List,
negation_weight: Optional[float] = 1,
ic_map: Optional[Dict[str, float]] = None) -> float | The average of the simple scores across a list of categories | 3.161736 | 3.022929 | 1.045918 |
if negated_ids is None: negated_ids = []
if method not in self.sim_api.matchers():
raise NotImplementedError("Sim method not implemented "
"in {}".format(str(self.sim_api)))
# Determine if entity is a phenotype or individual containin... | def search(
self,
id_list: List[str],
negated_ids: Optional[List] = None,
limit: Optional[int] = 100,
taxon_filter: Optional[int]= None,
category_filter: Optional[str]= None,
method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM
)... | Execute a search using sim_api, resolving non-phenotype ids to
phenotype lists then adding them to the profile (eg genes, diseases)
:raises NotImplementedError:
- If sim method or filters are not supported | 3.596495 | 3.350421 | 1.073446 |
if method not in self.sim_api.matchers():
raise NotImplementedError("Sim method not implemented "
"in {}".format(str(self.sim_api)))
is_first_result = True
comparisons = None
reference_phenos = PhenoSimEngine._resolve_nodes_to_... | def compare(self,
reference_ids: List,
query_profiles: List[List],
method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM) -> SimResult | Execute one or more comparisons using sim_api
:param reference_ids: a list of phenotypes or ids that comprise
one or more phenotypes
:param query_profiles: a list of lists of phenotypes or ids
that comprise one or more phenotypes
:... | 2.536028 | 2.424379 | 1.046053 |
pheno_list = []
node_types = get_id_type_map(id_list)
for node in id_list:
if 'phenotype' in node_types[node]:
pheno_list.append(node)
else:
phenotypes = get_objects_for_subject(
subject=node, object_category='... | def _resolve_nodes_to_phenotypes(id_list: List[str]) -> List[str] | Given a list of ids of unknown type, determine which ids
are phenotypes, if the id is not a phenotype, check to
see if it is associated with one or more phenotypes via
the 'has_phenotype' relation
:param id_list: list of ids of any type (curies as strings)
:return: list of phenot... | 3.292482 | 3.199154 | 1.029173 |
db, db_object_id = self._split_prefix(entity)
taxon = normalize_taxon(entity["taxon"]["id"])
vals = [
db,
db_object_id,
entity.get('label'),
entity.get('full_name'),
entity.get('synonyms'),
entity.get('type'),
... | def write_entity(self, entity) | Write a single entity to a line in the output file | 4.378431 | 3.954102 | 1.107314 |
pass | def search(self,
id_list: Iterable,
negated_classes: Iterable,
limit: Optional[int],
method: Optional) -> List[SimResult] | Given an input list of classes, searches for similar lists of classes
and provides a ranked list of matches | 23,094.4375 | 11,415.916016 | 2.023003 |
pass | def filtered_search(self,
id_list: Iterable,
negated_classes: Iterable,
limit: Optional[int],
taxon_filter: Optional,
category_filter: Optional,
method: Optional) -> SimResult | Given an input iterable of classes or individuals,
provides a ranking of similar profiles | 46,685.46875 | 13,824.976563 | 3.376893 |
pass | def compare(self,
reference_ids: Iterable,
query_profiles: Iterable[Iterable],
method: Optional) -> SimResult | Given two lists of entities (classes, individuals),
resolves them to some type (phenotypes, go terms, etc) and
returns their similarity | 77,436.351563 | 42,704.917969 | 1.813289 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.