def __init__(self):
     requests = CacheControlHelper()
     url = QueryOMIMExtended.API_BASE_URL + "/apiKey"
     session_data = {'apiKey': QueryOMIMExtended.API_KEY, 'format': 'json'}
     r = requests.post(url, data=session_data)
     assert 200 == r.status_code
     self.cookie = r.cookies
Exemple #2
0
    def send_query_post(handler, params, retmax=1000):

        requests = CacheControlHelper()
        url_str = QueryNCBIeUtils.API_BASE_URL + '/' + handler
        params['retmax'] = str(retmax)
        params['retmode'] = 'json'
        #        print(url_str)
        try:
            res = requests.post(url_str,
                                data=params,
                                timeout=QueryNCBIeUtils.TIMEOUT_SEC)
        except requests.exceptions.Timeout:
            print('HTTP timeout in QueryNCBIeUtils.py; URL: ' + url_str,
                  file=sys.stderr)
            time.sleep(
                1)  ## take a timeout because NCBI rate-limits connections
            return None
        except requests.exceptions.ConnectionError:
            print('HTTP connection error in QueryNCBIeUtils.py; URL: ' +
                  url_str,
                  file=sys.stderr)
            time.sleep(
                1)  ## take a timeout because NCBI rate-limits connections
            return None
        status_code = res.status_code
        if status_code != 200:
            print('HTTP response status code: ' + str(status_code) +
                  ' for URL:\n' + url_str,
                  file=sys.stderr)
            res = None
        return res
Exemple #3
0
 def get_single_ticket(tgt):
     params = {'service': "http://umlsks.nlm.nih.gov"}
     h = {
         "Content-type": "application/x-www-form-urlencoded",
         "Accept": "text/plain",
         "User-Agent": "python"
     }
     requests = CacheControlHelper()
     r = requests.post(tgt, data=params, headers=h)
     st = r.text
     return st
Exemple #4
0
    def uniprot_id_to_reactome_pathways(uniprot_id):
        """returns a ``set`` of reactome IDs of pathways associated with a given string uniprot ID

        :param uniprot_id: a ``str`` uniprot ID, like ``"P68871"``
        :returns: a ``set`` of string Reactome IDs
        """

        payload = {
            'from': 'ACC',
            'to': 'REACTOME_ID',
            'format': 'tab',
            'query': uniprot_id
        }
        contact = "*****@*****.**"
        header = {'User-Agent': 'Python %s' % contact}

        requests = CacheControlHelper()

        try:
            url = QueryUniprot.API_BASE_URL
            res = requests.post(QueryUniprot.API_BASE_URL,
                                data=payload,
                                headers=header)
        except requests.exceptions.Timeout:
            print(url, file=sys.stderr)
            print('Timeout in QueryUniprot for URL: ' +
                  QueryUniprot.API_BASE_URL,
                  file=sys.stderr)
            return None
        except KeyboardInterrupt:
            sys.exit(0)
        except BaseException as e:
            print(url, file=sys.stderr)
            print('%s received in QueryUniprot for URL: %s' % (e, url),
                  file=sys.stderr)
            return None
        status_code = res.status_code
        if status_code != 200:
            print(QueryUniprot.API_BASE_URL, file=sys.stderr)
            print('Status code ' + str(status_code) + ' for url: ' +
                  QueryUniprot.API_BASE_URL,
                  file=sys.stderr)
            return None


#        assert 200 == res.status_code
        res_set = set()
        for line in res.text.splitlines():
            field_str = line.split("\t")[1]
            if field_str != "To":
                res_set.add(field_str)
        return res_set
Exemple #5
0
 def get_ticket_gen():
     # params = {'username': self.username,'password': self.password}
     params = {'apikey': QueryUMLS.api_key}
     h = {
         "Content-type": "application/x-www-form-urlencoded",
         "Accept": "text/plain",
         "User-Agent": "python"
     }
     requests = CacheControlHelper()
     r = requests.post(QueryUMLS.Ticket_URL + QueryUMLS.auth_endpoint,
                       data=params,
                       headers=h)
     response = fromstring(r.text)
     ## extract the entire URL needed from the HTML form (action attribute) returned - looks similar to https://utslogin.nlm.nih.gov/cas/v1/tickets/TGT-36471-aYqNLN2rFIJPXKzxwdTNC5ZT7z3B3cTAKfSc5ndHQcUxeaDOLN-cas
     ## we make a POST call to this URL in the getst method
     tgt = response.xpath('//form/@action')[0]
     return tgt
Exemple #6
0
    def query_mesh_id_to_uniprot_ids_desc(mesh_id):
        ent = 'disease'
        id = 'mesh'
        STR = "c1.MESH = '"
        intfield = mesh_id
        seq = ( """
        DEFINE
        c0='/data/gene_disease_summary',
	c1='/data/diseases',
	c2='/data/genes',
	c4='/data/sources'
        ON
           'http://www.disgenet.org/web/DisGeNET'
        SELECT
        c1 (diseaseId, name, diseaseClassName, STY, MESH, OMIM, type ),
	c2 (geneId, symbol,   uniprotId, description, pantherName ),
	c0 (score, EI, Npmids, Nsnps)

        FROM
            c0
        WHERE
            (
                """ + STR +  mesh_id+"""'
            AND
                c4 = 'ALL'
            )
        ORDER BY
            c0.score DESC""" ); #

        binary_data = seq.encode('utf-8')
        url_str = QueryDisGeNet.SPARQL_ENDPOINT_URL
        requests = CacheControlHelper()

        try:
            res = requests.post(url_str, data=binary_data, timeout=QueryDisGeNet.TIMEOUT_SEC)
        except requests.exceptions.Timeout:
            print(url_str, sys.stderr)
            print('Timeout in QueryDisGeNet for URL: ' + url_str, file=sys.stderr)
            return dict()
        except BaseException as e:
            print(url_str, file=sys.stderr)
            print('%s received in QueryDisGeNet for URL: %s' % (e, url_str), file=sys.stderr)
            return None

        status_code = res.status_code

        if status_code != 200:
            print(url_str, sys.stderr)
            print('Status code ' + str(status_code) + ' for url: ' + url_str, file=sys.stderr)
            return dict()

        if len(res.content) == 0:
            print(url_str, file=sys.stderr)
            print('Empty response from URL!', file=sys.stderr)
            return dict()

        ret_data_df = pandas.read_csv(io.StringIO(res.content.decode('utf-8')), sep='\t').head(QueryDisGeNet.MAX_GENES_FOR_DISEASE)
        uniprot_ids_list = ret_data_df['c2.uniprotId'].tolist()
        gene_names_list = ret_data_df['c2.symbol'].tolist()
        ret_dict = dict(list(zip(uniprot_ids_list, gene_names_list)))
        for prot in ret_dict.copy().keys():
            if type(prot)==str and prot != "null":
                if '.' in prot or ';' in prot:
                    gene = ret_dict[prot]
                    del ret_dict[prot]
                    prot.replace('.', '')
                    prots_to_add = prot.split(';')
                    if len(prots_to_add) > QueryDisGeNet.MAX_PROTS_FOR_GENE:
                        prots_to_add = prots_to_add[0:QueryDisGeNet.MAX_PROTS_FOR_GENE]
                        dict_add = dict()
                        for prot_name in prots_to_add:
                            if type(prot_name) == str and prot_name != "null":
                                dict_add[prot_name] = gene
                        ret_dict.update(dict_add)
            else:  ## this is a math.nan
                del ret_dict[prot]
        return(ret_dict)