Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #26 test cases and uri code updated #31

Merged
merged 11 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pysradb/basedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

import pandas as pd
import os

from .utils import _extract_first_field

Expand All @@ -24,7 +25,8 @@ def __init__(self, sqlite_file):

def open(self):
"""Open sqlite connection."""
self.db = sqlite3.connect(self.sqlite_file)
# Originally sqlite3.connect(self.sqlite_file)
self.db = sqlite3.connect("file:{}?mode=ro".format(self.sqlite_file), uri=True)
self.db.text_factory = str

def close(self):
Expand Down
3 changes: 2 additions & 1 deletion pysradb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def _print_save_df(df, saveto=None):
to_print = []
for line in to_print_split:
to_print.append(line.lstrip())
print(("{}".format(os.linesep)).join(to_print))
sys.stdout.write(("{}".format(os.linesep)).join(to_print))
# print(("{}".format(os.linesep)).join(to_print))


def _check_sradb_file(db):
Expand Down
4 changes: 2 additions & 2 deletions pysradb/sradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _verify_srametadb(filepath):
db = BASEdb(filepath)
except:
print(
"{} not a valid SRAmetadb.sqlite file.\n".format(filepath)
"{} not a valid SRAmetadb.sqlite file or path.\n".format(filepath)
+ "Please download one using `pysradb metadb`."
)
sys.exit(1)
Expand All @@ -177,8 +177,8 @@ def __init__(self, sqlite_file):


"""
super(SRAdb, self).__init__(sqlite_file)
_verify_srametadb(sqlite_file)
super(SRAdb, self).__init__(sqlite_file)
self._db_type = "SRA"
self.valid_in_acc_type = [
"SRA",
Expand Down
1 change: 0 additions & 1 deletion pysradb/sraweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ def get_esummary_response(self, db, term, usehistory="y"):
if isinstance(term, list):
term = " OR ".join(term)
payload += [("term", term)]

request = requests.get(self.base_url["esearch"], params=OrderedDict(payload))
esearch_response = request.json()
if "esummaryresult" in esearch_response:
Expand Down
57 changes: 57 additions & 0 deletions tests/_test_sradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from pysradb import SRAdb
from pysradb.filter_attrs import guess_cell_type, guess_tissue_type, guess_strain_type
from sqlite3 import OperationalError


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -77,6 +78,10 @@ def test_all_row_counts(sradb_connection):
assert sradb_connection.all_row_counts().loc["metaInfo", "count"] == 2


def test_all_row_counts2(sradb_connection):
assert len(sradb_connection.all_row_counts()) == 13


def test_sra_metadata(sradb_connection):
df = sradb_connection.sra_metadata("SRP017942")
assert df["experiment_accession"][0] == "SRX217027"
Expand All @@ -94,11 +99,29 @@ def test_search(sradb_connection):
assert len(df.index)


def test_search2(sradb_connection):
df = sradb_connection.search_sra(
'"salivary microbiome" AND "diabetes mellitus"', detailed=True
)
assert "SRP241848" in df["study_accession"].to_list()


def test_search_by_expt_id(sradb_connection):
df = sradb_connection.search_by_expt_id("SRX1254413")
assert df.study_name.tolist()[0] == "GSE73136"


def test_search_by_expt_id2(sradb_connection):
srx_id = "SRX116363"
df_expt = sradb_connection.search_by_expt_id(srx_id)
sra_id = df_expt["submission_accession"].loc[0]
df = sradb_connection.sra_metadata(sra_id)
connected_srp = sradb_connection.srx_to_srp("SRX116363").iloc[0, 1]
assert (srx_id in df["experiment_accession"].to_list()) and (
connected_srp == "SRP010374"
)


# def test_download_fasp(sradb_connection):
# df = sradb_connection.sra_metadata("SRP098789")
# df = df[df.experiment_accession == "SRX2536403"]
Expand Down Expand Up @@ -148,3 +171,37 @@ def test_strain_type(sradb_connection):
"s288c",
"s288c",
]


def test_srp_to_srx(sradb_connection):
assert len(sradb_connection.srp_to_srx("SRP082570")) == 14


def test_srp_to_srr(sradb_connection):
df = sradb_connection.srp_to_srr("SRP091987")
assert sorted(list(df["run_accession"])[:3]) == [
"SRR4447104",
"SRR4447105",
"SRR4447106",
]


def test_srp_to_gse(sradb_connection):
gse_id = sradb_connection.srp_to_gse("SRP050443").iloc[0, 1]
df = sradb_connection.gse_to_gsm(gse_id)
assert "GSM1557451" in df["experiment_alias"].to_list()


def test_gsm_to_gse(sradb_connection):
df = sradb_connection.gsm_to_gse(["GSM1020651", "GSM1020664", "GSM1020771"])
assert set(list(df["study_alias"])) == {"GSE41637"}


def test_srs_to_gsm(sradb_connection):
df = sradb_connection.srs_to_gsm("SRS1757470")
assert "GSM2358940" == df.iloc[0, 1]


@pytest.mark.xfail(raises=ValueError)
def test_wrong_input_metadata(sradb_connection):
df = sradb_connection.sra_metadata("should_throw_error")
20 changes: 20 additions & 0 deletions tests/test_sradb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Tests for sradb.py
"""

import os
import pytest
from pysradb import SRAdb
from pysradb.filter_attrs import guess_cell_type, guess_tissue_type, guess_strain_type
from sqlite3 import OperationalError


def test_not_valid_file():
"""Test to check for error if file is either not
present or not a valid sqlite file"""
path = "SRAmetadb.sqlite"
try:
db = SRAdb(path)
except SystemExit:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand which case would a SystemExit error arise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case if a file does not exist, sqlite3.connect() (line 27) in basedb.py will throw an Operational error which will be caught in except: statement (line 152 in sradb.py) which will eventually lead to a sys.exit(1) on line 157 in sradb.py. This is the SystemExit that my function would catch.

assert os.path.isfile(path) == False
except OperationalError:
assert True