Load packages
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.2
library(UniprotR)
## Warning: package 'UniprotR' was built under R version 4.3.2
library(Biostrings)
## Loading required package: BiocGenerics
##
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
##
## IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
##
## anyDuplicated, aperm, append, as.data.frame, basename, cbind,
## colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
## get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
## match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
## Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
## table, tapply, union, unique, unsplit, which.max, which.min
## Loading required package: S4Vectors
## Loading required package: stats4
##
## Attaching package: 'S4Vectors'
## The following object is masked from 'package:utils':
##
## findMatches
## The following objects are masked from 'package:base':
##
## expand.grid, I, unname
## Loading required package: IRanges
##
## Attaching package: 'IRanges'
## The following object is masked from 'package:grDevices':
##
## windows
## Loading required package: XVector
## Loading required package: GenomeInfoDb
## Warning: package 'GenomeInfoDb' was built under R version 4.3.2
##
## Attaching package: 'Biostrings'
## The following object is masked from 'package:base':
##
## strsplit
library(seqinr)
## Warning: package 'seqinr' was built under R version 4.3.2
##
## Attaching package: 'seqinr'
## The following object is masked from 'package:Biostrings':
##
## translate
library(ggfortify)
## Loading required package: ggplot2
library(forcats)
Function code chunk
pid_alignments <- function(accession_ids){
#Create a variable of the accession ids
ids <-(accession_ids)
#Retrieve sequences
df <- GetSequences(ids)
df
#first change the ids from being the rownames to being a column
df$accession_id <- row.names(df)
rownames(df) <- 1:nrow(df)
#Reduce uniprot_seqs dataframe to only contain the sequences and ids
df <- df[,c("accession_id", "Sequence")]
# Create a matrix of subject and query sequences
seq_mat <-cbind2(df[2:nrow(df),1:2], df[1,1:2])
seq_mat
# Create a loop that performs pairwise alignment of each combination and calculates the percentage identity of the result of each pairwise alignment.
#alignment_score <- c()
nameSeq1 <- c()
name_refSeq <- c()
alignments <- list()
for (i in 1:nrow(seq_mat)){
aligned_seqs <- pairwiseAlignment(AAStringSet(seq_mat[i,2]), AAStringSet(seq_mat[i,4]), substitutionMatrix = "BLOSUM50", gapOpening = -2, gapExtension = -8, scoreOnly = FALSE)
nameSeq1[i] <- c(as.character(seq_mat[i,1]))
name_refSeq[i] <- c(as.character(seq_mat[i,3]))
alignments[[i]] <- c(aligned_seqs)
pid_aln <-sapply(alignments, pid, type = "PID4")
#I have chosen PID4 this time but there are other options for calculating the percentage identity. See options below:
#"PID1": 100*(identicalpositions)/(alignedpositions+internalgappositions)
#"PID2": 100*(identicalpositions)/(alignedpositions)
#"PID3": 100*(identicalpositions)/(lengthshortersequence)
#"PID4": 100*(identicalpositions)/(averagelengthofthetwosequences)
}
#Create dataframe of sequence name and scores
aligned_df <- data.frame(name_refSeq, nameSeq1, pid_aln)
colnames(aligned_df) <- c("Primary_accession_id", "Query_accession_id", "Percent_Identity")
#Create Plot of PID
PID_plot <- ggplot(aligned_df, aes(x = fct_inorder(Query_accession_id), y = Percent_Identity))+
geom_col(fill= "turquoise3", width = 0.7)+
theme_bw()+
theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 10), plot.title = element_text(hjust = 0.5, size = 12)) +
labs(title = "Percent Identity plot of Pairwise alignments of Query sequences against Subject sequence",
x = "Query accession id",
y = "Percent Identity")
return(list(aligned_df,PID_plot))
}
Test that function works
#Test function
pid_alignments(c("A0A6P3H9Z6", "A0A3Q1MJB2", "Q08879", "A0A287AL89", "P23142", "A0A0G2JY81", "G1T0Q0", "A0A452F2R0", "Q8MJJ9", "A0A0D9QZ51", "A0A2K6SW02", "A0A2K5DTU8", "A0A2K5VCS0", "A0A2K6MZ25", "A0A2K6QYG8"))
## Please wait we are processing your accessions ...
## [[1]]
## Primary_accession_id Query_accession_id Percent_Identity
## 1 A0A6P3H9Z6 A0A3Q1MJB2 97.53623
## 2 A0A6P3H9Z6 Q08879 84.11893
## 3 A0A6P3H9Z6 A0A287AL89 84.92967
## 4 A0A6P3H9Z6 P23142 87.87219
## 5 A0A6P3H9Z6 A0A0G2JY81 80.78890
## 6 A0A6P3H9Z6 G1T0Q0 73.20955
## 7 A0A6P3H9Z6 A0A452F2R0 94.88104
## 8 A0A6P3H9Z6 Q8MJJ9 73.11321
## 9 A0A6P3H9Z6 A0A0D9QZ51 87.72694
## 10 A0A6P3H9Z6 A0A2K6SW02 75.40741
## 11 A0A6P3H9Z6 A0A2K5DTU8 86.95652
## 12 A0A6P3H9Z6 A0A2K5VCS0 73.65398
## 13 A0A6P3H9Z6 A0A2K6MZ25 74.10072
## 14 A0A6P3H9Z6 A0A2K6QYG8 74.56647
##
## [[2]]
