I/O Modules
TCGA Builders
The case-level builders in pymaftools.io.tcga are the canonical TCGA API.
They resolve downloaded GDC files to cases, select the requested sample type,
and return the appropriate table class with aligned metadata. The standalone
functions in pymaftools.io.tcga_readers remain available as lower-level
compatibility utilities.
TCGA-specific data builders and GDC client.
- class pymaftools.io.tcga.GDCClient(token_path=None, data_types=None, projects=None, gdc_client_path=None, threads=8, retries=5)[source]
Bases:
objectClient for querying, aligning, and downloading TCGA data from GDC.
- Parameters:
token_path (str or Path, optional) – GDC authentication token (required for controlled-access data).
data_types (dict, optional) – Data type configurations keyed by label. Each entry must have
data_type(GDC field) and optionallyworkflow_type. Defaults toDATA_TYPE_CONFIGS.projects (list of str, optional) – GDC project IDs (e.g.,
["TCGA-LUAD", "TCGA-LUSC"]).gdc_client_path (str or Path, optional) – Path to the gdc-client binary. Auto-detected from PATH if not set.
threads (int) – Download threads passed to gdc-client (default 8).
retries (int) – Retry attempts for gdc-client (default 5).
Examples
>>> # From config file (recommended) >>> client = GDCClient.from_config("config.toml") >>> client.generate_full_manifests(outdir="data/manifests/full") >>> client.align_manifests(outdir="data/manifests/aligned")
>>> # Programmatic >>> client = GDCClient(projects=["TCGA-LUAD"]) >>> aligned = client.align_cases("TCGA-LUAD")
- classmethod from_config(config_path='config.toml')[source]
Create a GDCClient from a TOML config file.
- Parameters:
config_path (str or Path) –
Path to a TOML file with the following structure:
projects = ["TCGA-LUAD", "TCGA-LUSC"] [download] gdc_client = "tools/gdc-client" token = "/path/to/gdc-token.txt" threads = 8 retries = 5 [data_types.expression] data_type = "Gene Expression Quantification" workflow_type = "STAR - Counts" [data_types.mutation] data_type = "Masked Somatic Mutation" workflow_type = "Aliquot Ensemble Somatic Variant Merging and Masking"
Each
[data_types.<label>]entry must havedata_typeand optionallyworkflow_type. Labels become manifest filename prefixes and raw download directory names.- Return type:
- Returns:
GDCClient
- get_cases(project_id, data_type_key)[source]
Get all case submitter_ids that have a given data type.
- align_cases(project_id, data_types=None)[source]
Find cases with all specified data types (set intersection).
- align_multi_project(project_ids=None, data_types=None)[source]
Align cases across multiple projects.
- generate_full_manifests(projects=None, outdir='data/manifests/full', mapping_path='data/file_to_case.tsv')[source]
Generate complete per-dtype manifests (no case filtering) and build a file_id → case_id / sample_type / project mapping table.
- build_file_mapping(records)[source]
Build file_id → case_id / sample_type / project mapping via GDC API.
- align_manifests(mode='pipeline', full_manifest_dir='data/manifests/full', portal_path=None, mapping_path='data/file_to_case.tsv', outdir='data/manifests/aligned', aligned_cases_path='data/aligned_cases.tsv')[source]
Align manifests across data types — keep only cases with all omics.
- Parameters:
mode (str) –
"pipeline"(read full manifests + file_to_case.tsv) or"portal"(read a GDC portal manifest, query GDC for metadata).full_manifest_dir (str or Path) – Directory containing full per-dtype manifests (pipeline mode).
portal_path (str or Path, optional) – Path to a GDC portal manifest file (portal mode).
mapping_path (str or Path) – Path to file_to_case.tsv (pipeline mode).
outdir (str or Path) – Output directory for aligned manifests.
aligned_cases_path (str or Path) – Output path for aligned_cases.tsv.
- Return type:
- Returns:
Path – Aligned manifest directory.
- get_file_metadata(project_id, data_type_key, case_ids=None)[source]
Get file metadata for a project + data type, optionally filtered by cases.
- Return type:
DataFrame
- download(case_ids, project_id, data_types=None, outdir='data/raw', manifest_dir=None)[source]
Generate manifests for aligned cases and download via gdc-client.
- Parameters:
- Return type:
- class pymaftools.io.tcga.TCGATableBuilder(data_dir, mapping, sample_type='Primary Tumor')[source]
Bases:
ABCBase builder for TCGA data types.
Subclasses must implement: -
file_pattern: glob pattern for matching files -read_and_merge(files): read files and return a table- Parameters:
data_dir (str or Path) – Directory containing downloaded files.
mapping (str, Path, or pd.DataFrame) – Path to file_to_case.tsv or pre-loaded mapping DataFrame.
sample_type (str or None, default "Primary Tumor") – Sample type to retain when building a case-level matrix. Set to None only when the input contains at most one file per case.
- select_files(files)[source]
Select one deterministic input file for each case.
Case-level matrices cannot represent multiple sample types or aliquots under the same column identifier. Filtering happens before both matrix construction and metadata construction so that they cannot disagree.
- abstractmethod read_and_merge(files)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- build_sample_metadata(table, files)[source]
Build sample_metadata from resolved file info.
Creates a DataFrame indexed by case_id with columns: case_id, sample_type, file_id, data_type.
- Parameters:
table (PivotTable) – The built table (used to get column names).
- Return type:
DataFrame- Returns:
pd.DataFrame – Sample metadata indexed by case_id.
- class pymaftools.io.tcga.TCGAExpressionBuilder(data_dir, mapping, count_column='unstranded', enrich_coordinates=True, sample_type='Primary Tumor')[source]
Bases:
TCGATableBuilderBuild an ExpressionTable from TCGA STAR-Counts files.
- Parameters:
data_dir (str or Path) – Directory containing expression files.
mapping (str, Path, or pd.DataFrame) – Path to file_to_case.tsv or pre-loaded mapping DataFrame.
count_column (str, default "unstranded") – Column to extract from STAR-Counts files. Options: unstranded, stranded_first, stranded_second, tpm_unstranded, fpkm_unstranded, fpkm_uq_unstranded.
enrich_coordinates (bool, default True) – If True, enrich feature_metadata with genomic coordinates (chromosome_name, start_position, end_position) from Ensembl BioMart via
pymaftools.utils.geneinfo.load_ensembl_map(). The first call downloads and caches the map; subsequent calls are fast.sample_type (str or None, default "Primary Tumor") – Sample type to retain in the case-level matrix.
- read_and_merge(files)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Return type:
- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- class pymaftools.io.tcga.TCGAMutationBuilder(data_dir, mapping, sample_type='Primary Tumor')[source]
Bases:
TCGATableBuilderBuild a MAF object from TCGA masked somatic mutation files.
Note: MAF is a flat DataFrame (not a PivotTable matrix). Use
maf.to_pivot_table()to get a gene × sample matrix.- Parameters:
- read_and_merge(files, tumor_only=True)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Return type:
- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- build_sample_metadata(table, files)[source]
Build sample_metadata from resolved file info.
Creates a DataFrame indexed by case_id with columns: case_id, sample_type, file_id, data_type.
- Parameters:
table (PivotTable) – The built table (used to get column names).
- Returns:
pd.DataFrame – Sample metadata indexed by case_id.
- class pymaftools.io.tcga.TCGACNVSegmentBuilder(data_dir, mapping, sample_type='Primary Tumor')[source]
Bases:
TCGATableBuilderBuild CNV data from TCGA masked segment files.
Provides two outputs: -
build()→ raw segment DataFrame (long format) -build_cytoband_table()→ cytoband × sample CopyNumberVariationTable- Parameters:
- read_and_merge(files)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Return type:
DataFrame- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- build_cytoband_table(seg_df=None, cytoband_path=None)[source]
Convert segments to cytoband × sample matrix.
Computes overlap-weighted average Segment_Mean per cytoband per case.
- Parameters:
seg_df (pd.DataFrame, optional) – Pre-built segment DataFrame. If None, calls
build()first.cytoband_path (str or Path, optional) – Path to cytoBand.txt. Defaults to bundled file.
- Return type:
- Returns:
CopyNumberVariationTable – Cytoband × sample matrix.
- class pymaftools.io.tcga.TCGACNVGeneBuilder(data_dir, mapping, value_column='copy_number', strip_gene_version=True)[source]
Bases:
TCGATableBuilderBuild a CopyNumberVariationTable from TCGA gene-level copy number files.
Supports both ASCAT3 filename variants: -
*.gene_level_copy_number.v36.tsv-*.gene_level.copy_number_variation.tsv- Parameters:
data_dir (str or Path) – Directory containing gene-level CNV files.
mapping (str, Path, or pd.DataFrame) – Path to file_to_case.tsv or pre-loaded mapping DataFrame.
value_column (str, default "copy_number") – Column to use as values. Options: copy_number, min_copy_number, max_copy_number.
strip_gene_version (bool, default True) – Strip ENSG version suffix (e.g.
.15).
- read_and_merge(files)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Return type:
- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- class pymaftools.io.tcga.TCGAMethylationBuilder(data_dir, mapping, sample_type='Primary Tumor')[source]
Bases:
TCGATableBuilderBuild a PivotTable from TCGA methylation beta value files.
- Parameters:
- read_and_merge(files)[source]
Read resolved files and merge into a table.
- Parameters:
files (list of dict) – Output from
resolve_files().- Return type:
- Returns:
PivotTable or subclass – Table with columns as sample identifiers.
- class pymaftools.io.tcga.TCGAClinicalBuilder(data_dir, mapping, file_type='patient')[source]
Bases:
objectBuild a clinical DataFrame from TCGA BCR clinical TXT files.
- Parameters:
- pymaftools.io.tcga.load_file_mapping(mapping_path)[source]
Load file_to_case.tsv mapping table.
Expected columns: file_id, filename, data_type, case_id, sample_type.
- Parameters:
mapping_path (str or Path) – Path to the TSV mapping file.
- Return type:
DataFrame- Returns:
pd.DataFrame – Mapping table indexed by file_id.
- pymaftools.io.tcga.resolve_files(data_dir, pattern, mapping_df)[source]
Scan a download directory and resolve each file to its case metadata.
Supports two directory layouts: - gdc-client:
{data_dir}/{file_uuid}/{filename}- flat:{data_dir}/{filename}- Parameters:
data_dir (str or Path) – Base directory containing downloaded files.
pattern (str) – Glob pattern for the data files.
mapping_df (pd.DataFrame) – Mapping table from
load_file_mapping(), indexed by file_id.
- Return type:
- Returns:
list of dict – Each dict has keys: case_id, sample_type, data_type, file_id, filepath. Sorted by case_id.
VCF Parsing
- pymaftools.io.vcf.parsers.sample_type(sample_id)[source]
Classify a VCF sample column as tumour or normal.
- Parameters:
sample_id (str) – Sample identifier from the VCF header.
- Return type:
- Returns:
str –
"TUMOR"or"NORMAL".- Raises:
ValueError – If the sample identifier does not end with
TorN.
- pymaftools.io.vcf.parsers.parse_info(format_str, value_str, *, alt_index=0)[source]
Parse caller-specific FORMAT data into depth metrics.
- Parameters:
- Return type:
- Returns:
tuple[int, int, int, float] – Parsed
(DP, ref_count, alt_count, AF)values. ref_count is the reference allele depth.- Raises:
ValueError – If the FORMAT pattern is not recognised or cannot be parsed.
- pymaftools.io.vcf.parsers.parse_vcf_rows(vcf_path, caller)[source]
Parse PASS VCF rows into validated dictionaries.