コード例 #1
0
class OpenTargetClientTest(unittest.TestCase):
    _AUTO_GET_TOKEN = 'auto'

    def setUp(self):

        self.client = OpenTargetsClient()
        self.http2_client = OpenTargetsClient(use_http2=True)
        self.auth_client = OpenTargetsClient(
            auth_app_name='test',
            auth_secret='test',
        )

    def tearDown(self):
        self.client.close()

    def testSearchTargetCorrectResult(self):
        target_symbol = 'BRAF'
        response = self.client.search(target_symbol)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['type'], 'search-object-target')
        self.assertEqual(result['id'], 'ENSG00000157764')
        self.assertEqual(result['data']['approved_symbol'], target_symbol)

    def testSearchTargetFetchAllResults(self):
        target_symbol = 'BRAF'
        response = self.client.search(target_symbol)
        total_results = len(response)
        self.assertGreater(total_results, 0)
        c = 0
        for i in response:
            c += 1
        self.assertEqual(total_results, c)

    def testSearchTargetFetchAllResultsAuth(self):
        target_symbol = 'BRAF'
        response = self.auth_client.search(target_symbol)
        total_results = len(response)
        self.assertGreater(total_results, 0)
        c = 0
        for i in response:
            c += 1
        self.assertEqual(total_results, c)

    def testSearchTargetCorrectResultHTTP2(self):
        target_symbol = 'BRAF'
        response = self.http2_client.search(target_symbol)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['type'], 'search-object-target')
        self.assertEqual(result['id'], 'ENSG00000157764')
        self.assertEqual(result['data']['approved_symbol'], target_symbol)

    def testSearchTargetFetchAllResultsHTTP2(self):
        target_symbol = 'BRAF'
        response = self.http2_client.search(target_symbol)
        total_results = len(response)
        self.assertGreater(total_results, 0)
        c = 0
        for i in response:
            c += 1
        self.assertEqual(total_results, c)

    def testSearchDiseaseCorrectResult(self):
        disease_label = 'cancer'
        response = self.client.search(disease_label)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['type'], 'search-object-disease')
        self.assertEqual(result['id'], 'EFO_0000311')

    # #this takes a lot to run
    # def testSearchDiseaseFetchAllResults(self):
    #     disease_label = 'cancer'
    #     response = self.client.search(disease_label, size = 100)
    #     total_results = len(response)
    #     self.assertGreater(total_results,0)
    #     c=0
    #     for i in response:
    #         c+=1
    #     self.assertEqual(total_results, c)
    #     print(total_results, c)

    def testGetAssociation(self):
        association_id = "ENSG00000157764-EFO_0005803"
        response = self.client.get_association(association_id)
        self.assertEquals(len(response), 1)
        self.assertEquals(association_id, response[0]['id'])

    def testFilterAssociations(self):
        response = self.client.filter_associations()
        self.assertGreater(len(response), 0)
        total = response.info.total
        response.filter(target='ENSG00000157764')
        self.assertLess(len(response), total)
        total = response.info.total
        response.filter(direct=True)
        self.assertLess(len(response), total)
        total = response.info.total
        response.filter(scorevalue_min=0.2)
        self.assertLess(len(response), total)
        total = response.info.total
        response.filter(therapeutic_area='efo_0000701')
        self.assertLess(len(response), total)
        results = []
        for i, r in enumerate(response):
            print(i, r['id'], r['association_score']['overall'],
                  r['disease']['efo_info']['label'])
            results.append(r)
        response_multi = self.client.filter_associations(
            target='ENSG00000157764',
            direct=True,
            scorevalue_min=0.2,
            therapeutic_area='efo_0000701')
        self.assertEqual(len(response_multi), response.info.total)
        for i, r in enumerate(response_multi):
            self.assertEqual(results[i]['id'], r['id'])
        response_chained = self.client.filter_associations().filter(
            target='ENSG00000157764').filter(direct=True).filter(
                therapeutic_area='efo_0000701').filter(scorevalue_min=0.2)
        self.assertEqual(len(response_chained), response.info.total)
        for i, r in enumerate(response_chained):
            self.assertEqual(results[i]['id'], r['id'])

    def testGetAssociationsForTarget(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(target_symbol,
                                                           size=30)
        self.assertGreater(len(response), 0)
        for i, result in enumerate(response):
            self.assertEqual(result['target']['gene_info']['symbol'],
                             target_symbol)
            if i > 90:
                break

    def testGetAssociationsForDisease(self):
        disease_label = 'cancer'
        response = self.client.get_associations_for_disease(disease_label)
        self.assertGreater(len(response), 0)
        for result in response:
            self.assertEqual(result['disease']['efo_info']['label'],
                             disease_label)

    @unittest.expectedFailure
    def testGetEvidence(self):
        evidence_id = "03fba0599655b9040012b29cf0de8060"
        response = self.client.get_evidence(evidence_id)
        self.assertEquals(len(response), 1)
        self.assertEquals(evidence_id, response[0]['id'])

    def testFilterEvidence(self):
        response = self.client.filter_evidence()
        self.assertGreater(len(response), 0)

    def testGetEvidenceForTarget(self):
        target_symbol = 'BRAF'
        response = self.client.get_evidence_for_target(target_symbol,
                                                       size=1000)
        self.assertGreater(len(response), 0)
        for i, result in enumerate(response):
            self.assertEqual(result['target']['gene_info']['symbol'],
                             target_symbol)
            if i > 100:
                break

    def testGetSimilarTargets(self):
        target_symbol = 'BRAF'
        response = self.client.get_similar_target(target_symbol)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['subject']['label'], target_symbol)
        self.assertEqual(result['object']['label'], 'KRAS')

    def testGetSimilarDisease(self):
        disease_label = 'ulcerative colitis'
        response = self.client.get_similar_disease(disease_label)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['subject']['label'], disease_label)
        self.assertEqual(result['object']['label'], "Crohn's disease")

    def testGetEvidenceForDisease(self):
        disease_label = 'medulloblastoma'
        response = self.client.get_evidence_for_disease(disease_label)
        self.assertGreater(len(response), 0)
        result = next(response)
        self.assertEqual(result['disease']['efo_info']['label'], disease_label)

    def testSerialiseToJson(self):
        target_symbol = 'BRAF'
        '''test iterable version'''
        response = self.client.get_associations_for_target(target_symbol)
        items = len(response)
        self.assertGreater(len(response), 0)
        json_output = response.to_json()
        parsed_json = [json.loads(i) for i in json_output]
        self.assertEqual(items, len(parsed_json))
        '''test non iterable version'''
        response = self.client.get_associations_for_target(target_symbol)
        items = len(response)
        self.assertGreater(len(response), 0)
        json_output = response.to_json(iterable=False)
        parsed_json = json.loads(json_output)
        self.assertEqual(items, len(parsed_json))

    def testResultToPandasDataFrame(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(target_symbol)
        items = len(response)
        self.assertGreater(len(response), 0)
        dataframe = response.to_dataframe()
        self.assertEqual(len(dataframe), items)

    def testResultToPandasCSV(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(
            target_symbol,
            fields=[
                'association_score.*', 'target.gene_info.symbol',
                'disease.efo_info.*'
            ])
        items = len(response)
        self.assertGreater(len(response), 0)
        csv = response.to_csv()
        filename = 'braf_associations.csv'
        open(filename, 'wb').write(csv.encode('utf-8'))
        self.assertEqual(len(csv.split('\n')), items + 2)
        self.assertTrue(os.path.isfile(filename))
        os.remove(filename)

    def testResultToPandasExcel(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(
            target_symbol,
            fields=[
                'association_score.*', 'target.gene_info.symbol',
                'disease.efo_info.*'
            ])
        self.assertGreater(len(response), 0)
        filename = 'braf_associations.xls'
        response.to_excel(filename)
        self.assertTrue(os.path.isfile(filename))
        os.remove(filename)

    def testResultToFile(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(
            target_symbol,
            fields=[
                'association_score.*', 'target.gene_info.symbol',
                'disease.efo_info.*'
            ])
        self.assertGreater(len(response), 0)
        filename = 'braf_associations.json.gz'
        response.to_file(filename)
        self.assertTrue(os.path.isfile(filename))
        os.remove(filename)

    def testSerialiseToObject(self):
        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(target_symbol)
        items = len(response)
        self.assertGreater(len(response), 0)
        obj_output = list(response.to_object())
        for i, result in enumerate(obj_output):
            self.assertIsNotNone(result.target.id)
        self.assertEqual(items, i + 1)

    def testGetStats(self):
        response = self.client.get_stats()
        self.assertEquals(len(response), 0)

    def testAutodetectPost(self):
        self.assertFalse(
            Connection._auto_detect_post({'target': ['ENSG00000157764']}))
        self.assertTrue(
            Connection._auto_detect_post({
                'target': [
                    'ENSG00000157764',
                    'ENSG00000171862',
                    'ENSG00000136997',
                    'ENSG00000012048',
                    'ENSG00000139618',
                ]
            }))

    def testGetToPost(self):
        response = self.client.conn.get('/platform/public/association/filter',
                                        params={
                                            'target': [
                                                'ENSG00000157764',
                                                'ENSG00000171862',
                                                'ENSG00000136997',
                                                'ENSG00000012048',
                                                'ENSG00000139618',
                                            ]
                                        })
        self.assertGreater(len(response), 0)

    def testCustomScore(self):
        def score_with_datatype_subset(datatypes, results):
            for r in results:
                datatype_scores = r['association_score']['datatypes']
                filtered_scores = [datatype_scores[dt] for dt in datatypes]
                custom_score = HarmonicSumScorer.harmonic_sum(filtered_scores)
                if custom_score:
                    yield (round(custom_score, 3), r['disease']['id'],
                           dict(zip(datatypes, filtered_scores)))

        target_symbol = 'BRAF'
        response = self.client.get_associations_for_target(target_symbol)
        self.assertGreater(len(response), 0)
        for i, filtered_data in enumerate(
                score_with_datatype_subset(
                    ['genetic_association', 'known_drug', 'somatic_mutation'],
                    response)):
            self.assertGreater(filtered_data[0], 0.)

        self.assertLess(i, len(response))

    def testGetAvailableEndpoints(self):
        endpoints = self.client.conn.get_api_endpoints()
        self.assertTrue('/platform/public/search' in endpoints)

    def testGetEndpointDocs(self):
        docs = self.client.conn.api_endpoint_docs('/platform/public/search')
        self.assertGreater(len(docs['get']['parameters']), 0)

    def testPing(self):
        response = self.client.conn.ping()
        if isinstance(response, bool):
            self.assertTrue(response)
        else:
            self.assertIsNotNone(response)
コード例 #2
0
ファイル: Client.py プロジェクト: peiyaoli/BioClients
    ids = []
    if args.ifile:
        fin = open(args.ifile)
        while True:
            line = fin.readline()
            if not line: break
            if line.rstrip(): ids.append(line.rstrip())
        fin.close()
    elif args.ids:
        ids = re.split(r'[,\s]+', args.ids)
    if ids: logging.info('Input IDs: {}'.format(len(ids)))

    otclient = OpenTargetsClient()

    logging.debug(otclient.get_stats().info)

    if args.op == 'searchAssociations':
        if not ids: parser.error('--i or --ids required.')
        opentargets.Utils.SearchAssociations(otclient, ids, args.idtype,
                                             args.minscore, args.skip,
                                             args.nmax, fout)

    elif args.op == 'getEvidence':
        parser.error('Unimplemented operation: {}'.format(args.op))
        if not ids: parser.error('--i or --ids required.')
        #opentargets.Utils.Target2Disease_Evidence(otclient, tid, args.did, fout)

    else:
        parser.error('Invalid operation: {}'.format(args.op))
コード例 #3
0
def ot_version():
    ot = OpenTargetsClient()
    stats = ot.get_stats()
    return str(stats.info['data_version'])