Full run
Run TWAS on your own data: the input data you need, summary-level and genotype-level TWAS, results and plots, and meta-analysis TWAS with ACAT.
1.Input data
What you need depends on the TWAS you run. Summary-level TWAS needs only a GWAS; genotype-level TWAS (and GWAS) needs the data of individual subjects. Both use the full models of gene expression (see Getting started), and all data must use rsIDs for SNPs.
| Input | Format | Added with |
|---|---|---|
| Summary-level TWAS | ||
| GWAS summary statistics | one file per trait, one row per SNP: SNP ID (rsID), the two alleles, and the effect size (beta) with its standard error or a p-value (see column names) | stwas.add_gwas |
| Genotype-level TWAS and GWAS | ||
| Subject IDs | one ID per line, no header | project.set_subjects |
| Covariates | a table with an id column and one column per covariate, e.g. age, sex, and genetic principal components (numbers only, e.g. sex as 0/1) | project.add_covariates |
| Phenotypes | a table with an id column and one column per trait; the column names are the trait names | project.add_phenotypes |
| Genotypes | one BGEN file per chromosome (1–22), each with a matching .sample file | project.add_genotypes |
- The subject IDs must be the same in all files (in the
.samplefiles, both ID columns). - Tables are tab-separated by default; for comma-separated tables, add
sep=','. To use only some columns, addcovar_cols=[...]orphen_cols=[...]. - AGENT-P cleans the genotypes before use: it keeps SNPs with less than 5% missing calls, a minor allele frequency of at least 1%, and a Hardy–Weinberg equilibrium p-value above 1e-5.
- Genotypes in VCF format can be converted to BGEN with
plink2(see Troubleshooting). - Example files of every kind are in
~/agentp/example/example_data. The code on this page uses them, so each piece of code runs as is; replace them with your own files.
2.Create a project
Write the steps on this page in a Python script (for example ~/my_analysis.py), and run it with python ~/my_analysis.py.
A project is a folder that keeps the inputs and results of one cohort together.
import os from agentp import Project project = Project(os.path.expanduser('~/my_project')) # creates the folder ~/my_project
3.Summary-level TWAS
From GWAS summary statistics of a trait:
import os from agentp import Project, SummaryTWAS data = os.path.expanduser('~/agentp/example/example_data') # the folder of your data (here: the example data) project = Project(os.path.expanduser('~/my_project')) stwas = SummaryTWAS(project, 'JTI', 'caudate') # model method and brain region stwas.add_gwas('amygdala_volume', f'{data}/amygdala_volume_gwas.regenie.gz') # trait name and GWAS file stwas.run_twas('amygdala_volume')
Column names. GWAS files made with AGENT-P's GWAS (see More options) are read without any extra settings. For other files, including REGENIE's own output, tell AGENT-P the column names:
import os from agentp import Project, SummaryTWAS data = os.path.expanduser('~/agentp/example/example_data') # the folder of your data (here: the example data) project = Project(os.path.expanduser('~/my_project')) stwas = SummaryTWAS(project, 'JTI', 'caudate') stwas.add_gwas('amygdala_volume', f'{data}/amygdala_volume_gwas.regenie.gz', sep=',', # the column names in your file snp_col='SNP', alt_col='ALLELE1', ref_col='ALLELE0', beta_col='BETA', beta_se_col='SE', pval_col='PVAL') stwas.run_twas('amygdala_volume')
alt_colis the effect allele: the allele that the beta refers to.ref_colis the other allele.- If your file has a z-score or odds ratio instead of a beta, use
zscore_coloror_col. - FUSION models need the beta and p-value columns. REGENIE's own output has no p-value column: use
sep=' ', snp_col='ID', alt_col='ALLELE1', ref_col='ALLELE0', beta_col='BETA', beta_se_col='SE'(every method except FUSION), or run the GWAS with AGENT-P'sGWAS.
4.Genotype-level TWAS
From individual-level data. First add the subject data to the project (only once per project; see Input data for the file formats):
import os from agentp import Project data = os.path.expanduser('~/agentp/example/example_data') # the folder of your data (here: the example data) project = Project(os.path.expanduser('~/my_project')) project.set_subjects(f'{data}/subjects.txt') # subject IDs project.add_covariates(f'{data}/covariates.csv') # e.g. age, sex, genetic principal components project.add_phenotypes(f'{data}/volumes.csv') # one or more traits project.add_genotypes(f'{data}/genotypes', 'test_c*.bgen') # folder, and file names with * for the chromosome number
Then predict gene expression and test the traits:
import os from agentp import Project, IndividualTWAS project = Project(os.path.expanduser('~/my_project')) # the project with the subject data itwas = IndividualTWAS(project, 'JTI', 'caudate') itwas.predict_grex() # predict gene expression (once per method and region) itwas.run_twas('amygdala_volume') # or a list: ['amygdala_volume', 'caudate_volume']
5.Results and plots
Each TWAS saves one table per trait (tab-separated, one row per gene):
| TWAS | Results file |
|---|---|
| Summary-level | ~/my_project/sTWAS/JTI_caudate/amygdala_volume.csv |
| Genotype-level | ~/my_project/iTWAS/JTI_caudate/amygdala_volume.csv |
The most important columns are gene (Ensembl gene ID), zscore (direction and strength of the association: positive means that higher expression goes with a larger trait value), and pvalue. A common threshold for significance is a Bonferroni correction: p < 0.05 / (number of genes tested).
Plots (saved as image files):
import os from agentp import load_pvalues, manhattan_plot, miami_plot, scatter_plot project_dir = os.path.expanduser('~/my_project') s = load_pvalues(f'{project_dir}/sTWAS/JTI_caudate/amygdala_volume.csv') g = load_pvalues(f'{project_dir}/iTWAS/JTI_caudate/amygdala_volume.csv') manhattan_plot(g, label='Genotype-level TWAS', out_file=f'{project_dir}/manhattan.png') # one set of results miami_plot(s, g, top_label='Summary-level TWAS', bottom_label='Genotype-level TWAS', out_file=f'{project_dir}/miami.png') # two sets, mirrored scatter_plot(s, g, x_label='Summary-level TWAS', y_label='Genotype-level TWAS', out_file=f'{project_dir}/scatter.png') # two sets, gene by gene
Significant genes (Bonferroni) are colored, and the dashed lines show the significance thresholds.
6.Meta-analysis TWAS with ACAT
run_all_twas runs TWAS with every model of gene expression (4 methods × 8 brain regions = 32 model sets), then combines the p-values of each gene into one p-value with ACAT (Aggregated Cauchy Association Test):
import os from agentp import Project, run_all_twas data = os.path.expanduser('~/agentp/example/example_data') # the folder of your data (here: the example data) project = Project(os.path.expanduser('~/my_project')) # summary-level TWAS run_all_twas(project, 'amygdala_volume', twas_type='stwas', gwas_files={'amygdala_volume': f'{data}/amygdala_volume_gwas.regenie.gz'}) # genotype-level TWAS (after adding the subject data) run_all_twas(project, 'amygdala_volume', twas_type='itwas')
The combined results are saved to ~/my_project/sTWAS/ACAT/amygdala_volume.csv (or ~/my_project/iTWAS/ACAT/). This table has one p-value column per model set, n_models (how many models tested the gene), and the combined p-value (sTWAS_ACAT or iTWAS_ACAT). Genes tested by fewer than 4 models get no combined p-value.
If your GWAS file is not in REGENIE format, give the column names once for all files: gwas_params={'sep': '\t', 'snp_col': 'rsid', 'alt_col': 'a2', ...}. To combine results that you already ran one model at a time, use collect_twas(project, 'amygdala_volume').
7.More options
- Run a GWAS (needs the subject data):
Python
import os from agentp import Project, GWAS project = Project(os.path.expanduser('~/my_project')) # the project with the subject data GWAS(project).run(['amygdala_volume', 'caudate_volume']) # saved to ~/my_project/GWAS/
- Add other models of gene expression. Models in PredictDB format (a
.dbweights file and a.txt.gzcovariance file, e.g. other GTEx tissues) can be added and then used like the built-in ones:Pythonimport os from agentp import Project, SummaryTWAS project = Project(os.path.expanduser('~/my_project')) models = os.path.expanduser('~/Downloads') # the folder with the model files project.add_model('PDX', 'hypothalamus', weights=f'{models}/en_Brain_Hypothalamus.db', covariance=f'{models}/en_Brain_Hypothalamus.txt.gz') stwas = SummaryTWAS(project, 'PDX', 'hypothalamus')
- Run again and replace old results: add
overwrite=True, e.g.stwas.run_twas('amygdala_volume', overwrite=True). Without it, AGENT-P skips results that already exist. - Several traits in parallel:
run_twas([...], num_threads=4)runs 4 traits at the same time.
8.Troubleshooting
- My genotypes are in VCF format. Convert them to BGEN, one file per chromosome:
SNP IDs must be rsIDs. If your VCF uses other IDs (e.g.Terminal
conda activate agentp mkdir -p ~/my_data/genotypes for c in $(seq 1 22); do plink2 --vcf ~/my_data/chr${c}.dose.vcf.gz dosage=DS --double-id --export bgen-1.2 bits=8 --out ~/my_data/genotypes/chr${c} donechr1:12345:A:G), add rsIDs first (e.g. withbcftools annotateand a dbSNP file). - Very few genes are tested. The SNP IDs in your GWAS or genotypes do not match the models of gene expression: check that they are rsIDs.
- Problems with the installation: see Getting started.