Skip to content

Commit

Permalink
Merge pull request #2 from grisslab/1-add-option-to-select-type-of-do…
Browse files Browse the repository at this point in the history
…wnloaded-expression-values

1 add option to select type of downloaded expression values
  • Loading branch information
alexgrent authored Jan 25, 2024
2 parents 8191724 + d89a9a2 commit 3cdc3d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/grein_loader/load_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
LOGGER = logging.getLogger(__name__)


def load_dataset(gse_id: str) -> Tuple[dict, dict, pandas.DataFrame]:
def load_dataset(gse_id: str, download_type: str="RAW") -> Tuple[dict, dict, pandas.DataFrame]:
""" Loads a dataset from GREIN.
:param: gse_id: The dataset's GSE id.
:param: gse_id: The dataset's GSE id, download_type: The type of data to download for expression value, either RAW or NORMALIZED
:type: gse_id: str
:return: description, metadata, count_matrix of the GREIN dataset
:rtype: description:dict, metadata:dictionary, count_matrix:pandas dataframe
"""
if download_type != "RAW" and download_type != "NORMALIZED":
LOGGER.error("Invalid download_type passed. Value must either by 'RAW' or 'NORMALIZED'.")
raise ValueError("Invalid download_type passed. Value must either by 'RAW' or 'NORMALIZED'.")

payloads = utils.GreinLoaderUtils(gse_id)
# create the unique random string used later for nonce parameter in url
n = utils.GreinLoaderUtils.get_random_url_string_parameter()
Expand Down Expand Up @@ -157,7 +161,7 @@ def load_dataset(gse_id: str) -> Tuple[dict, dict, pandas.DataFrame]:
"Origin": "http://www.ilincs.org",
"Referer": "http://www.ilincs.org/apps/grein/?gse=" + gse_id
},
data=payloads.description_formdata(100))
data=payloads.description_formdata(100)) # Bruh wtf moment ???
description_r.raise_for_status()
except requests.exceptions.HTTPError as err:
LOGGER.error(f"Dataset description for {gse_id} not received: ", err)
Expand Down Expand Up @@ -212,6 +216,14 @@ def load_dataset(gse_id: str) -> Tuple[dict, dict, pandas.DataFrame]:
line_content = line.decode()
if "ACK" in line_content:
break

# in case method parameter is set to normalized, different request is send
if download_type == "NORMALIZED":
try:
xhr_send_r = s.post(xhr_send_url, data=payloads.count_matrix_normalized())
except requests.exceptions.HTTPError as err:
LOGGER.error("Streaming error for normailzed count matrix", err)
raise GreinLoaderException("Streaming error for normailzed count matrix", err)

# requesting count matrix
try:
Expand Down Expand Up @@ -323,4 +335,3 @@ def _generate_metadata_formdata(n_columns, no_samples=100):
n = n+1
raw_form += raw_utils.raw_form_end(no_samples)
return raw_form

3 changes: 3 additions & 0 deletions src/grein_loader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def metadata_labels_parameter(self):
def count_matrix_parameter(self):
return '["18#0|m|{\\"method\\":\\"update\\",\\"data\\":{\\"counts_rows_selected\\":[],\\"counts_rows_current\\":[],\\"counts_rows_all\\":[],\\"counts_state\\":null,\\"counts_search\\":\\"\\",\\"counts_cell_clicked\\":{},\\".clientdata_output_downloadcounts_hidden\\":false}}"]'

def count_matrix_normalized(self):
return '["19#0|m|{\\"method\\":\\"update\\",\\"data\\":{\\"counts_choice\\":\\"Normalized\\"}}"]'

def raw_form_start(self):
return "draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false"

Expand Down

0 comments on commit 3cdc3d7

Please sign in to comment.