# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Accessing SRA Controlled Access data
# This notebook explores using GA4GH DRS to access data stored in the cloud for a controlled access dbGaP project - [phs001554](https://www.ncbi.nlm.nih.gov/projects/gap/cgi-bin/study.cgi?study_id=phs001554.v1.p1) "Detection of Colorectal Cancer Susceptibility Loci Using Genome-Wide Sequencing".
#
# A query using the GA4GH Search API on subject and file attributes identifies some DRS ids. 
#
# >Note: The subject and sample data searched through the initial query are on a scrambled copy of the data so that no controlled access records remain intact. Please do not attempt to draw meaningful associations between the subject and sample attributes with the genomic data; this would lead to erroneous conclusions. These examples serve only to illustrate future possibilities, in the hope of identifying ways to make those possibilities real.
# >
# >The sequence files used in this notebook are also under controlled access. You will not be able to access those files unless you have been granted access through dbGaP to the phs001554 study. If you have that access you will also have access to the non-scrambled subject and sample data. If that is the case, we would be pleased to hear of your interest, and the possibility of collaborating to ensure the GA4GH tools described here enable your studies. Please see [form to register interest](https://docs.google.com/forms/d/e/1FAIpQLSfmmc3VKd6ANdzaVMyelT3c9gIWuoS4ZwT0vsqD-o2ZRxJf7A/viewform)

from fasp.search import DataConnectClient
cl = DataConnectClient('https://ga4gh-search-adapter-presto-public.prod.dnastack.com/')
query = '''select acc, sa.sample_id, sra_drs_id, sex, age
from search_cloud.cshcodeathon.gecco_sra_drs_index i
join dbgap_demo.scr_gecco_susceptibility.sample_multi sa on sa.sample_id = i.sample_id
join dbgap_demo.scr_gecco_susceptibility.subject_phenotypes_multi su on su.dbgap_subject_id = sa.dbgap_subject_id
where age between 50 and 55
and affection_status = 'Case'
and file_type = 'cram' limit 10'''
df = cl.runQuery(query, returnType='dataframe')
df

from fasp.loc import SRADRSClient
drsClient = SRADRSClient('https://locate.be-md.ncbi.nlm.nih.gov', public=True)
# extract an example if from the results above
example_id = df.at[3,'sra_drs_id']
# Use DRS to find locations for the file
res_list = drsClient.getObject(example_id)
res_list

# Use custom client method to get a URL to the file in a specified region.

drsClient.getAccessURLRegion(example_id, 's3.us-east-1')


drsClient.getAccessURL(example_id,'d97c4f9ef829579d9e59888deec15d66799af278')

# ### For interest: Compare the same file on Seven Bridges

query2 = "select * from dbgap_demo.scr_gecco_susceptibility.sb_drs_index where sample_id = '117497' and file_type = 'cram'"
cl.runQuery(query2)

from fasp.loc import sbcgcDRSClient
drsClient = sbcgcDRSClient('/Users/forei/.keys/sevenbridges_keys.json', 's3')
drsClient.getObject('5ba922a0e4b0db63859cd973')

# ### For interest: Compare the same file on NCI Data Commons Framework (Gen3)
# To do.



StarcoderdataJupyterScriptsDedupFiltered