Scripts/Demultiplexing Script

From Taiwan beetles
Revision as of 08:47, 17 June 2026 by Admin (talk | contribs) (Created page with "<syntaxhighlight lang="r"> #============================================================================== # 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 PREPA...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

<syntaxhighlight lang="r">

  1. ==============================================================================
  2. PREPARING DEMULTIPLEXING FILE
  3. ==============================================================================
  1. We need the file with five columns going like this:
  2. 1. Sample name
  3. 2. fwd primer barcode (= tag)
  4. 3. rev primer barcode (= tag)
  5. 4. fwd primer sequence
  6. 5. rev primer sequence
  7. The text file SHOULD NOT have column names!
  1. FILES NEEDED TO PREPARE THE DEMULTIPLEX FILE:
  2. 1. List of primers (Primers_list.txt): name, tag, sequence as separate columns
  3. 2. Plate plan for reverse primers (Primers_plate.txt): import from Excel including name of columns (1-12) and rows (A-H)
  4. 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)
  1. Set working directory with all files ready

setwd("D:/Projects/Taiwan Leaf Litter Barcoding/LabWork_Kaohsiung/___SEQUENCING/2026_June")

      1. ----------------- READING FILES
  1. Read list of primers
  2. (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

  1. Read plan of rev primers plate
  2. (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

  1. Read plate maps from Excel
  2. (with stripe numbers in the first row, and stripe position in first column,
  3. plate name in upper left corner, all plates one after another without empty
  4. 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]
 plateListi<-plate
 names(plateList)[i]<-plateNames[i]

}

  1. Read list of plates and their forward primers
  2. (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,]

  1. 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

      1. ----------------- PREPARING TABLE "SAMPLE - FWD - REV"
  1. 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")

  1. 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 = plateListwhichPlate[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

  1. This part keeps only the needed columns and deletes the rows for BLANKS or empty
  2. cells (adapt the "BLANK" text if you mark blanks in a different way in the PCR
  3. 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),]

  1. sample_primers should be the final list of samples and their fwd and rev primer names

sample_primers nrow(sample_primers)

  1. 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]

  1. ALTERNATIVE: load the sample-primer table done by yourself
  2. format: sample, fwd_primer, rev_primer
  3. sample_primers <- read.table(file = "clipboard", sep = "\t", header=TRUE)
      1. ----------------- REPLACING PRIMER NAMES BY PRIMER SEQUENCE AND TAG
  1. This part reads the tag and primer sequence for each fwd and rev primer for each sample
  2. 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)]

  1. This is the final demultiplexing file

demultiplex

      1. ----------------- EXPORTING DEMULTIPLEXING FILE
  1. Exporting the file to the txt
  2. CHANGE THE FILE NAME as you wish!

write.table(demultiplex, file="June2026_demultiplex_658.txt", quote=FALSE, sep=",", row.names=FALSE, col.names=FALSE) </syntaxhighlight>