{"data":{"slug":"github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-consensus-clustering-analysis-ae7bae303886","title":"Consensus Clustering Analysis for Bulk Expression Matrices","summary":"Identify stable sample subtypes from bulk expression matrices using ConsensusClusterPlus with PAC-based model selection and visualization of consensus matrix and CDF curves.","url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-consensus-clustering-analysis-ae7bae303886","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-consensus-clustering-analysis-ae7bae303886","markdown_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-consensus-clustering-analysis-ae7bae303886.md","download_url":"https:\/\/skillshunter.net\/skills\/github-aipoch-medical-research-skills-awesome-med-research-skills-data-analysis-consensus-clustering-analysis-ae7bae303886\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"Ensure the input files are in CSV, TSV, or TXT format with genes as rows and samples as columns. The group file must include sample IDs and group labels. Select the disease group to retain for clustering, and specify the maximum number of clusters to evaluate.","instructions":"---\nname: consensus-clustering-analysis\ndescription: \"Use when identifying stable sample subtypes from bulk expression matrices with ConsensusClusterPlus, including PAC-based model selection and consensus matrix\/CDF visualization. NOT for: differential expression analysis, single-cell clustering workflows, or non-expression tables.\"\nlicense: MIT\nauthor: AIPOCH\n---\n> **Source**: [https:\/\/github.com\/aipoch\/medical-research-skills](https:\/\/github.com\/aipoch\/medical-research-skills)\n\n# Consensus Clustering Analysis\n\n## When to Use\n\nUse this skill when you need to identify stable sample subtypes from a bulk expression matrix with `ConsensusClusterPlus`, compare candidate clustering settings with PAC, and export consensus matrix\/CDF visualizations.\n\nDo not use this skill for differential expression analysis, single-cell clustering, or non-expression tabular data.\n\n## When to Read External Files\n\n| Situation | File to Read | Purpose |\n|-----------|--------------|---------|\n| **Need algorithm details** | `references\/algorithm.md` | Consensus clustering, PAC scoring, and preprocessing assumptions |\n| **Need to run analysis** | `scripts\/main.R` | Execute: `Rscript scripts\/main.R --input_file ... --group_file ...` |\n| **Encounter errors** | `references\/troubleshooting.md` | Common errors and solutions |\n| **Need CLI examples** | `references\/cli-guide.md` | Detailed CLI usage examples with verified local runs |\n\n---\n\n## Usage\n\n```bash\nRscript scripts\/main.R \\\n  --input_file .\/expression_matrix.csv \\\n  --group_file .\/groups.csv \\\n  --disease_group case \\\n  --max_k 4 \\\n  --output_dir .\/output\/ \\\n  --gene_selection highly_variable \\\n  --top_n 5000 \\\n  --reps 1000 \\\n  --p_item 0.8 \\\n  --p_feature 1.0 \\\n  --timeout_seconds 3600 \\\n  --seed 42\n```\n\n---\n\n## Arguments\n\n| Short | Long | Type | Default | Description |\n|-------|------|------|---------|-------------|\n| `-i` | `--input_file` | character | **required** | Expression matrix file (genes as rows, samples as columns) |\n| `-g` | `--group_file` | character | **required** | Group information file (sample ID + group columns) |\n| `-d` | `--disease_group` | character | `case` | Group label retained for clustering |\n| `-k` | `--max_k` | integer | `4` | Maximum cluster count to evaluate |\n| `-o` | `--output_dir` | character | `.\/output\/` | Output directory |\n| `-m` | `--gene_selection` | character | `highly_variable` | Gene selection mode: `highly_variable` or `custom` |\n| `-n` | `--top_n` | integer | `5000` | Number of top variable genes to keep |\n| `-l` | `--gene_list` | character | `NULL` | Custom gene list file when `gene_selection=custom` |\n| `-c` | `--center_data` | logical | `TRUE` | Median-center each gene before clustering |\n| `-r` | `--reps` | integer | `1000` | Consensus resampling repetitions |\n|  | `--p_item` | double | `0.8` | Sample resampling proportion |\n|  | `--p_feature` | double | `1.0` | Feature resampling proportion |\n| `-t` | `--timeout_seconds` | integer | `3600` | Elapsed timeout in seconds |\n| `-s` | `--seed` | integer | `42` | Random seed for reproducibility |\n\n---\n\n## Input Format\n\n### Expression Matrix (input_file)\n\nGenes as rows, samples as columns, CSV\/TSV\/TXT format with gene ID in the first column.\n\n```csv\n,Sample01,Sample02,Sample03\nTSPAN6,1.8479,1.8318,3.8276\nTNMD,0.0349,0.0533,1.3889\n```\n\n### Group File (group_file)\n\nDelimited text file with sample ID and group columns.\n\n```csv\nsample,group\nSample01,case\nSample02,control\nSample03,case\n```\n\n### Gene List (gene_list)\n\nOptional plain text or single-column CSV file with one gene symbol per line.\n\n```csv\nTNMD\nDPM1\nSCYL3\n```\n\n---\n\n## Output Files\n\n| File | Description |\n|------|-------------|\n| `Cluster_res.csv` | PAC summary for each distance\/algorithm combination with `is_best` marking the selected model |\n| `genes_for_clustering.csv` | Selected genes and gene selection mode |\n| `samples_for_clustering.csv` | Samples retained after disease-group filtering |\n| `result_<distance>_<algorithm>\/` | Method-specific consensus outputs and `PAC_scores.csv` |\n| `Consensus Matrix Plot.pdf` | Consensus matrix heatmap for the optimal model |\n| `CDF curve Plot.pdf` | CDF curves for the optimal method |\n| `session_info.txt` | R session and package version info |\n\n---\n\n## Workflow\n\n### Step 1: Validate Input\n- Check file existence\n- Detect sample and group columns in the group file\n- Validate sample matching between expression matrix and group file\n\n### Step 2: Prepare Clustering Matrix\n- Filter samples by the requested disease group\n- Select genes using `highly_variable` or `custom`\n- Median-center genes if requested\n\n### Step 3: Run Consensus Clustering\n- Evaluate supported distance and clustering algorithm combinations\n- Compute PAC scores across candidate K values\n- Select the optimal model by minimum PAC\n\n### Step 4: Generate Outputs\n- Save result tables\n- Generate consensus matrix and CDF plots\n- Record session information for reproducibility\n\n---\n\n## Methods\n\n### ConsensusClusterPlus\nRepeated subsampling is used to estimate cluster stability across candidate K values and clustering settings.\n\n### PAC Score\nThe proportion of ambiguous clustering is computed as `CDF(0.9) - CDF(0.1)` from lower-triangle consensus values. Lower PAC indicates more stable clustering.\n\n### Gene Selection\n- `highly_variable`: rank genes by median absolute deviation\n- `custom`: use the intersection of the provided gene list and matrix row names\n\n---\n\n## Examples\n\n### Basic Usage\n```bash\nRscript scripts\/main.R \\\n  -i expression_matrix.csv \\\n  -g groups.csv \\\n  -d case \\\n  -k 3 \\\n  -r 20 \\\n  -o output\/example_basic \\\n  -t 120\n```\n\n### With a Custom Gene List\n```bash\nRscript scripts\/main.R \\\n  -i expression_matrix.csv \\\n  -g groups.csv \\\n  -d case \\\n  -m custom \\\n  -l genes.csv \\\n  -k 4 \\\n  -r 20 \\\n  -o output\/example_custom \\\n  -t 120\n```\n\n### Without Median Centering\n```bash\nRscript scripts\/main.R \\\n  -i expression_matrix.csv \\\n  -g groups.csv \\\n  -d case \\\n  -c FALSE \\\n  -k 3 \\\n  -r 20 \\\n  -o output\/example_rawscale \\\n  -t 120\n```\n\n---\n\n## Error Handling\n\n### Common Errors\n\n| Error | Cause | Solution |\n|-------|-------|----------|\n| `SKILL_FILE_NOT_FOUND` | Input file does not exist | Check file path and permissions |\n| `SKILL_MISSING_COLUMNS` | Group file lacks sample\/group columns | Verify column names in the group file |\n| `SKILL_SAMPLE_MISMATCH` | Sample names do not match | Ensure group file sample IDs match matrix columns |\n| `SKILL_INVALID_PARAMETER` | CLI value is invalid | Check allowed options and numeric ranges |\n| `SKILL_INVALID_DATA` | Too few samples\/genes remain after filtering | Lower `max_k` or review the input data |\n| `SKILL_TIMEOUT` | Run exceeded the configured timeout | Increase `timeout_seconds` or reduce `reps` |\n| `SKILL_DEPENDENCY_MISSING` | Required R package is not installed | Install missing packages before rerunning |\n\n**IF error persists**, READ: `references\/troubleshooting.md`\n\n---\n\n## Testing\n\n### Smoke Check\n\n```bash\n# Check help\nRscript scripts\/main.R --help\n\n# Run analysis\nRscript scripts\/main.R \\\n  -i tests\/data\/expression_matrix.csv \\\n  -g tests\/data\/groups.csv \\\n  -d case \\\n  -k 3 \\\n  -r 20 \\\n  -o output\/example_basic \\\n  -t 120\n```\n\n### Validation Commands\n\n```bash\n# Inspect selected model\ncat output\/example_basic\/Cluster_res.csv\n\n# Check output plots exist\nls -la output\/example_basic\n```\n\n---\n\n## Implementation Checklist\n\n- [x] CLI parsing with `optparse`\n- [x] `set.seed()` for reproducibility\n- [x] `requireNamespace()` dependency checks\n- [x] Session info recording\n- [x] `data.table::fread()` input reading\n- [x] File reading instructions in `SKILL.md`\n- [x] Modular script structure (<150 lines per file)\n- [x] Test data provided\n- [x] Error handling with `SKILL_*` codes\n- [x] Scripts in `scripts\/` directory\n- [x] References in `references\/` directory\n\n---\n\n*Last updated: 2026-04-17 | Version: 1.0.0*\n","use_cases":["Identify stable subtypes in cancer patient samples from RNA-seq data","Compare clustering stability across different distance metrics and algorithms","Visualize consensus matrix and CDF curves for optimal clustering model"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/aipoch\/medical-research-skills\/blob\/686e09dfdb45d9fa1a08664f06bb93769b3c9d91\/awesome-med-research-skills\/Data%20Analysis\/consensus-clustering-analysis\/SKILL.md","source_commit":"686e09dfdb45d9fa1a08664f06bb93769b3c9d91","license":"MIT","version":null,"listing_approved_at":"2026-11-24","created_at":"2026-11-23T06:57:36.000000Z","updated_at":"2026-11-24T07:13:04.000000Z","seo_title":"Consensus Clustering Analysis for Bulk Expression Data","seo_description":"Use ConsensusClusterPlus to identify stable sample subtypes from bulk expression matrices with PAC-based model selection and visualization of consensus matrix and CDF cur","seo_keywords":["consensus clustering analysis","bulk expression matrix clustering","PAC score model selection","consensus matrix visualization","cluster stability analysis"]}}