Skip to content
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
61 changes: 61 additions & 0 deletions tools/flexynesis/fetch_cbioportal_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python

import argparse
import os

from flexynesis.utils import CBioPortalData


def main():
parser = argparse.ArgumentParser(description="Fetch and prepare cBioPortal data for Flexynesis.")
parser.add_argument("--study_id", required=True, help="cBioPortal study ID (e.g., 'brca_tcga')")
parser.add_argument("--data_types", required=True, help="Comma-separated list of data types (e.g., 'clin,mut,omics')")
parser.add_argument("--mapped_files", default=None, help="Comma-separated list of .txt files to map to data_types (optional)")
parser.add_argument("--split_ratio", type=float, default=0.7, help="Training/test split ratio (0.0 to 1.0)")
parser.add_argument("--output_dir", required=True, help="Output directory for datasets")

args = parser.parse_args()

data_types = args.data_types.split(",")
if "clin" not in data_types:
raise ValueError("Clinical data ('clin') is required for splitting the dataset.")

file_mapping = {
"clin": "data_clinical_patient.txt", # can be any with 'clinical' in file name
"mut": "data_mutations.txt", # any with 'mutations' in file name
"omics": "data_cna.txt",
"other": None
}

if args.mapped_files:
mapped_files = args.mapped_files.split(",")
if len(mapped_files) != len(data_types):
raise ValueError(f"Number of mapped files ({len(mapped_files)}) must match number of data types ({len(data_types)}).")
files_to_fetch = {dt: mf for dt, mf in zip(data_types, mapped_files)}
for mf in mapped_files:
if not mf.endswith(".txt"):
raise ValueError(f"Mapped file '{mf}' must end with '.txt'.")
else:
files_to_fetch = {dt: file_mapping[dt] for dt in data_types if dt in file_mapping}

invalid_types = set(data_types) - set(file_mapping.keys())
if invalid_types:
raise ValueError(f"Invalid data types: {invalid_types}. Supported types: {list(file_mapping.keys())}")

cbioportal = CBioPortalData(study_id=args.study_id)
cbioportal.get_cbioportal_data(study_id=args.study_id, files=files_to_fetch)
dataset = cbioportal.split_data(ratio=args.split_ratio)

os.makedirs(args.output_dir, exist_ok=True)

for data_type in data_types:
if data_type in dataset['train']:
train_file = os.path.join(args.output_dir, f"{data_type}_train.csv")
dataset['train'][data_type].to_csv(train_file, index=True)
if data_type in dataset['test']:
test_file = os.path.join(args.output_dir, f"{data_type}_test.csv")
dataset['test'][data_type].to_csv(test_file, index=True)


if __name__ == "__main__":
main()
121 changes: 121 additions & 0 deletions tools/flexynesis/flexynesis_cbioportal_import.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<tool id="flexynesis_cbioportal_import" name="Flexynesis cBioPortal import" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
<description>and prepare cBioPortal data for Flexynesis analysis</description>
<macros>
<import>macros.xml</import>
</macros>
<expand macro="requirements"/>
<command detect_errors="exit_code"><![CDATA[
@CHECK_NON_COMMERCIAL_USE@
python '$__tool_directory__/fetch_cbioportal_data.py'
--study_id '$study_id'
--data_types '$data_types'
#if $mapped_files:
--mapped_files '$mapped_files'
#end if
--split_ratio '$split_ratio'
--output_dir 'output'
]]></command>
<inputs>
<param name="non_commercial_use" label="I certify that I am not using this tool for commercial purposes." type="boolean" truevalue="NON_COMMERCIAL_USE" falsevalue="COMMERCIAL_USE" checked="False">
<validator type="expression" message="This tool is only available for non-commercial use.">value == True</validator>
</param>
<param name="study_id" type="text" label="cBioPortal study ID" help="The ID of the study to fetch from cBioPortal (e.g., 'brca_tcga')." />
<param name="data_types" type="select" multiple="true" label="Data types to fetch" help="Select the types of data to retrieve from cBioPortal.">
<option value="clin" selected="true">Clinical data (default: data_clinical_patient.txt)</option>
<option value="mut">Mutations (default: data_mutations.txt)</option>
<option value="omics">Omics data (default: data_cna.txt)</option>
<option value="other">Other custom data</option>
</param>
<param name="mapped_files" type="text" optional="true" label="Mapped files" help="Comma-separated list of .txt files to map to the selected data types (e.g., 'data_clinical_sample.txt,data_mutations.txt,data_castom.txt'). Must match the number and order of data types." />
<param name="split_ratio" type="float" value="0.7" min="0.0" max="1.0" label="Training/Test split ratio" help="Proportion of data to use for training (e.g., 0.7 means 70% train, 30% test)." />
</inputs>
<outputs>
<collection name="datasets" type="list" label="${tool.name} on ${study_id}: datasets">
<discover_datasets pattern="(?P&lt;name&gt;.+_(train|test))\.csv$" format="csv" directory="output" />
</collection>
</outputs>
<tests>
<test expect_num_outputs="1">
<param name="non_commercial_use" value="True"/>
<param name="study_id" value="lgg_tcga" />
<param name="data_types" value="clin,mut" />
<param name="split_ratio" value="0.7" />
<output_collection name="datasets" type="list">
<element name="clin_test">
<assert_contents>
<has_text_matching expression="PATIENT_ID"/>
</assert_contents>
</element>
<element name="clin_train">
<assert_contents>
<has_text_matching expression="PATIENT_ID"/>
</assert_contents>
</element>
<element name="mut_test">
<assert_contents>
<has_text_matching expression="Hugo_Symbol"/>
</assert_contents>
</element>
<element name="mut_train">
<assert_contents>
<has_text_matching expression="Hugo_Symbol"/>
</assert_contents>
</element>
</output_collection>
</test>
<test expect_num_outputs="1">
<param name="non_commercial_use" value="True"/>
<param name="study_id" value="lgg_tcga" />
<param name="data_types" value="clin,mut,other" />
<param name="mapped_files" value="data_clinical_patient.txt,data_mutations.txt,data_cna.txt" />
<param name="split_ratio" value="0.8" />
<output_collection name="datasets" type="list">
<element name="clin_train">
<assert_contents>
<has_text_matching expression="PATIENT_ID"/>
</assert_contents>
</element>
<element name="mut_train">
<assert_contents>
<has_text_matching expression="Hugo_Symbol"/>
</assert_contents>
</element>
<element name="other_test">
<assert_contents>
<has_text_matching expression="Hugo_Symbol"/>
</assert_contents>
</element>
</output_collection>
</test>
</tests><help><![CDATA[
**Flexynesis cBioPortal import**

This tool fetches data from cBioPortal using the Flexynesis `CBioPortalData` class and prepares it for use with the Flexynesis Galaxy tool. It downloads a specified study, extracts the requested data types, splits them into training and test sets, and outputs them as CSV files compatible with Flexynesis (e.g., `clin_train.csv`, `mut_test.csv`).

**Inputs**

- **cBioPortal study ID**: The identifier of the study to fetch (e.g., `brca_tcga`, `lgg_tcga`). Find study IDs on the cBioPortal.
- **Data types to fetch**: Select one or more data types to retrieve. 'Clinical data' (`clin`) is required for splitting. Options:
- `clin`: Clinical data (default: `data_clinical_patient.txt`)
- `mut`: Mutation data (default: `data_mutations.txt`)
- `omics`: Omics data (default: `data_cna.txt`)
- `other`: Custom data type (requires `--mapped_files`)
- **Mapped files (optional)**: A comma-separated list of `.txt` files to override default filenames. Must match the number and order of selected data types (e.g., `data_clinical_sample.txt,data_mutations.txt,data_custom.txt`).
- **Training/Test split ratio**: The proportion of data for training (e.g., 0.7 means 70% train, 30% test).

**Outputs**

A collection of datasets including:
- `clin_train.csv` and `clin_test.csv`: Training and test clinical data (always included).
- `mut_train.csv` and `mut_test.csv`: Training and test mutation data (if selected).
- `omics_train.csv` and `omics_test.csv`: Training and test omics/CNA data (if selected).
- `other_train.csv` and `other_test.csv`: Training and test custom data (if `other` is selected with a mapped file).

These datasets can be used as inputs to the Flexynesis Galaxy tool for multi-omics analysis.

**Note**: Ensure the study ID is valid and the selected data types (or mapped files) are available in the study archive. Clinical data (`clin`) is mandatory for splitting.
]]></help>
<citations>
<citation type="doi">10.1101/2024.07.16.603606</citation>
</citations>
</tool>
2 changes: 1 addition & 1 deletion tools/flexynesis/macros.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<macros>
<token name="@TOOL_VERSION@">0.2.17</token>
<token name="@VERSION_SUFFIX@">0</token>
<token name="@VERSION_SUFFIX@">1</token>
<token name="@PROFILE@">24.1</token>
<xml name="requirements">
<requirements>
Expand Down