R/CNV_by_Method.R

Defines functions CNV.by.method

Documented in CNV.by.method

#' CNV generated by given method
#'
#' @param CNV.input Grange object
#' @param gene.name string
#' @param pid string
#' @param legend int
#' @param out.dir string
#' @param file.type string
#' @param pixel.per.cnv int
#' @param color list
#' @param gene.anno string
#' @param start.gene string
#' @param end.gene string
#' @param sort.method string
#' @param color.method string
#'
#' @return list variable
#'
#'
#' @export

CNV.by.method <- function(CNV.input,gene.name,title,legend,
                          out.dir,file.type,pixel.per.cnv,color,cnv.type,
                          gene.anno,start.gene,end.gene,sort.method,color.method,zoomed,
                          path,format,SaveAsObject,orient,drop.low.amp){

  CNV_1 <- CNV.input@matrix
  f.score <- CNV.input@gene_score
  gene.name <- CNV.input@gene_name


  t_gene_start = CNV.input@t_gene_start
  t_gene_end = CNV.input@t_gene_end


  # solid parameters
  chrom = as.vector(seqnames(CNV_1))
  start.CNV=start(CNV_1)
  end.CNV=end(CNV_1)

  # initail
  index <- (end.CNV - start.CNV) < 100000000 # only events shorter than 10 M
  m <- sum(index)
  startPos <- start.CNV[index]
  endPos <- end.CNV[index]

  if(missing(sort.method) & missing(color.method)){
    sort.method = "length"
    color.method = "cohort"
  }
  if(missing(sort.method)){sort.method = color.method}
  if(missing(color.method)){color.method = sort.method}

  if(missing(path)){path=""}
  if(missing(format)){format="tiff"}
  if(missing(SaveAsObject)){SaveAsObject=TRUE}
  if(missing(orient)){orient="v"}
  if(missing(drop.low.amp)){drop.low.amp=TRUE}

  #if(sort.method=="length"){color.method = "cohort"}
  score = CNV_1$Score
  cohort = CNV_1$Cohort
  pids = CNV_1$PID

  score = score[index]
  cohort = cohort[index]
  pids = pids[index]


  rescore <- unlist(lapply(score,MapPloidyClasses))
  score.values <- as.character(sort(unique(rescore)))
  n <- length(unique(rescore))

  if(sort.method=="length" & color.method=="length"){
    rescore <- rep(100000000,m)
    score.values <- as.character(sort(unique(rescore)))
    n <- length(unique(rescore))
  }

  if(sort.method=="length" & color.method=="ploidy"){
    if(missing(score)){score <- rep(100000000,length(index))}  # if no argument is given --> score is 4 (diploid)
    rescore <- unlist(lapply(score,MapPloidyClasses))
    score.values <- as.character(sort(unique(rescore)))
    n <- length(unique(rescore))
  }

  if(sort.method=="ploidy" & color.method=="ploidy"){
    # if(missing(score)){score <- rep(100000000,length(index))}  # if no argument is given --> score is 4 (diploid)
    rescore <- unlist(lapply(score,MapPloidyClasses))
    score.values <- as.character(sort(unique(rescore)))
    n <- length(unique(rescore))
  }




  if(1>0){
    if(missing(gene.name)){gene.name <- "geneX"}
    #gene.name = gene_name
    if(missing(cnv.type)){cnv.type <- "del"}
    if(missing(title)){
      if(missing(pids)){
        title <- cnv.type
      }else{
        title <- paste(gene.name,":",m," ",cnv.type," events from",length(unique(pids[index])),"samples") } # normal title genereated when pids given
    }else{
      if(missing(pids)){
        title <- title
      }else{
        title <- paste(gene.name,":",m," ",cnv.type," events from",length(unique(pids[index])),"samples")
      } # normal title genereated when pids given
    }
    if(missing(legend)){legend <- "pie"}
    if(missing(pixel.per.cnv)){
      pixel.per.cnv <- 200/m
    }  ## better a equation dependened on the number of CNVs (index!)
    ## color ------------------------------------------------------------------------------------------------------------------------------------------------------------
    if(missing(color)){
      color <- "1"
    }else{
      color <- color
    }

    ## where to plot?----------------------------------------------------------------------------------------------------------------------------------------------------

    if(missing(gene.anno)){
      gene.anno <- "missing"
    }

    if(missing(start.gene) & gene.anno == TRUE){
      print("start.gene argument is missing")
    }

    if(missing(end.gene) & gene.anno == TRUE){
      print("end.gene argument is missing")
    }
  }


  if(sort.method=="length"){
    print("sort.method=length!!!")
    sorting <- order(endPos - startPos) # sort by length
  }else if(sort.method=="cohort"){
    if(missing(cohort)){
      print("use CNV.by.ploidy or CNV.by.length functions")
    }else{
      #print(length(cohort))
      cohort <- cohort[index]
      #print(length(cohort))
      #print(length(endPos))
      #print(length(startPos))

      sorting <- order(cohort,endPos - startPos)
      #print("sorted by cohort")
    }
  }else if(sort.method=="ploidy"){
    sorting <- order(rescore,endPos - startPos) # sort by score and then by length, sort by ploidy
  }else if(sort.method=="length" & color.method=="cohort"){
    sorting <- order(endPos - startPos) # sort by length, color by cohort
    cohort <- cohort[index]
  }else if(sort.method=="length" & color.method=="ploidy"){
    sorting <- order(endPos - startPos) # sort by length, color by ploidy
  }

  if(missing(start.gene)){start.gene <- gene.name}
  if(missing(end.gene)){end.gene <- gene.name}


  if(missing(zoomed)){zoomed <- "global"}
  file.type="default"
  out.dir="default"
  out.fp <- out.dir


  paralist <- list("gene.name"=gene.name,"cnv.type"=cnv.type,"title"=title,"legend"=legend,
                   "file.type"=file.type,"out.dir"=out.dir,"pixel.per.cnv"=pixel.per.cnv,
                   "color"=color,"sorting"=sorting,"start.gene"=start.gene,"end.gene"=end.gene,"gene.anno"=gene.anno,
                   "chrom"=chrom,"start.CNV"=start.CNV,"end.CNV"=end.CNV,"rescore"=rescore,
                   "index"=index,"m"=m,"startPos"=startPos,"endPos"=endPos,
                   "cohort"=cohort,"pids"=pids,
                   #"sorting.color"=sorting.color,
                   "sort.method"=sort.method,"color.method"=color.method,
                   "t_gene_start" = t_gene_start,"t_gene_end" = t_gene_end,
                   "f.score"=f.score,"zoomed"=zoomed,"orient"=orient,"drop.low.amp"=drop.low.amp,
                   "format"=format,"path"=path,"SaveAsObject"=SaveAsObject)
  return(paralist)
}
chenhong-dkfz/tornado.test.1 documentation built on Dec. 28, 2021, 7:28 p.m.