From R¶
The c4r package loads data from R through the same data catalog as
Python's cohort(), and returns ordinary data.frames. It is a thin wrapper:
the Python c4 package does the work, through
reticulate.
Install¶
You need the Python install first (setup), plus the R package reticulate. Then, from the root of the c4 repository:
R CMD INSTALL c4r
or, in R, install.packages("c4r", repos = NULL, type = "source").
Set up¶
Tell c4r which Python has c4 installed, once per session:
library(c4r)
c4r_setup(python = "/path/to/venv/bin/python") # or a virtualenv directory
To skip this step, set C4_PYTHON in ~/.Renviron:
C4_PYTHON=/path/to/venv/bin/python
To use another catalog, e.g. the demo one, pass it to setup:
c4r_setup(catalog = "examples/demo/catalog.yaml").
Check the setup
From the repository root,
C4_PYTHON=/path/to/venv/bin/python Rscript c4r/tests/run_demo.R runs
every method on the demo cohort and ends with "All c4r checks passed."
Load data¶
The cohort object has the same methods as in Python, called with $ instead
of .. The examples use the fake demo cohort, shown with its data folder as
/data/lab.
c4_cohorts()
[1] "demo"
demo <- cohort("demo") # prints what's available, as in Python
pheno <- demo$load("pheno")
str(pheno)
Left out 1 patient(s) marked drop=1 in the id_map (keep_dropped=True to keep them).
'data.frame': 7 obs. of 8 variables:
$ patient_id: chr "P001" "P002" "P003" "P004" ...
$ qc_note : chr NA NA "old exome_id: EX-0003" "old exome_id: EX-0004" ...
$ ID : chr "P001" "P002" "P003" "P004" ...
$ age : int 64 58 71 66 49 73 55
$ female : int 0 1 1 0 1 0 0
$ site : chr "A" "A" "B" "B" ...
$ OS_mo : num 31.2 44 12.5 27.9 50.1 19 38.6
$ OS_event : int 1 0 1 1 0 1 0
Several table names are merged on patient_id:
df <- demo$load("pheno", "pcs", "hla.hlahd")
df[, c("patient_id", "OS_mo", "IID", "PC1", "seqID", "A")]
Note: 1 of 7 rows in 'pcs' have no patient_id (array ID not in id_map).
Left out 1 patient(s) marked drop=1 in the id_map (keep_dropped=True to keep them).
pheno.main 7 patients
(1 row(s) of pcs without a patient_id dropped before merging)
+ pcs 5 matched (2 without)
+ hla.hlahd.standardized 5 matched (2 without)
= 7 rows (left join on patient_id)
patient_id OS_mo IID PC1 seqID A
1 P001 31.2 ARR-0001 0.012 EX-0001 A*02:01/A*24:02
2 P002 44.0 ARR-0002 0.010 EX-0002 A*01:01/A*03:01
3 P003 12.5 ARR-0003 -0.044 EX-0004 A*11:01/A*24:02
4 P004 27.9 ARR-0004 0.009 EX-0003 A*02:01/A*02:01
5 P005 50.1 <NA> NA EX-0005 A*03:01/A*68:01
6 P007 19.0 ARR-0007 -0.051 <NA> <NA>
7 P008 38.6 <NA> NA <NA> <NA>
$load() arguments
- Unnamed: table names.
- Named: values for templated files, e.g.
demo$load("hla.hibag", panel = "african").
- Options: how ("left", "inner", "outer"), qc_note,
keep_dropped, verbose.
What it does is described in Loading and merging.
Everything else¶
demo$path("dna.imputed", chrom = 6) # "/data/lab/demo/dna/chr6.dose.vcf.gz"
demo$paths("dna.imputed")[["6"]] # all chromosomes, named by number
demo$describe("OS_mo") # Overall survival (months) (numeric)
demo$id_map() # patient_id / exome_id / array_id / imputed_id
demo$dropped_patients() # "P006"
demo$check() # problems with the catalog, if any
How R and Python differ¶
| Python | R | |
|---|---|---|
| Call a method | demo.load("pheno") |
demo$load("pheno") |
| Options | keep_dropped=True |
keep_dropped = TRUE |
| Missing values | NaN / None |
NA |
| HLA genotype columns | HLAgenotype objects |
text, e.g. "A*02:01/A*24:02" |
| Merge report and notes | printed | R messages; hide with verbose = FALSE or suppressMessages() |
| Errors | Python exceptions | R errors with the same message |
For example, an invalid template value:
demo$load("hla.hibag", panel = "martian")
Error: ValueError: Invalid panel='martian' for 'hla.hibag.raw'. Choose from: ['european', 'african', 'asian', 'multiethnic']
Only data loading is wrapped. The command-line tools (c4 dosages,
c4 survival, …) work the same whether you use R or Python; see
Command line.