cutadapt_summary_pe.R 7.23 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/env Rscript
#' # Quality Control Summary for `r getwd()`
##+ echo=FALSE, message=FALSE

## Note This script is supposed to be knitr::spin'ed

7 8
devtools::source_url("https://git.mpi-cbg.de/bioinfo/datautils/raw/v1.46/R/core_commons.R")
devtools::source_url("https://git.mpi-cbg.de/bioinfo/datautils/raw/v1.46/R/ggplot_commons.R")
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
load_pack(knitr)

## can we access variables from the parent spin.R process?
#echo("rscript is ", r_script)

argv = commandArgs(TRUE)
#echo("argv is ", argv)

#if(str_detect(argv[1], "fastqc_summary")) argv <- argv[-1]

if(length(argv) != 1){
  stop("Usage: cutadapt_summary.R <directory with cutadapt log files>")
}

baseDir=argv[1]
#baseDir="."


if(is.na(file.info(baseDir)$isdir)){
  stop(paste("directory '", baseDir,"'does not exist"))
}

# baseDir="/home/mel/MPI-Bioinf/Project1_reads/141126_cutadapt_logs"


logDataFiles <- list.files(path=file.path(baseDir, ".logs"), pattern="__ca__.*.out.log", full.names=TRUE, recursive=T)

#echo("files are", logDataFiles)

#' ## General information
info1=readLines(pipe( paste("grep -F 'cutadapt' ", logDataFiles[1]) ))
echo(info1)
# info2=readLines(pipe( paste("grep -F 'Maximum error rate' ", logDataFiles[1]) ))
# echo(info2)
info3=readLines(pipe( paste("grep -F 'adapters with' ", logDataFiles[1]) ))
echo(info3)

#' ## Cutadapt Parameters:
parameters=readLines(pipe( paste("grep -F 'Command line parameters' ", logDataFiles[1]) ))

# #' cutadatpt parameters were`r parameters`

#' #### Some explanations:
if (grepl("-a", parameters) ==TRUE)
  echo("-a indicates that the following is a 3' end adapter.")
if (grepl("-g", parameters) ==TRUE)
  echo("-g indicates that the following is a 5' end adapter.")
if (grepl("-b", parameters) ==TRUE)
  echo("-b: indicates that the adapter is located at the 3' or 5' end (both possible).")
if (grepl("-m", parameters) ==TRUE)
  echo("Reads shorter than -m bases are thrown away.")
if (grepl("-q:", parameters) ==TRUE)
  echo("Quality trimming is done with a threshold specified after -q.")
if (grepl("-p:", parameters) ==TRUE)
  echo("option 'paired output' is used.")
if (grepl("-e:", parameters) ==TRUE)
  echo("-e changes the error tolerance. (The default maximum error rate is 0.1)")
if (grepl("-O:", parameters) ==TRUE)
  echo("The minimum overlap length is changed using -O.")
if (grepl("-N", parameters) ==TRUE)
  echo("Wildcard characters in the adapter are enabled by -N.")

#' #### For more detailed information on cutadapt go to https://cutadapt.readthedocs.org/en/latest/index.html


tidySamples  <- function(sample) str_replace(sample, ".*__ca__", "")

#' ## Trimming Overview
readSummaryStats <- function(logFile){
    # DEBUG logFile=logDataFiles[1]
  data.frame(
    Run=sub("^([^.]*).*", "\\1", basename(logFile)) %>% tidySamples,
81 82 83 84 85 86 87
    No_of_processed_Reads=paste("grep -F 'Total read pairs processed' ", logFile )  %>% pipe() %>% readLines() %>% str_split(fixed(":")) %>% unlist() %>% {.[2]} %>% str_trim %>% str_replace(",", "") %>% as.numeric(),
    No_of_processed_Bases=paste("grep -F 'Total basepairs processed' ", logFile )  %>% pipe() %>% readLines() %>% str_replace_all(",", "") %>% strsplit( "[^0-9]+") %>% unlist() %>% as.numeric() %>% {.[2]} ,
  # Trimmed_Reads=(paste("grep -F 'Trimmed reads' ", logFile )  %>% pipe() %>% readLines() %>% strsplit( "[^0-9\\.]+") %>% unlist() %>% as.numeric())[3],

  Quality_trimmed=paste("grep -F 'Quality-trimmed' ", logFile )  %>% pipe() %>% readLines() %>%  str_replace_all(",", "") %>% strsplit( "[^0-9\\.]+") %>% unlist() %>% as.numeric()%>% {.[3]},
    # Trimmed_Bases=(paste("grep -F 'Trimmed bases' ", logFile )  %>% pipe() %>% readLines() %>% strsplit( "[^0-9\\.]+") %>% unlist() %>% as.numeric())[4],
    Too_short_Reads=paste("grep -F 'too short' ", logFile )  %>% pipe() %>% readLines()  %>%  str_replace_all(",", "")%>% strsplit( "[^0-9\\.]+") %>% unlist() %>% as.numeric()%>% {.[3]}
88 89 90 91
  )
}
trimmingStats <- logDataFiles %>% map_df(readSummaryStats)

92
trimmingStats %>% table_browser()
93 94 95 96

write_tsv(trimmingStats, file="cutadapt_summary.trimmingStats.txt")
#'  [Trimming Statistics](cutadapt_summary.trimmingStats.txt)

97 98
# myfun <- function(x) x/100
# trimmingStats <- mutate_each(trimmingStats, funs(myfun), Trimmed_Reads, Quality_trimmed, Trimmed_Bases, Too_short_Reads)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

#+ fig.height=nrow(trimmingStats)/3, fig.width=12
require(scales)
ggplot(trimmingStats, aes(Run, No_of_processed_Reads)) + geom_bar(stat='identity') + coord_flip() + scale_y_continuous(labels=comma)
#ggplot(trimmingStats, aes(Run, No_of_processed_Bases)) + geom_bar(stat='identity') + coord_flip() + scale_y_continuous(labels=comma)

ggplot(trimmingStats, aes(Run, Trimmed_Reads)) +
    geom_bar(stat='identity') +
    coord_flip() +
    scale_y_continuous(labels=percent) +
    ## other layer with quality trimmed ones
    geom_bar(aes(Run, Quality_trimmed), stat='identity', color='red', alpha=0.4) +
    ylab("Trimmed reads proportion (quality-related colored in in red)")

#ggplot(trimmingStats, aes(Run, Quality_trimmed)) + geom_bar(stat='identity') + coord_flip() + scale_y_continuous(labels=percent)


#ggplot(trimmingStats, aes(Run, Trimmed_Bases)) + geom_bar(stat='identity') + coord_flip() + scale_y_continuous(labels=percent)
ggplot(trimmingStats, aes(Run, Too_short_Reads)) +
    geom_bar(stat='identity') +
    coord_flip() +
    scale_y_continuous(labels=percent) +
    ggtitle("Discarded Reads Proportion")



#' ## Adapter trimming statistics
readAdapterStats <- function(logFile){
  #browser()
  data.frame(
    Run=sub("^([^.]*).*", "\\1", basename(logFile)) %>% tidySamples,
    Adapter=(paste("grep -F '=== Adapter ' ", logFile )  %>% pipe() %>% readLines() %>% str_split_fixed("'", 3))[,2],
    Trimmed=(paste("grep -F '; Trimmed: ' ", logFile )  %>% pipe() %>% readLines() %>% str_split_fixed( "[^0-9]+", 6))[,5] %>% as.numeric(),
    Overlapped_at_5prime=(paste("grep -F 'overlapped the 5' ", logFile )  %>% pipe() %>% readLines() %>% str_split_fixed( "[^0-9]+", 2) )[,1] %>% as.numeric(),
    Overlapped_at_3prime=(paste("grep -F 'overlapped the 3' ", logFile )  %>% pipe() %>% readLines() %>% str_split_fixed( "[^0-9]+", 2) )[,1] %>% as.numeric()
  )
}
adapterTrimmingStats <- logDataFiles %>% map_df(readAdapterStats)

#+ results = 'asis'
adapterTrimmingStats %>% head() %>% kable()

#+
write_tsv(adapterTrimmingStats, file="cutadapt_summary.adapterTrimmingStats.txt")
#'  [Adapter Statistics](cutadapt_summary.adapterTrimmingStats.txt)

#with(adapterTrimmingStats, Trimmed==Overlapped_at_3prime+Overlapped_at_5prime)
#ggplot(adapterTrimmingStats, aes(Run, Trimmed)) + geom_bar(stat='identity') + facet_wrap(~Adapter) + coord_flip() + scale_y_continuous(labels=comma)
#ggplot(adapterTrimmingStats, aes(Run, Overlapped_at_5prime)) + geom_bar(stat='identity') + facet_wrap(~Adapter) + coord_flip() + scale_y_continuous(labels=comma)
#ggplot(adapterTrimmingStats, aes(Run, Overlapped_at_3prime)) + geom_bar(stat='identity') + facet_wrap(~Adapter) + coord_flip() + scale_y_continuous(labels=comma)

#+ fig.height=nrow(trimmingStats), fig.width=12
adapterTrimmingStats %>% select(-Trimmed) %>% melt() %>%
    mutate(overlap_at=str_replace(variable, "Overlapped_at_", "")) %>%
    ggplot(aes(Run, value, fill=overlap_at)) +
    geom_bar(stat='identity') +
    facet_wrap(~Adapter) +
    coord_flip() +
    scale_y_continuous(labels=comma) +
    ylab("# num reads") +
    ggtitle("Adapter trimming by overlap")