Skip to content

Commit

Permalink
add optional separate genomic_id for variantfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
daisieh committed Nov 9, 2022
1 parent c413091 commit ccb7827
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions data/files.sql
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ INSERT INTO alias VALUES('y','Y');
INSERT INTO alias VALUES('mt','MT');
CREATE TABLE variantfile (
id VARCHAR NOT NULL,
genomic_id VARCHAR,
drs_object_id VARCHAR,
indexed INTEGER,
chr_prefix VARCHAR,
Expand Down
8 changes: 7 additions & 1 deletion htsget_server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Contig(ObjectDBBase):
class VariantFile(ObjectDBBase):
__tablename__ = 'variantfile'
id = Column(String, primary_key=True)
genomic_id = Column(String)
indexed = Column(Integer)
chr_prefix = Column(String)
reference_genome = Column(String)
Expand Down Expand Up @@ -130,6 +131,7 @@ def __repr__(self):
result = {
'id': self.id,
'drsobject': self.drs_object_id,
'genomic_id': self.genomic_id,
'indexed': self.indexed,
'chr_prefix': self.chr_prefix,
'reference_genome': self.reference_genome,
Expand Down Expand Up @@ -497,14 +499,18 @@ def get_variantfile(variantfile_id):


def create_variantfile(obj):
# obj = {"id", "reference_genome"}
# obj = {"id", "reference_genome", "genomic_id"}
with Session() as session:
new_variantfile = session.query(VariantFile).filter_by(id=obj['id']).one_or_none()
if new_variantfile is None:
new_variantfile = VariantFile()
new_variantfile.indexed = 0
new_variantfile.chr_prefix = '0'
new_variantfile.id = obj['id']
if "genomic_id" in obj:
new_variantfile.genomic_id = obj['genomic_id']
else:
new_variantfile.genomic_id = obj['id']
new_variantfile.reference_genome = obj['reference_genome']
new_drs = session.query(DrsObject).filter_by(id=obj['id']).one_or_none()
if new_drs is not None:
Expand Down
8 changes: 8 additions & 0 deletions htsget_server/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ paths:
- $ref: '#/components/parameters/idPathParam'
- $ref: '#/components/parameters/forceParam'
- $ref: '#/components/parameters/refGenomeParam'
- $ref: '#/components/parameters/genomicIdParam'
responses:
200:
description: Successfully indexed variant file
Expand Down Expand Up @@ -666,6 +667,13 @@ components:
required: false
schema:
$ref: '#/components/schemas/ReferenceGenome'
genomicIdParam:
in: query
name: genomic_id
description: If specified, sets this variant entity's associated genomic_id. Otherwise default is the same as the id.
required: false
schema:
type: string
readsFormatParam:
in: query
name: format
Expand Down
8 changes: 6 additions & 2 deletions htsget_server/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ def get_variants_data(id_, reference_name=None, format_="VCF", start=None, end=N


@app.route('/variants/<path:id_>/index')
def index_variants(id_=None, force=False, genome='hg38'):
def index_variants(id_=None, force=False, genome='hg38', genomic_id=None):
if not authz.is_site_admin(request):
return {"message": "User is not authorized to index variants"}, 403
if id_ is not None:
varfile = database.create_variantfile({"id": id_, "reference_genome": genome})
params = {"id": id_, "reference_genome": genome}
if genomic_id is not None:
params["genomic_id"] = genomic_id
varfile = database.create_variantfile(params)
if varfile is not None:
if varfile['indexed'] == 1 and not force:
return varfile, 200
Expand Down Expand Up @@ -193,6 +196,7 @@ def search_variants():
htsget_obj['urls'].append(_get_base_url("variant", drs_obj_id, data=False))
htsget_obj['id'] = drs_obj_id
htsget_obj['variantcount'] = count
htsget_obj['genomic_id'] = database.get_variantfile(drs_obj_id)['genomic_id']
htsget_obj['samples'] = database.get_samples_in_drs_objects({'drs_object_ids': [drs_obj_id]})
htsget_obj['reference_genome'] = searchresult['reference_genome'][i]
result['results'].append(htsget_obj)
Expand Down
10 changes: 7 additions & 3 deletions tests/test_htsget_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,21 @@ def test_post_update():


def test_index_variants():
return [('sample.compressed'), ('NA18537'), ('NA20787'), ('multisample_1'), ('multisample_2')]
return [('sample.compressed', None), ('NA18537', None), ('NA20787', None), ('multisample_1', 'HG00096'), ('multisample_2', 'HG00097')]


@pytest.mark.parametrize('sample', test_index_variants())
def test_index_variantfile(sample):
@pytest.mark.parametrize('sample, genomic_id', test_index_variants())
def test_index_variantfile(sample, genomic_id):
url = f"{HOST}/htsget/v1/variants/{sample}/index"
params = {"genome": "hg37"}
if genomic_id is not None:
params["genomic_id"] = genomic_id
#params['force'] = True
response = requests.get(url, params=params, headers=headers)
print(response.text)
assert response.json()["id"] == sample
if genomic_id is not None:
assert response.json()["genomic_id"] == genomic_id


def invalid_start_end_data():
Expand Down

0 comments on commit ccb7827

Please sign in to comment.