| name | bio-ecological-genomics-landscape-genomics |
| description | Tests genotype-environment associations and identifies loci under local adaptation using LFMM2 (LEA), pcadapt outlier detection, OutFLANK Fst-based selection scans, and redundancy analysis. Detects adaptive genetic variation correlated with environmental variables while controlling for population structure. Use when identifying adaptive loci across environmental gradients, testing for signatures of local adaptation, or predicting genetic vulnerability to climate change with gradientForest. |
| tool_type | r |
| primary_tool | LEA |
Landscape Genomics
Identifies loci under local adaptation by testing genotype-environment associations while controlling for population structure.
Population Structure Estimation with LEA
Determine K (number of ancestral populations) before running GEA:
library(LEA)
vcf2lfmm('variants.vcf', 'genotypes.lfmm')
vcf2geno('variants.vcf', 'genotypes.geno')
snmf_result <- snmf('genotypes.geno', K = 1:10, repetitions = 5,
entropy = TRUE, project = 'new')
ce_values <- sapply(1:10, function(k) min(cross.entropy(snmf_result, K = k)))
plot(1:10, ce_values, xlab = 'K', ylab = 'Cross-entropy', pch = 19, col = 'blue')
best_K <- which.min(ce_values)
LFMM2 Genotype-Environment Association
Latent factor mixed model tests association between each locus and environmental variables while correcting for population structure via latent factors:
library(LEA)
genotypes <- read.lfmm('genotypes.lfmm')
env_vars <- read.env('environment.env')
lfmm_result <- lfmm2(input = genotypes, env = env_vars, K = 3)
pvalues <- lfmm2.test(lfmm_result, input = genotypes, env = env_vars,
full = TRUE, genomic.control = TRUE)
gif <- median(qchisq(1 - pvalues$pvalues[, 1], df = 1) qchisq df
cat gif
libraryqvalue
qvals qvaluepvaluespvalues qvalues
candidates whichqvals
cat candidates
Multiple Environmental Variables
for (i in 1:ncol(env_vars)) {
pvals <- lfmm2.test(lfmm_result, input = genotypes, env = env_vars,
full = TRUE, genomic.control = TRUE)
qvals_i <- qvalue(pvals$pvalues[, i])$qvalues
cat('Variable', i, '- candidates (q<0.05):', sum(qvals_i < 0.05), '\n')
}
pcadapt Outlier Detection
Detects loci under selection based on Mahalanobis distances from principal components, without requiring environmental data:
library(pcadapt)
geno <- read.pcadapt('genotypes.bed', type = 'bed')
x_scree <- pcadapt(geno, K = 20)
plot(x_scree, option = 'screeplot')
x <- pcadapt(geno, K = 3)
plot(x, option = 'qqplot')
plot(x, option = 'manhattan')
library(qvalue)
qvals <- qvalue(x$pvaluesqvalues
outliers whichqvals
cat outliers
OutFLANK Fst-Based Selection Scan
Identifies Fst outliers by fitting a chi-squared distribution to the trimmed Fst distribution:
library(OutFLANK)
fst_mat <- MakeDiploidFSTMat(SNPmat = genotype_matrix,
locusNames = snp_names,
popNames = pop_assignments)
outflank_result <- OutFLANK(fst_mat, LeftTrimFraction = 0.05,
RightTrimFraction = 0.05, Hmin = 0.1,
NumberOfSamples = length(unique(pop_assignments)),
qthreshold = 0.05)
OutFLANKResultsPlotter(outflank_result, withOutliers = TRUE,
NoCorr = TRUE, Hmin
outlier_idx whichoutflank_resultresultsOutlierFlag
cat outlier_idx
RDA-Based GEA
Redundancy analysis detects multilocus adaptive signatures correlated with environment:
library(vegan)
allele_freq[is.na(allele_freq)] <- apply(allele_freq, 2,
function(x) mean(x, na.rm = TRUE))[col(allele_freq)[is.na(allele_freq)]]
rda_result <- rda(allele_freq ~ temperature + precipitation + altitude +
Condition(as.matrix(q_matrix)), data = env_data)
anova(rda_result, permutations = 999
loadings scoresrda_result choices display
zscores applyloadings x x meanx sdx
rda_candidates whichapplyzscores
cat rda_candidates
gradientForest: Allele Turnover Prediction
Predicts how allele frequencies shift along environmental gradients using random forests:
library(gradientForest)
library(terra)
gf <- gradientForest(cbind(env_predictors, allele_data),
predictor.vars = colnames(env_predictors),
response.vars = colnames(allele_data),
ntree = 500, trace = FALSE)
plot(gf, plot.type = 'Overall.Importance')
plot(gf, plot.type = 'Cumulative.Importance')
current <- rast('bioclim_current.tif')
future <- rast('bioclim_2070_ssp585.tif'
current_vals extractcurrent sampling_coords
future_vals extractfuture sampling_coords
current_transformed predictgf current_vals
future_transformed predictgf future_vals
genetic_offset rowSumscurrent_transformed future_transformed
Environmental Data Extraction with terra
library(terra)
bioclim <- rast('wc2.1_30s_bio.tif')
coords_vect <- vect(coords, geom = c('longitude', 'latitude'), crs = 'EPSG:4326')
env_values <- extract(bioclim, coords_vect)
Related Skills
- conservation-genetics - Population genetic health assessment
- community-ecology - Environmental gradient analysis for species
- population-genetics/selection-statistics - Selection scans in human data
- population-genetics/population-structure - Population structure inference
- variant-calling/vcf-basics - VCF input preparation