def test_pheno_assocs(): q = GolrAssociationQuery(subject=TWIST_ZFIN, object_category='phenotype') print("Q={}".format(q)) print("Q.subject={}".format(q.subject)) print("Q.evidec={}".format(q.evidence)) params = q.solr_params() print("PARAMS={}".format(params)) results = q.exec() print("RES={}".format(results)) assert len(results) > 0
def setup_class(self): self.golr_query = GolrAssociationQuery() # Mock the PySolr search function to # return our test docs input_fh = os.path.join(os.path.dirname(__file__), 'resources/solr/mock-solr-evidence.json') input_docs = json.load(open(input_fh)) self.pysolr_results = pysolr.Results(input_docs) self.golr_query.solr.search = MagicMock( return_value=self.pysolr_results)
def get_evidence(statementId): if statementId.startswith('biolink:'): statementId = statementId[8:] evidences = [] results = GolrAssociationQuery(id=statementId).exec() associations = results['associations'] for association in associations: publications = association.get('publications', None) if publications != None: for publication in publications: evidence = {} evidence['id'] = publication.get('id', '') evidence['label'] = publication.get('label', 'PubMed article') evidence['date'] = '0000-0-00' evidences.append(evidence) # If the statement is found but has no associated publication, give a # generic response for evidence. if len(evidences) == 0 and len(associations) != 0: evidence = {} evidence['id'] = '' evidence['date'] = '0000-00-00' evidence[ 'label'] = 'From the Monarch Initiative - No further supporting text' evidences.append(evidence) print(evidences) return jsonify(evidences)
def test_clinical_modifiers(): """ Test that clinical modifiers show up in the GolrAssociationQuery.exec() object when present in the input solr document """ manager = GolrAssociationQuery() manager.solr = pysolr.Solr(url="mock_solr", timeout=10) input_fh = os.path.join(os.path.dirname(__file__), 'resources/solr/input/clinical-mod-doc.json') input_docs = json.load(open(input_fh)) expected_fh = os.path.join(os.path.dirname(__file__), 'resources/solr/expected/clinical-mod.json') expected_obj = json.load(open(expected_fh)) manager.solr.search = MagicMock(return_value=pysolr.Results(input_docs)) results = manager.exec() assert json.dumps(expected_obj, sort_keys=True) == \ json.dumps(results, default=lambda obj: getattr(obj, '__dict__', str(obj)), sort_keys=True)
class TestEvidenceTable(): """ Functional test for obograph_to_assoc_results which converts an evidence graph to an association results object """ @classmethod def setup_class(self): self.golr_query = GolrAssociationQuery() # Mock the PySolr search function to # return our test docs input_fh = os.path.join(os.path.dirname(__file__), 'resources/solr/mock-solr-evidence.json') input_docs = json.load(open(input_fh)) self.pysolr_results = pysolr.Results(input_docs) self.golr_query.solr.search = MagicMock( return_value=self.pysolr_results) @classmethod def teardown_class(self): self.manager = None def test_obograph_to_assoc_results(self): # Hits the mock solr manager in setup_class results = self.golr_query.exec() assoc = results['associations'][0] if len( results['associations']) > 0 else {} eg = {'graphs': [assoc.get('evidence_graph')]} digraph = convert_json_object(eg, reverse_edges=False)['graph'] association_results = obograph_to_assoc_results(digraph) results = json.dumps( association_results, default=lambda obj: getattr(obj, '__dict__', str(obj))) expected_fh = os.path.join(os.path.dirname(__file__), 'resources/expected/test-evidence.json') expected_results = json.dumps(json.load(open(expected_fh))) assert results == expected_results
def get_types(): frequency = {semanticGroup: 0 for semanticGroup in semantic_mapping.keys()} results = GolrAssociationQuery( rows=0, facet_fields=['subject_category', 'object_category']).exec() facet_counts = results['facet_counts'] subject_category = facet_counts['subject_category'] object_category = facet_counts['object_category'] for key in subject_category: frequency[monarch_to_UMLS(key)] += subject_category[key] for key in object_category: frequency[monarch_to_UMLS(key)] += object_category[key] return jsonify([{ 'id': c, 'idmap': None, 'frequency': f } for c, f in frequency.items()])
def get_statements(): s = getlist('s') relations = request.args.get('relations', None) t = getlist('t') keywords = request.args.get('keywords', None) semanticGroups = request.args.get('semanticGroups', None) pageSize = int(request.args.get('pageSize', 1)) pageNumber = int(request.args.get('pageNumber', 1)) validatePagination(pageNumber, pageSize) validateIdList(s) if t == None or len(t) == 0: t = None # query 'source' set as subject qSub = GolrAssociationQuery( subjects=s, objects=t, object_category=build_categories(semanticGroups), relation=get_relation( relations ), # Currently only first relation in the list, if any, is taken? rows=pageSize, start=getStartIndex(pageNumber, pageSize), non_null_fields=['subject', 'relation', 'object']) subStmts = qSub.exec() # query 'source' set as subject qObj = GolrAssociationQuery( objects=s, subjects=t, subject_category=build_categories(semanticGroups), relation=get_relation( relations ), # Currently only first relation in the list, if any, is taken? rows=pageSize, start=getStartIndex(pageNumber, pageSize), non_null_fields=['subject', 'relation', 'object']) objStmts = qObj.exec() # Merge two dictionaries results = {**subStmts, **objStmts} print("statement results: " + str(len(results)) + " items found?") key_pairs = {'id': 'id', 'name': 'label'} statements = [] for d in results['associations']: try: statement = {} statement['id'] = 'biolink:' + d[ 'id'] # add the biolink: prefix to statement id's statement['object'] = { k1: d['object'][k2] for k1, k2 in key_pairs.items() } statement['object'] = get_concept(statement['object']['id']) statement['subject'] = { k1: d['subject'][k2] for k1, k2 in key_pairs.items() } statement['subject'] = get_concept(statement['subject']['id']) statement['predicate'] = { k1: d['relation'][k2] for k1, k2 in key_pairs.items() } statements.append(statement) except: pass return jsonify(statements)
def main(): """ Wrapper for OGR """ parser = argparse.ArgumentParser( description='Command line interface to python-ontobio.golr library' """ Provides command line interface onto the ontobio.golr python library, a high level abstraction layer over Monarch and GO solr indices. """, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-A', '--associations', dest='associations', action='store_true', default=False, help='Path to output file') parser.add_argument('-s', '--settings', type=str, help='Path to config file') parser.add_argument('-o', '--outfile', type=str, required=False, help='Path to output file') parser.add_argument('-f', '--facets', type=str, required=False, help='Facet fields: comma-delimited') parser.add_argument('-q', '--fq', type=json.loads, default={}, required=False, help='Facet query (solr fq) - should be json') parser.add_argument( '-Q', '--qargs', type=json.loads, default={}, required=False, help='Query to be passed directly to python golr_associations query') parser.add_argument('-l', '--legacy_solr', dest='legacy_solr', action='store_true', default=False, help='Set for legacy solr schema (solr3 golr)') parser.add_argument('-u', '--url', type=str, required=False, help='Solr URL. E.g. http://localhost:8983/solr/golr') parser.add_argument('-v', '--verbosity', default=0, action='count', help='Increase output verbosity') parser.add_argument('search', type=str, help='Search terms') args = parser.parse_args() if args.verbosity >= 2: logging.basicConfig(level=logging.DEBUG) elif args.verbosity == 1: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.WARNING) logging.info("Welcome!") facets = [] if args.facets is not None: facets = args.facets.split(",") config = None if args.settings is not None: from ontobio.config import load_config config = load_config(args.settings) results = None if args.associations: q = None if args.search != '%': q = args.search q = GolrAssociationQuery(q=q, is_go=args.legacy_solr, fq=args.fq, facet_fields=facets, url=args.url) results = q.exec() #print("RESULTS={}".format(results)) docs = results['associations'] print("RESULTS: {}".format(len(docs))) for r in docs: print(str(r)) else: logging.info("FQ={}".format(args.fq)) q = GolrSearchQuery(args.search, is_go=args.legacy_solr, fq=args.fq, facet_fields=facets, url=args.url) results = q.exec() #print("RESULTS={}".format(results)) docs = results['docs'] print("RESULTS: {}".format(len(docs))) for r in docs: print(" {} '{}' {} // {}".format(r['id'], r['label'], r['score'], r['category'])) if len(facets) > 0: #from collections import OrderedDict fcs = results['facet_counts'] for f in facets: d = fcs[f] print(str(d)) print("## FACET: {}".format(f)) for k, v in sorted(d.items(), key=lambda t: -t[1]): print(" {:5d}: {}".format(v, k))