forked from esherm/intSiteRetriever
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintSiteRetriever.R
More file actions
159 lines (140 loc) · 5.99 KB
/
intSiteRetriever.R
File metadata and controls
159 lines (140 loc) · 5.99 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
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
# This source code file is a component of the larger INSPIIRED genomic analysis software package.
# Copyright (C) 2016 Frederic Bushman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
.get_unique_sites <- function(sample_ref, conn) {
sample_ref_in_db <- .get_sample_ref_in_db(sample_ref, conn)
sites <- dplyr::tbl(conn, "sites")
dplyr::inner_join(sites, sample_ref_in_db)
}
#' for a given samples get sites positions.
#' @param sample_ref dataframe with 2 cols: sampleName, refGenome
#' @param conn connection to database
#' @return sites dataframe with cols: siteID, chr, strand, position, sampleName, refGenome
#' @export
getUniqueSites <- function(sample_ref, conn) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
sites <- .get_unique_sites(sample_ref, conn)
dplyr::collect(
dplyr::select(
sites, siteID, chr, strand, position, sampleName, refGenome),
n = Inf)
}
.get_multihitpositions <- function(sample_ref, conn) {
sample_ref_in_db <- .get_sample_ref_in_db(sample_ref, conn)
multihitpositions <- dplyr::tbl(conn, "multihitpositions")
dplyr::inner_join(multihitpositions, sample_ref_in_db, by = "sampleID")
}
#' lengths distributions for multihits
#' @inheritParams getUniqueSites
#' @return df with cols: sampleName, refGenome, multihitID, length
#' @export
getMultihitLengths <- function(sample_ref, conn) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
samples_multihitpositions <- .get_multihitpositions(sample_ref, conn)
multihit_lengths <- dplyr::tbl(conn, "multihitlengths")
dplyr::collect(dplyr::distinct(dplyr::select(dplyr::inner_join(
samples_multihitpositions, multihit_lengths, by = "multihitID"),
sampleName, refGenome, multihitID, length)),
n = Inf)
}
.get_breakpoints <- function(sample_ref, conn) {
sample_ref_sites <- .get_unique_sites(sample_ref, conn)
breakpoints <- dplyr::tbl(conn, "pcrbreakpoints")
dplyr::inner_join(sample_ref_sites, breakpoints)
}
#' breakpoints
#' @inheritParams getUniqueSites
#' @export
getUniquePCRbreaks <- function(sample_ref, conn) {
breakpoints <- .get_breakpoints(sample_ref, conn)
dplyr::collect(dplyr::select(breakpoints,
breakpoint, count, position, siteID, chr,
strand, sampleName, refGenome),
n = Inf
)
# column named kept as in DB ...sites.position AS integration...
}
.check_has_sample_ref_cols <- function(sample_ref) {
return(all(c("sampleName", "refGenome") %in% names(sample_ref)))
}
.get_sample_table <- function(conn) {
samples_in_db <- dplyr::tbl(conn, "samples")
dplyr::select(samples_in_db, sampleID, sampleName, refGenome)
}
.get_sample_ref_in_db <- function(sample_ref, conn) {
samples_in_db <- dplyr::tbl(conn, "samples")
samples_in_db <- dplyr::select(
samples_in_db, sampleID, sampleName, refGenome, gender)
dplyr::inner_join(
samples_in_db, sample_ref,
by = c('sampleName', 'refGenome'), copy = TRUE)
}
#' do we have samples in database
#' @inheritParams getUniqueSites
#' @return vector of TRUE/FALSE for each row in sample_ref df
#' @export
setNameExists <- function(sample_ref, conn) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
sample_ref_in_db <- dplyr::collect(.get_sample_table(conn), n = Inf)
if (nrow(sample_ref_in_db) == 0) { # nothing is in db
return(rep(FALSE, nrow(sample_ref)))
}
ids <- paste0(sample_ref$sampleName, sample_ref$refGenome)
ids_DB <- paste0(sample_ref_in_db$sampleName, sample_ref_in_db$refGenome)
ids %in% ids_DB
}
#' counts
#' @inheritParams getUniqueSites
#' @export
getUniqueSiteReadCounts <- function(sample_ref, conn) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
sample_ref_sites_breakpoints <- .get_breakpoints(sample_ref, conn)
sample_ref_sites_breakpoints_grouped <- dplyr::group_by(
sample_ref_sites_breakpoints, sampleName, refGenome)
dplyr::collect(dplyr::summarize(
sample_ref_sites_breakpoints_grouped, readCount = sum(count)),
n = Inf)
}
#' unique counts for integration sites for a given sample(with fixed genome)
#' @inheritParams getUniqueSites
#' @export
getUniqueSiteCounts <- function(sample_ref, conn) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
sample_ref_sites <- .get_unique_sites(sample_ref, conn)
sample_ref_sites_grouped <- dplyr::group_by(
sample_ref_sites, sampleName, refGenome)
dplyr::collect(dplyr::summarize(
sample_ref_sites_grouped, uniqueSites = n()), n = Inf)
}
#' creates match random controls.
#' @inheritParams getUniqueSites
#' @param numberOfMRCs how many controls for each site
#' @return df with cols: siteID, position, strand, chr, sampleName, refGenome
#' @export
getMRCs <- function(sample_ref, conn, numberOfMRCs=3) {
stopifnot(.check_has_sample_ref_cols(sample_ref))
sites <- .get_unique_sites(sample_ref, conn)
sites.metadata <- dplyr::collect(dplyr::select(
sites, siteID, gender, sampleName, refGenome), n = Inf)
sites_meta <- data.frame(
"siteID" = sites.metadata$siteID,
"gender" = tolower(sites.metadata$gender))
stopifnot(length(unique(sites.metadata$refGenome)) == 1)
ref_genome <- sites.metadata$refGenome[1] # all the same
mrcs <- get_N_MRCs(
sites_meta, get_reference_genome(ref_genome), numberOfMRCs)
merge(mrcs, sites.metadata[c("siteID", "sampleName", "refGenome")])
}