#' Visualizing query spectrum matched to existing library
#'
#' The function generates the comparison plot between query spectrum and library
#'
#' @param library A list generated by the function library_generator() or library_similarity()$SELECTED
#' @param query_spectrum Two-column data matrix. Two columns represent m/z and intensity of query tandem spectrum
#' @param png.out Boolean. True if plotted mirror spectra are exported as png images!
#'
#' @return Comparing query spectrum to "hits" in the spectrum library via mirror plot
#'
#' @author Youzhong Liu, \email{Youzhong.Liu@uantwerpen.be}
#'
#' @importFrom MSnbase fData readMgfData
#' @importFrom tools file_ext
#' @importFrom stringr str_replace_all fixed
#' @importFrom OrgMassSpecR SpectrumSimilarity
#'
#' @export
library_visualizer_similarity<-function(library, id = library$metadata$ID[1],
query_spectrum=NULL, png.out=F){
options(stringsAsFactors = FALSE)
options(warn=-1)
#################
### Check inputs:
#################
if (missing(library)){
stop("Please provide the output of library_generator() or a .mgf file as input library!")}
if (is.character(library)){
if (file_ext(library)!="mgf"){
stop("The file extension of your input library must be mgf!")
}}
if (is.list(library)){
if (length(library)==2 & "complete" %in% names(library)){
library = library$complete
}
if (length(library)!=2 || (!is.list(library$sp)) || !is.data.frame(library$metadata)){
stop("Please make sure your input library is a valid output of library_generator()!")
}}
if (is.null(query_spectrum)){
stop("Please provide a 2 column query spectrum!")
} else {
if (ncol(query_spectrum)<2){
stop("Spectrum must have 2 columns m/z and intensity!")
}
}
###############################
### Preprocess query spectrum:
###############################
dat = query_spectrum[,1:2]
# Normalize, cut only masses smaller than precursor and filter background noise:
dat[,2]=dat[,2]/max(dat[,2])*100
#####################################
### Reading from spectral library:
#####################################
if (is.character(library)){ # If input is a mgf file name
library=readMGF2(library)}
if (nrow(library$metadata)==0){stop("The library for visualization is empty!")}
library1 = library_manager(library, query = paste0("ID =", id))$SELECTED
metadata = library1$metadata
spectrum_list = library1$sp
scan_max = max(as.numeric(metadata$SCANS)) # Most recent Scan selected
ind = which(as.numeric(metadata$SCANS)==scan_max)
metadata = metadata[ind,]
spectrum = spectrum_list[[ind]]
#################
### Plot results:
################
if (png.out){png(paste0("SIM_ID_", metadata$ID, ".png"), width = 700, height = 480)}
bottom.label = paste0("Library spectrum of ID = ", metadata$ID)
xlim = c(min(dat[,1])*0.8, max(dat[,1])*1.2)
SpectrumSimilarity(dat, spectrum, top.label="Query spectrum",bottom.label= bottom.label,xlim=xlim, print.graphic = T)
if (png.out){dev.off()}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.