You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.5KB

  1. ###################
  2. ## cn.mops ##
  3. ## Jiyang Zhang ##
  4. ## 2019.01.21 ##
  5. ###################
  6. #
  7. #1 Introduction
  8. ##The cn.mops package is part of the Bioconductor (http://www.bioconductor.org) project.
  9. ##The package allows to detect copy number variations (CNVs) from next generation sequencing (NGS) data sets based on a generative model.
  10. ##Please visit http://www.bioinf.jku.at/software/cnmops/cnmops.htmlfor additional information.
  11. #
  12. #2 Set up
  13. #
  14. ##2.1 load package
  15. library(cn.mops) #cn.mops_1.16.2
  16. #
  17. ##2.2 load options
  18. #
  19. ###2.2.1 Collect arguments
  20. args <- commandArgs(TRUE)
  21. #
  22. ###2.2.2 Help section
  23. #### Default setting when no arguments passed
  24. if(length(args) < 1) {
  25. args <- c("--help")
  26. }
  27. #### Help section
  28. if("--help" %in% args) {
  29. cat("
  30. The R Script
  31. Arguments:
  32. --Tumor=the name of the TUMOR sample BAMfile - string
  33. --Normal=the name of the NORMAL sample BAMfile - string
  34. --TumorSampleID=the name of the TUMOR sample - string
  35. --bed_file=BED (Browser Extensible Data) lines have four required fields included chr, start, end and gene annotation. - string
  36. --workDir=working directory - character
  37. --help - print this text
  38. Example:
  39. Rscript cn.mops.R --Tumor=TumorSample.bam --Normal=NormalSample.bam --TumorSampleID=TumorSampleID --bed_file=bedFile --workDir=./ \n\n") #--outputDir=./
  40. q(save="no")
  41. }
  42. #
  43. ####2.2.3 Parse arguments (we expect the form --arg=value)
  44. parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
  45. argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
  46. argsL <- as.list(as.character(argsDF$V2))
  47. names(argsL) <- argsDF$V1
  48. args<-argsL
  49. #
  50. ## Arg1 default
  51. if(is.null(args$Tumor)) {
  52. stop("Tumor sample bamFile is missing")
  53. }else{
  54. Tumor=args$Tumor
  55. }
  56. #
  57. ## Arg2 default
  58. if(is.null(args$Normal)) {
  59. stop("Normal sample bamFileis missing")
  60. }else{
  61. Normal=args$Normal
  62. }
  63. #
  64. ## Arg3 default
  65. if(is.null(args$TumorSampleID)) {
  66. stop("TumorSampleID missing")
  67. }else{
  68. TumorSampleID=args$TumorSampleID
  69. }
  70. #
  71. ## Arg4 default
  72. if(is.null(args$bed_file)) {
  73. stop("bed_file is missing")
  74. }else{
  75. bed_file=args$bed_file
  76. }
  77. #
  78. ## Arg5 default
  79. if(is.null(args$workDir)) {
  80. stop("workDir is missing")
  81. }else{
  82. workDir=args$workDir
  83. }
  84. #
  85. #3 Run cn.mops
  86. # Tumor <- c("Illumina_pt2_B1700.chr20_X.sorted.deduped.bam")
  87. # Normal <- c("Illumina_pt2_B17NC.chr20_X.sorted.deduped.bam")
  88. # workDir <- "/home/zhangjiyang/shiJzhiP/shijianzhiP2/scripts"
  89. # outputDir <- "/home/zhangjiyang/shiJzhiP/shijianzhiP2/scripts"
  90. # bed_file <- "Illumina_pt2_exonanno_exonfrom1_chr20_X.bed"
  91. #
  92. ##3.1 BAM files and bed file as input.
  93. BAMFiles <- c(Tumor,Normal)
  94. segments <- read.table(paste0(workDir,"/", bed_file),sep="\t",as.is=TRUE)
  95. #
  96. ##3.2 Get read counts from BAM.
  97. gr <- GRanges(segments[,1],IRanges(segments[,2],segments[,3]))
  98. #
  99. ##3.3 The result object
  100. sample <- getSegmentReadCountsFromBAM(BAMFiles,GR=gr,mode="paired")
  101. resRef <- referencecn.mops(cases=sample[,1],controls=sample[,2],
  102. classes=c("CN0", "CN1", "CN2", "CN3", "CN4", "CN5", "CN6",
  103. "CN7","CN8","CN16","CN32","CN64","CN128"),
  104. I = c(0.025, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 8, 16, 32, 64),
  105. segAlgorithm="DNAcopy")
  106. result<- calcIntegerCopyNumbers(resRef)
  107. CNVs <- as.data.frame(cnvs(result))
  108. colnames(segments) <- c("chr","start","end","anno")
  109. colnames(CNVs)[colnames(CNVs)=="seqnames"] <- c("chr")
  110. bed.anno <- segments
  111. bed.anno$start_idx <- paste(bed.anno$chr,bed.anno$start,sep="_")
  112. bed.anno$end_idx <- paste(bed.anno$chr,bed.anno$end,sep="_")
  113. CNV_number <- nrow(CNVs)
  114. Res <- data.frame()
  115. for(i in 1:CNV_number) {
  116. CNVs.start.idx <- paste(CNVs[i,]$chr,CNVs[i,]$start,sep="_")
  117. CNVs.end.idx <- paste(CNVs[i,]$chr,CNVs[i,]$end,sep="_")
  118. exon_start <- rownames(bed.anno[bed.anno$start_idx%in%CNVs.start.idx,])
  119. exon_end <- rownames(bed.anno[bed.anno$end_idx%in%CNVs.end.idx,])
  120. res <- CNVs[i,]
  121. res$gene <- unlist(strsplit(bed.anno[exon_start,]$anno,split = "_"))[1]
  122. res$exon_start <- unlist(strsplit(bed.anno[exon_start,]$anno,split = "_"))[2]
  123. res$exon_end <- unlist(strsplit(bed.anno[exon_end,]$anno,split = "_"))[2]
  124. Res <- rbind(Res,res)
  125. }
  126. Res <- data.frame(sampleName=Res$sampleName,chr=Res$chr,
  127. start=Res$start,end=Res$end,gene=Res$gene,
  128. exon_start=Res$exon_start,exon_end=Res$exon_end,
  129. CN=Res$CN)
  130. #
  131. ##3.4 Output
  132. write.table(Res,paste0(TumorSampleID,"cn.mops.res.txt"),sep="\t",
  133. col.names=T,row.names=F,quote=F)