Scripts/Demultiplexing Script
From Taiwan beetles
#==============================================================================
# PREPARING DEMULTIPLEXING FILE
#==============================================================================
# We need the file with five columns going like this:
# 1. Sample name
# 2. fwd primer barcode (= tag)
# 3. rev primer barcode (= tag)
# 4. fwd primer sequence
# 5. rev primer sequence
# The text file SHOULD NOT have column names!
# FILES NEEDED TO PREPARE THE DEMULTIPLEX FILE:
# 1. List of primers (Primers_list.txt): name, tag, sequence as separate columns
# 2. Plate plan for reverse primers (Primers_plate.txt): import from Excel including name of columns (1-12) and rows (A-H)
# 3. Plate plan for all PCR plates in one file: import by Ctrl+C from Excel including name of columns (1-12) and rows (A-H)
# Set working directory with all files ready
setwd("D:/Projects/Taiwan Leaf Litter Barcoding/LabWork_Kaohsiung/___SEQUENCING/2026_June")
### ----------------- READING FILES
# Read list of primers
# (should be a tab-delimited text file or Excel sheet)
primerNames <- read.table("Primers_list.txt", header=TRUE, sep="\t")
primerNames <- read.table(file="clipboard", header=TRUE, sep="\t")
primerNames
# Read plan of rev primers plate
# (should be a tab-delimited text file or Excel sheet)
primersPlate <- read.table("Primers_plate.txt", header=TRUE, sep="\t")
primersPlate <- read.table(file="clipboard", header=TRUE, sep="\t", row.names = 1)
colnames(primersPlate) <- c(1:12)
primersPlate
# Read plate maps from Excel
# (with stripe numbers in the first row, and stripe position in first column,
# plate name in upper left corner, all plates one after another without empty
# rows)
allPlates <- read.table(file="clipboard", header=FALSE, sep="\t")
numberPlates <- nrow(allPlates)/9
PlateFirstRows <- ((0:(numberPlates-1))*9)+1
plateNames <- allPlates[PlateFirstRows,1]
plateList <- list()
for (i in 1:numberPlates){
plate <- allPlates[(PlateFirstRows[i]+1):(PlateFirstRows[i]+8),2:13]
plateList[[i]]<-plate
names(plateList)[i]<-plateNames[i]
}
# Read list of plates and their forward primers
# (from Excel sheet)
List_of_Plates <- read.table(file="clipboard", header=TRUE, sep="\t")
List_of_Plates658 <- List_of_Plates[List_of_Plates$Length==658,]
List_of_Plates407 <- List_of_Plates[List_of_Plates$Length==407,]
List_of_Plates313 <- List_of_Plates[List_of_Plates$Length==313,]
#Check if names of plates in list and map correspond
List_of_Plates658[,1] %in% plateNames
List_of_Plates407[,1] %in% plateNames
List_of_Plates313[,1] %in% plateNames
### ----------------- PREPARING TABLE "SAMPLE - FWD - REV"
# This part makes a table of sample / fwd primer name / rev primer name for plates
sample_plate_primer = data.frame(matrix(nrow=0, ncol=6))
colnames (sample_plate_primer) = c("rowNo", "colNo", "sample", "plate", "fwd_primer", "rev_primer")
# SELECT WHICH PLATES YOU WANT TO USE HERE:
selectedPlates <- List_of_Plates658
namesOfPlates <- selectedPlates[,1]
row=1
for (k in 1:nrow(selectedPlates)){
whichPlate <- which(names(plateList) %in% namesOfPlates[k])
for (j in 1:8){
for (i in 1:12){
rowNo = j
colNo = i
sample = plateList[[whichPlate]][j,i]
plateNo = plateNames[whichPlate]
primer = primersPlate[j,i]
sample_plate_primer[row,] = c(rowNo, colNo, sample, plateNo, selectedPlates[k,3], primer)
row = row+1
}
}
}
sample_plate_primer
# This part keeps only the needed columns and deletes the rows for BLANKS or empty
# cells (adapt the "BLANK" text if you mark blanks in a different way in the PCR
# plate plan)
sample_primers <- sample_plate_primer [,c(3,5,6)]
sample_primers<-sample_primers[!(sample_primers$sample==""),]
sample_primers<-sample_primers[!(sample_primers$sample=="BLANK"),]
sample_primers<-sample_primers[!is.na(sample_primers$sample),]
# sample_primers should be the final list of samples and their fwd and rev primer names
sample_primers
nrow(sample_primers)
# checking if all samples are unique (to prevent problems with same sample in multiple plates)
duplicated(sample_primers[,1])
which(duplicated(sample_primers[,1]))
sample_primers[which(duplicated(sample_primers[,1])),1]
# ALTERNATIVE: load the sample-primer table done by yourself
# format: sample, fwd_primer, rev_primer
# sample_primers <- read.table(file = "clipboard", sep = "\t", header=TRUE)
### ----------------- REPLACING PRIMER NAMES BY PRIMER SEQUENCE AND TAG
# This part reads the tag and primer sequence for each fwd and rev primer for each sample
# and makes the final demultiplexing table
sample_primers["fwd_barcode"] <- NA
sample_primers["rev_barcode"] <- NA
sample_primers["fwd_seq"] <- NA
sample_primers["rev_seq"] <- NA
len = length(sample_primers[,1])
for (n in 1:len){
searched_fwd_primer <- sample_primers[n,"fwd_primer"]
searched_rev_primer = sample_primers[n,"rev_primer"]
primFWD = primerNames[(primerNames$Name==searched_fwd_primer),]
primREV = primerNames[(primerNames$Name==searched_rev_primer),]
sample_primers[n,"fwd_barcode"]=primFWD[1,"Tag"]
sample_primers[n,"rev_barcode"]=primREV[1,"Tag"]
sample_primers[n,"fwd_seq"]=primFWD[1,"Basic.Primer.Sequence"]
sample_primers[n,"rev_seq"]=primREV[1,"Basic.Primer.Sequence"]
}
demultiplex = sample_primers[,c(1,4:7)]
# This is the final demultiplexing file
demultiplex
### ----------------- EXPORTING DEMULTIPLEXING FILE
# Exporting the file to the txt
# CHANGE THE FILE NAME as you wish!
write.table(demultiplex, file="June2026_demultiplex_658.txt", quote=FALSE, sep=",", row.names=FALSE, col.names=FALSE)