R/OPF_10bit.R

Defines functions OPF_10bit

Documented in OPF_10bit

#' Overlapping Property Features_10bit (OPF_10bit)
#'
#' This group of functions (OPF Group) categorize amino acids in different groups based on the type.
#' This function includes 10 amino acid properties. OPF_10bit substitutes each amino acid with a 10-dimensional vector.
#' Each element of the vector shows if that amino acid locates in a special property category or not. '0' means that amino acid is not located in that property group and '1' means it is located.
#'
#'
#' @references Wei,L., Zhou,C., Chen,H., Song,J. and Su,R. ACPred-FL: a sequence-based predictor using effective feature representation to improve the prediction of anti-cancer peptides. Bioinformatics (2018).
#'
#' @note This function is provided for sequences with the same lengths.
#' Users can use 'txt' option in outFormat for sequences with different lengths.
#' Warning: If outFormat is set to 'mat' for sequences with different lengths, it returns an error.
#' Also, when output format is 'txt', label information is not shown in the text file.
#' It is noteworthy that 'txt' format is not usable for machine learning purposes if sequences have different sizes. Otherwise 'txt' format
#' is also usable for machine learning purposes.
#'
#' @param seqs is a FASTA file with amino acid sequences. Each sequence starts
#' with a '>' character. Also, seqs could be a string vector. Each element of the vector is a peptide/protein sequence.
#'
#'
#' @param label is an optional parameter. It is a vector whose length is equivalent to the number of sequences. It shows the class of
#' each entry (i.e., sequence).
#'
#' @param outFormat (output format) can take two values: 'mat'(matrix) and 'txt'. The default value is 'mat'.
#'
#' @param outputFileDist shows the path and name of the 'txt' output file.
#'
#'
#' @return The output is different depending on the outFormat parameter ('mat' or 'txt').
#' If outFormat is set to 'mat', it returns a feature matrix for sequences with the same lengths.
#' Number of columns for this feature matrix is equal to (length of the sequences)*10 and number of rows is equal to the number of sequences.
#' If outFormat is 'txt', all binary values will be written to a the output is written to a tab-delimited file. Each line in the file shows the binary format of a sequence.
#'
#'
#' @export
#'
#' @examples
#'
#' ptmSeqsADR<-system.file("extdata/",package="ftrCOOL")
#' ptmSeqsVect<-as.vector(read.csv(paste0(ptmSeqsADR,"/ptmVect101AA.csv"))[,2])
#' mat<-OPF_10bit(seqs = ptmSeqsVect,outFormat="mat")
#'
OPF_10bit<-function(seqs,label=c(),outFormat="mat",outputFileDist=""){

  if(length(seqs)==1&&file.exists(seqs)){
    seqs<-fa.read(seqs,alphabet="aa")
    seqs_Lab<-alphabetCheck(seqs,alphabet = "aa",label)

    seqs<-seqs_Lab[[1]]
    label<-seqs_Lab[[2]]
  }
  else if(is.vector(seqs)){

    seqs<-sapply(seqs,toupper)
    seqs_Lab<-alphabetCheck(seqs,alphabet = "aa",label)
    seqs<-seqs_Lab[[1]]
    label<-seqs_Lab[[2]]

  }else {
    stop("ERROR: Input sequence is not in the correct format. It should be a FASTA file or a string vector.")
  }


  numSeqs<-length(seqs)
  lenSeqs<-sapply(seqs, nchar)

  group<-list("Aromatic"= c("F","Y","W","H"),
               "Negative"= c("D","E"),
               "Positive"=        c("K", "H","R"),
               "Polar"=  c("N","Q","S","D","E","C","T","K","R","H","Y","W"),
               "Hydrophobic"= c("A","G","C","T","I","V","L","K","H","F","Y","W","M"),
               "Aliphatic"= c("I","V","L"),
               "Tiny"=   c("A","S","C","G"),
               "Charged"=  c("K","H","R","D","E"),
               "Small"=   c("P","N","D","T","C","A","G","S","V"),
               "Proline"=c("P"))

  properties<-c("Aromatic", "Negative",
                "Positive", "Polar", "Hydrophobic", "Aliphatic", "Tiny","Charged","Small","Proline")


  if(outFormat=="mat"){
    if(length(unique(lenSeqs))>1){
      stop("ERROR: All sequences should have the same length in 'mat' mode. For sequences with different lengths, please use 'txt' for outFormat parameter")
    }
    featureMatrix<-matrix(0, nrow = numSeqs, ncol = (lenSeqs[1]*10))
    tempN1<-rep(properties,lenSeqs[1])
    tempN2<-rep(1:lenSeqs[1],each=10)
    colnames(featureMatrix)<-paste0("pos",tempN2,"_",tempN1)

  for(n in 1:numSeqs){

    seq=seqs[n]

    aa=unlist(strsplit(seq,split = ""))

    vect<-c()
    for(a in aa)
    {
      g1 <- lapply(group, function(g) which(a %in% g))
      b=lapply(g1, function(x) length(x)>0)
      vect<-c(vect,as.numeric(b))
    }

    featureMatrix[n,]<-vect
  }
  row.names(featureMatrix)<-names(seqs)
  return(featureMatrix)
  }
  else{
    nameSeq<-names(seqs)
    for(n in 1:numSeqs){
      seq<-seqs[n]
      chars<-unlist(strsplit(seq,split = ""))
      vect<-c()
      for(a in aa)
      {
        g1 <- lapply(group, function(g) which(a %in% g))
        b=lapply(g1, function(x) length(x)>0)
        vect<-c(vect,as.numeric(b))
      }
      temp<-c(nameSeq[n],vect)
      temp<-paste(temp,collapse = "\t")
      write(temp,outputFileDist,append = TRUE)

    }
  }

}

Try the ftrCOOL package in your browser

Any scripts or data that you put into this service are public.

ftrCOOL documentation built on Nov. 30, 2021, 1:07 a.m.