This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We have this list of IDs, and want to get the final column (right before the ".CEL" from the ID). | |
> s | |
[1] "A8_CviMVP_PlDNA_5__SG_1240__5c86242a_253268_H1.CEL" "A9_CviMVP_PlDNA_5__SG_1659__12a25bac_253268_A2.CEL" "A9_CviMVP_PlDNA_5__SG_1659__12a25bac_253268_A10.CEL" | |
This function splits the string by the "_". When doing so, the 11th value listed is the column we want and outputs eg. "H1.CEL" | |
Then we want to get rid of the ".CEL", so we substitute the ".CEL" with a "" | |
``` | |
getstring <- function(x){a<-strsplit(x,"_");sub(".CEL","",a[[1]][11],)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pl_vcf is read in with VCFR package | |
ext_vcfgt_num2 <- extract.gt(pl_vcf, as.numeric = FALSE) # extract the genotype matrix from the vcf fil | |
str(ext_vcfgt_num2) | |
# “0/0” --> 0 | |
# “0/1" or “1/0” --> 1 | |
# “1/1" is --> 2 | |
FirstAllele<- matrix(as.numeric(substring(ext_vcfgt_num2,1,1)), ncol=ncol(ext_vcfgt_num2)) #first character is first allele | |
SecondAllele<- matrix(as.numeric(substring(ext_vcfgt_num2,3,3)), ncol=ncol(ext_vcfgt_num2)) #third character is second allele | |
Genotype <- FirstAllele + SecondAllele | |
str(Genotype) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The genotype matrix has individuals in rows and mutations #in columns, with a 0,1, or 2 entered for the number of #alternate alleles in the diploid | |
# Let’s create a matrix with 5 individuals, 10 SNPS, and some missing data: | |
set.seed(345) | |
GEN = matrix(sample(0:2, 50, replace=TRUE), nrow=5, ncol=10) | |
GEN[5,1] <- NA | |
GEN | |
# Function to calculate the allele frequency of a column | |
af <- function(i, gen){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggtheme <- theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour="grey30"), axis.title = element_text(colour="grey20"), axis.text = (element_text(colour="grey30")), legend.title = element_text(colour="grey20"), legend.text = element_text(colour="grey30")) | |
to use it, just use: | |
ggplot(data) + geom_point(aes(x=x, y=y)) + ggtheme |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par(xpd=NA, oma=c(4,0,0,0), mar=c(2,4,0,0),mfrow=c(1,1)) | |
plot(1,1, xlim=c(-1,1), ylim=c(0,1)) | |
# This is for a plot with x-axis from -1 to 1 and y axis from 0 to 1. It plots a legend in the bottom margin | |
legend(-1,-0.25, legend=c("high sal.", "low sal."), bty="n", adj=0, fill=c("grey", NA), horiz=TRUE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
packages_needed <- c("raster", "FNN", "RColorBrewer", "colorRamps", "adehabitatLT", | |
"data.table", "tidyverse", "fields", "ggplot2", "hexbin", | |
"rgdal", "tmap", "gstat", "sp", "maptools", "sf", "fasterize", | |
"fansi", "raster", "tmap", "gstat", "ContourFunctions","ash" | |
) | |
for (i in 1:length(packages_needed)){ | |
if(!(packages_needed[i] %in% installed.packages())){install.packages(packages_needed[i])} | |
} |