R/library_reporter.R

Defines functions library_reporter

Documented in library_reporter

#' Generating a summary of the spectral library
#'
#' The function generates a report from input spectral library
#'
#' @param library A list generated by the function library_generator() or the name of mgf spectral library file
#' @export
#'
#' @examples
#'
#' data(DRUG_THERMO_LIBRARY)
#' library_reporter(library2)
#'
#' @importFrom MSnbase fData readMgfData
#'
#'
library_reporter<-function(library){

  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()!")
    }}

  #####################################
  ### Reading from spectral library:
  #####################################

  if (is.character(library)){ # If input is a mgf file name
    library=readMGF2(library)}

  metadata = library$metadata
  spectrum_list = library$sp

  prec_mz = as.numeric(metadata$PEPMASS)
  prec_rt = as.numeric(metadata$RT)
  IDlist = metadata$ID
  IDs = unique(IDlist)

  ####################
  ### Basic summaries:
  ####################

  MS1_num = sum(metadata$MSLEVEL==1)
  MS2_num = sum(metadata$MSLEVEL==2)
  cat("The library contains", MS1_num, "MS1 scans and", MS2_num, "MS2 scans of", length(IDs), "compounds.")
  cat("\n")

  valid_2 = 0
  valid_1_2 = 0
  for (id in IDs){
    sub_levels = metadata$MSLEVEL[which(IDlist==id)]
    if (2 %in% sub_levels){valid_2 = valid_2 +1}
    if ((1 %in% sub_levels) & (2 %in% sub_levels)){valid_1_2 = valid_1_2 +1}}
  cat(valid_1_2, "compounds possess both MS1 and MS2 scans.")
  cat("\n")
  cat(valid_2, "compounds hold MS2 scans.")
  cat("\n")

  cpd_duplicated_1 = c() # Compounds that contain more than 1 MS1 or MS2 scans
  cpd_duplicated_2 = c() # Compounds that contain more than 1 MS1 or MS2 scans
  for (id in IDs){
    sub_levels = metadata$MSLEVEL[which(IDlist==id)]
    if (sum(sub_levels==1)>1){cpd_duplicated_1 = c(cpd_duplicated_1, id)}
    if (sum(sub_levels==2)>1){cpd_duplicated_2 = c(cpd_duplicated_2, id)}
  }
  cpd_duplicated_1 = paste0(cpd_duplicated_1,collapse=" ")
  cpd_duplicated_2 = paste0(cpd_duplicated_2,collapse=" ")
  cat("Compounds",cpd_duplicated_1,"hold multiple MS1 scans")
  cat("\n")
  cat("Compounds",cpd_duplicated_2,"hold multiple MS2 scans")
  cat("\n")
}
daniellyz/MergeION documentation built on Oct. 19, 2022, 1:56 p.m.