Skip to content

Instantly share code, notes, and snippets.

View DrK-Lo's full-sized avatar

Katie Lotterhos DrK-Lo

View GitHub Profile
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],)}
@DrK-Lo
DrK-Lo / convert VCF to 012 format
Created September 10, 2024 12:02
This code in R takes a VCFR object as input and converts it to a 012 geno format
#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)
#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){
@DrK-Lo
DrK-Lo / ggplot_theme_lotterhos
Created December 9, 2022 20:04
GGplot theme Lotterhos
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
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)
@DrK-Lo
DrK-Lo / Install_ifNeeded.R
Last active June 9, 2021 07:46
Easy code for installing packages in R (if not installed) and calling their libraries
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])}
}