Beispiel #1
0
    def _get_table(self, table):
        logger.info(self.organism)
        s = get_session(self.baseurl, self.login)

        query_payload = {
            "answerSpec": {
                "questionName": "GeneQuestions.GenesByTaxonGene",
                "parameters": {"organism": self.organism},
                "viewFilters": [],
                "filters": []
            },
            "formatting": {
                "formatConfig": {
                    "tables": [table],
                    "includeHeader": True,
                    "attachmentType": "plain"
                },
                "format": "tableTabular"
            }
        }

        r = s.post(self.baseurl + "/a/service/answer/report",
                   data=json.dumps(query_payload),
                   headers={'Content-Type': 'application/json'})
        r.raise_for_status()
        return self._parse_table(r.text, table)
Beispiel #2
0
 def _get_json(self, url, params=None):
     logger.info('  retrieving %s' % url)
     s = get_session(self.baseurl, self.login)
     res = s.get(url, verify=True, params=params)
     if "autologin" in res.text or 'Login</title>' in res.text:
         raise RuntimeError(
             "Login failed -- please check user credentials.")
     if (res.ok):
         return res.json()
     else:
         res.raise_for_status()
Beispiel #3
0
 def _get_json(self, url, params=None):
     logger.info('  retrieving %s' % url)
     s = get_session(self.baseurl, self.login)
     # workaround for problematic urlencoding if organism contains '#'
     params = "?organism=" + params['organism'] + \
              "&o-fields=" + params['o-fields']
     res = s.get(url + params, verify=True)
     if "autologin" in res.text or 'Login</title>' in res.text:
         raise RuntimeError("Login failed -- please check user credentials.")
     if(res.ok):
         return res.json()
     else:
         res.raise_for_status()
Beispiel #4
0
 def __init__(self, baseurl, login=None):
     self.baseurl = baseurl
     self.login = parse_login(login)
     self.fields = [
         "organism", "ncbi_tax_id", "is_reference_strain",
         "is_annotated_genome", "URLGenomeFasta", "URLgff"
     ]
     url = (
         '{0}/webservices/OrganismQuestions/GeneMetrics.json?o-fields={1}'
     ).format(self.baseurl, ','.join(self.fields))
     s = get_session(self.baseurl, self.login)
     res = s.get(url, verify=True)
     self.orgs = collections.deque()
     if (res.ok):
         j = res.json()
     else:
         res.raise_for_status()
     for v in j['response']['recordset']['records']:
         item = {}
         for f in v['fields']:
             item[f['name']] = f['value']
         self.orgs.append(item)
Beispiel #5
0
 def __init__(self, baseurl, organism, login=None):
     self.login = parse_login(login)
     self.baseurl = baseurl
     url = ('{0}/webservices/GenomicSequenceQuestions/' +
            'SequencesByTaxon.json' +
            '?organism={1}&o-fields=primary_key').format(
                baseurl, organism.replace('#', "%23"))
     res = self._get_json(url)
     seqids = []
     for v in res['response']['recordset']['records']:
         seqids.append(v['id'])
     logger.info('  needing to retrieve %s seqs for organism %s' %
                 (len(seqids), organism))
     parsed_url = urlparse(baseurl)
     self.baseurl = "%s://%s" % (parsed_url.scheme, parsed_url.netloc)
     project_id = parsed_url.netloc.split('.')[0].capitalize()
     project_id = re.sub('db$', 'DB', project_id)
     payload = {'project_id': project_id, 'ids': '\n'.join(seqids)}
     url = ('{0}/cgi-bin/contigSrt').format(self.baseurl)
     s = get_session(self.baseurl, self.login)
     res = s.post(url, data=payload)
     if not res.ok:
         res.raise_for_status()
     self.out = res.text