-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfrag
More file actions
executable file
·102 lines (84 loc) · 6.27 KB
/
frag
File metadata and controls
executable file
·102 lines (84 loc) · 6.27 KB
1
2
3
4
5
6
7
8
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env Rscript
library(optparse)
dr.str = "
███████╗██████╗ █████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗███╗ ██╗████████╗███████╗██████╗
██╔════╝██╔══██╗██╔══██╗██╔════╝ ██╔════╝██╔═══██╗██║ ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗
█████╗ ██████╔╝███████║██║ ███╗██║ ██║ ██║██║ ██║██╔██╗ ██║ ██║ █████╗ ██████╔╝
██╔══╝ ██╔══██╗██╔══██║██║ ██║██║ ██║ ██║██║ ██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗
██║ ██║ ██║██║ ██║╚██████╔╝╚██████╗╚██████╔╝╚██████╔╝██║ ╚████║ ██║ ███████╗██║ ██║
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
"
library(optparse)
options(bitmapType='cairo')
if (!exists('opt'))
{
option_list = list(
make_option(c("-b", "--bam"), type = "character", help = "Path to .bam or .cram file"),
make_option(c("-c", "--cov"), type = "character", help = "Path to existing coverage rds or bedgraph"),
make_option(c("-m", "--midpoint"), type = "character", default = "TRUE", help = "If TRUE only count midpoint if FALSE then count bin footprint of every fragment interval"),
make_option(c("-w", "--window"), type = "integer", default = 1000, help = "Window / bin size"),
make_option(c("-d", "--gcmapdir"), type = "character", help = "Mappability / GC content dir"),
make_option(c("-q", "--minmapq"), type = "integer", default = 20, help = "Minimal map quality"),
make_option(c("-r", "--reference"), type = "character", default = NULL, help = "Reference FASTA path (required for CRAM)"),
make_option(c("-p", "--paired"), type = "logical", default = TRUE, help = "Is paired"),
make_option(c("-e", "--exome"), type = "logical", default = FALSE, help = "Use exons as bins instead of fixed window"),
make_option(c("-u", "--use.skel"), type = "logical", default = FALSE, help = "Use user defined regions instead of default exome skeleton"),
make_option(c("-s", "--skeleton"), type = "character", help = "Path to skeleton file"),
make_option(c("-o", "--outdir"), type = "character", default = './', help = "Directory to dump output into"),
make_option("--require_flags", type = "character", default = '2', help = "-f flag for samtools used in fragCounter"),
make_option("--exclude_flags", type = "character", default = '3868', help = "-f flag for samtools used in fragCounter"),
make_option("--min.tlen", type = "numeric", default = 0, help = "Minimmum template length provided to bamUtils::bam.cov.tile"),
make_option("--max.tlen", type = "numeric", default = 1e4, help = "Maximum template length provided to bamUtils::bam.cov.tile"),
make_option("--correct_for_bias", type = "logical", default = TRUE, help = "Correct for GC and mappability bias."),
make_option(c("-l", "--libdir"), type = "character", default = paste(Sys.getenv('GIT_HOME'), 'isva', sep = '/'), help = "Directory containing this R file")
)
parseobj = OptionParser(option_list=option_list)
opt = parse_args(parseobj)
if (is.null(opt$libdir) | (is.null(opt$bam) & is.null(opt$cov)))
stop(print_help(parseobj))
## keep record of run
writeLines(paste(paste('--', names(opt), ' ', sapply(opt, function(x) paste(x, collapse = ',')), sep = '', collapse = ' '), sep = ''), paste(opt$outdir, 'cmd.args', sep = '/'))
saveRDS(opt, paste(opt$outdir, 'cmd.args.rds', sep = '/'))
}
#############################
suppressWarnings(suppressPackageStartupMessages(library(fragCounter)))
suppressWarnings(suppressPackageStartupMessages(library(skidb)))
suppressWarnings(suppressPackageStartupMessages(library(data.table)))
message(dr.str)
# Samtools flags
#
# Samtools flags for fragCounter bam query
# -F exclude ANY bits set
# 3868 sets bits
# 4 - Read unmapped
# 8 - Mate unmapped
# 16 - Read reverse strand
# 256 - Not primary alignment (secondary alignment)
# 512 - Read fails platform/vendor quality checks
# 1024 - Read is PCR or optical duplicate
# 2048 - Supplementary alignment (chimeric, non-contiguous alignment)
# -f requires read have ALL bits set
# 2 - Read mapped in proper pair
is_null = is.null(opt$require_flags)
is_character = is.character(opt$require_flags)
is_numeric = is.numeric(opt$require_flags)
is_len_one = NROW(opt$require_flags) == 1
is_na = is_len_one && (is.na(opt$require_flags) || (is_character && opt$require_flags %in% "NA"))
is_valid = is_len_one && (is_character || is_numeric)
require_flags = "-f 2"
if (!is_null && !is_na && is_valid) {
require_flags = paste("-f", opt$require_flags)
}
is_null = is.null(opt$exclude_flags)
is_character = is.character(opt$exclude_flags)
is_numeric = is.numeric(opt$exclude_flags)
is_len_one = NROW(opt$exclude_flags) == 1
is_na = is_len_one && (is.na(opt$exclude_flags) || (is_character && opt$exclude_flags %in% "NA"))
is_valid = is_len_one && (is_character || is_numeric)
exclude_flags = "-F 3868"
if (!is_null && !is_na && is_valid) {
exclude_flags = paste("-F", opt$exclude_flags)
}
st.flag = paste(require_flags, exclude_flags)
out = fragCounter(bam = opt$bam, cov = opt$cov, window = opt$window, reference = opt$reference, gc.rds.dir = opt$gcmapdir, map.rds.dir = opt$gcmapdir, minmapq = opt$minmapq, paired = opt$paired, outdir = opt$outdir, exome = opt$exome, use.skel = opt$use.skel, skeleton = opt$skeleton, st.flag = st.flag, min.tlen = opt$min.tlen, max.tlen = opt$max.tlen, correct_for_bias = opt$correct_for_bias)
saveRDS(out, paste(opt$outdir, 'cov.rds', sep = '/'))