CS-6643-Bioinformatics-Lab-9/Schrick-Noah_CS-6643_Lab-9.R

52 lines
1.5 KiB
R

# Lab 9 for the University of Tulsa's CS-6643 Bioinformatics Course
# Pairwise Sequence Alignment with Dynamic Programming
# Professor: Dr. McKinney, Fall 2022
# Noah L. Schrick - 1492657
## Set Working Directory to file directory - RStudio approach
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#### Part A: Specifying the Input
## Score Rules and Seqs
x_str <- "ATAC" # side sequence
y_str <- "GTGTAC" # top sequence
match_score <- 3
mismatch_score <- -1
gap_penalty <- -4
## Substitution Matrix
dna.letters<-c("A","C","G","T")
num.letters <- length(dna.letters)
S<-data.frame(matrix(0,nrow=num.letters,ncol=num.letters)) # data frame
rownames(S)<-dna.letters; colnames(S)<-dna.letters
for (i in 1:4){
for (j in 1:4){
if(dna.letters[i]==dna.letters[j]){
S[i,j]<- match_score
}
else{
S[i,j]<- mismatch_score
}
}
}
#### Part B: Alignment Score Matrix (F) and Traceback Matrix (T)
x <- unlist(strsplit(x_str, ""))
y <- unlist(strsplit(y_str, ""))
x.len <- length(x)
y.len <- length(y)
Fmat<-matrix(0,nrow=x.len+1,ncol=y.len+1)
Tmat<-Fmat # 0's to start
rownames(Fmat)<-c("-",x); colnames(Fmat)<-c("-",y)
rownames(Tmat)<-c("-",x); colnames(Tmat)<-c("-",y)
# create first row and column
Fmat[,1]<- seq(from=0,len=x.len+1,by=-abs(gap_penalty))
Fmat[1,]<- seq(from=0,len=y.len+1,by=-abs(gap_penalty))
Tmat[,1]<- rep(2,x.len+1) # 2 means align with a gap in the upper seq
Tmat[1,]<- rep(3,y.len+1) # 3 means align with a gap in the side seq