-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergePSI.R
72 lines (62 loc) · 2.07 KB
/
mergePSI.R
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
#!/usr/bin/env Rscript
# -*- coding: utf-8 -*-
# @Date : 2019-10-11 16:24:10
# @Author : lizd ([email protected])
# @Link : ---
# @Version : $Id$
if (!require("optparse", quietly = TRUE)) {
install.packages("optparse")
if (!require("optparse", quietly = TRUE)) {
stop("optparse not installed, please install it first.")
}
}
suppressPackageStartupMessages(library("optparse"))
suppressPackageStartupMessages(library("data.table"))
opt_list <- list(
make_option(
c("-i", "--indir"),
type = "character",
help = "RSEM output directory, only contain the results form the same specie.",
metavar = "character"
),
make_option(
c("-o", "--out_prefix"),
type = "character",
help = "Output prefix of merged file.",
metavar = "character"
),
make_option(
c("-t", "--type"),
type = "character",
help = "Output prefix of merged file.",
metavar = "character"
)
)
opts <- parse_args(OptionParser(
option_list = opt_list,
usage = "usage: %prog [options]",
add_help_option = TRUE,
prog = "",
description = "Merge Exonic parts PSI output files from spesific directory."))
if (is.null(opts$indir)) {
stop("-i --indir not set")
} else if (is.null(opts$out_prefix)) {
stop("-o --out_prefix not set")
} else if (is.null(opts$type)) {
stop("-o --type not set")
}
MergePSI <- function(path,
type = "PSI"){
filenames <- sort(list.files(path=path, pattern="exonic_parts.psi", full.names=TRUE))
datalist <- lapply(filenames, function(x){
tmp <- fread(x, header=TRUE, sep = "\t")
tmp <- tmp[, c("exon_ID", type), with=F]
samplename <- sub("[\\._]*exonic_parts.psi$", "", basename(x))
setnames(tmp, type, samplename)
setkey(tmp, exon_ID)
return(tmp)})
all <- Reduce(function(x,y) {merge(x, y, all=T, by="exon_ID")}, datalist)
outname <- paste0(opts$out_prefix, "_ExonicPart_", type, ".tsv")
fwrite(all, file = outname, sep = "\t", na = "NA", quote = FALSE)
}
MergePSI(opts$indir, opts$type)