-
Notifications
You must be signed in to change notification settings - Fork 6
/
library.jl
189 lines (158 loc) · 6.44 KB
/
library.jl
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Given value(s) `x` apply the sigmoidal function with maximum
value `l`, a steepness `k`, and an inflection point `p`.
"""
sigmoid(x, l, k, p) = clamp(l./(1 + e.^(-k.*(x - p))), min(0, l), max(0, l))
"""
Given value(s) `x` apply a simple linear function with maximum value `l`
"""
linear(x, l) = clamp(l.*x, min(0, l), max(0, l))
abstract KDPhenotypeRelationship
type Linear <: KDPhenotypeRelationship end
type Sigmoidal <: KDPhenotypeRelationship
"Slopes of the sigmoid are pulled from this distribution"
slope_dist::Distribution
"Inflection points are pulled from this distribution"
inflection_dist::Distribution
function Sigmoidal()
# slopes are pretty steep so that there is clear differentiation
# between linear and sigmoidal
slopes = TruncatedNormal(30, 5, 10, Inf)
# inflections are centered at high KD
inflections = TruncatedNormal(0.8, 0.2, 0, 1)
new(slopes, inflections)
end
end
response(::Linear) = linear
function response(sig::Sigmoidal)
slope, inflection = rand(sig.slope_dist), rand(sig.inflection_dist)
(x, l) -> sigmoid(x, l, slope, inflection)
end
abstract Cas9Behavior
type CRISPRi <: Cas9Behavior end
type CRISPRKO <: Cas9Behavior
knockout_dist::Categorical
CRISPRKO(dist::Categorical) = new(dist)
end
CRISPRKO() = CRISPRKO(Categorical([1/9, 4/9, 4/9]))
"""
Wrapper containing all library construction parameters
"""
type Library
"Distribution of guide knockdown efficiencies"
knockdown_dist::Dict{Int64, Tuple{Symbol, Sampleable}}
knockdown_probs::Categorical
"""
Maximum phenotype categories mapped to their probability of being
selected and the distribution to draw from if they are selected
"""
max_phenotype_dists::Dict{Int64, Tuple{Symbol, Sampleable}}
phenotype_probs::Categorical
"""
Knockdown-phenotype relationships mapped to their probability of
being selected and their respective KDPhenotypeRelationship
"""
kd_phenotype_relationships::Dict{Int64, Tuple{Symbol, KDPhenotypeRelationship}}
relationship_probs::Categorical
"""
Whether this library is CRISPRi or CRISPR cutting.
"""
cas9_behavior::Cas9Behavior
function Library(knockdown_dist::Dict{Symbol, Tuple{Float64, Sampleable}},
max_phenotype_dists::Dict{Symbol, Tuple{Float64, Sampleable}},
kd_phenotype_relationships::Dict{Symbol, Tuple{Float64, KDPhenotypeRelationship}},
cas9_behavior::Cas9Behavior)
kd = unroll(knockdown_dist)
max_p = unroll(max_phenotype_dists)
rela = unroll(kd_phenotype_relationships)
new(kd[1], kd[2], max_p[1], max_p[2], rela[1], rela[2], cas9_behavior)
end
end
function Library(cas9_behavior::Cas9Behavior)
max_phenotype_dists = Dict{Symbol, Tuple{Float64, Sampleable}}(
:inactive => (0.75, Delta(0.0)),
:negcontrol => (0.05, Delta(0.0)),
:increasing => (0.1, TruncatedNormal(0.55, 0.2, 0.1, 1)),
:decreasing => (0.1, TruncatedNormal(-0.55, 0.2, -1, -0.1))
)
Library(max_phenotype_dists, cas9_behavior)
end
function Library(max_phenotype_dists::Dict{Symbol, Tuple{Float64, Sampleable}},
cas9_behavior::CRISPRi)
# Assuming a high quality library has mostly good guides with some bad ones
knockdown_dist = Dict{Symbol, Tuple{Float64, Sampleable}}(
:high => (0.9, TruncatedNormal(0.90, 0.1, 0, 1)),
:low => (0.1, TruncatedNormal(0.05, 0.07, 0, 1))
)
Library(max_phenotype_dists, knockdown_dist, cas9_behavior)
end
function Library(max_phenotype_dists::Dict{Symbol, Tuple{Float64, Sampleable}},
cas9_behavior::CRISPRKO)
# For CRISPR KO assume that if guide is "high quality" than it will a
# maximum knockdown of 100%
knockdown_dist = Dict{Symbol, Tuple{Float64, Sampleable}}(
:high => (0.9, Delta(1.0)),
:low => (0.1, TruncatedNormal(0.05, 0.07, 0, 1))
)
Library(max_phenotype_dists, knockdown_dist, cas9_behavior)
end
function Library(max_phenotype_dists::Dict{Symbol, Tuple{Float64, Sampleable}},
knockdown_dist::Dict{Symbol, Tuple{Float64, Sampleable}},
cas9_behavior::Cas9Behavior)
kd_phenotype_relationships = Dict{Symbol, Tuple{Float64, KDPhenotypeRelationship}}(
:linear => (0.75, Linear()),
:sigmoidal => (0.25, Sigmoidal())
)
Library(knockdown_dist, max_phenotype_dists, kd_phenotype_relationships, cas9_behavior)
end
function unroll{T}(data::Dict{Symbol, Tuple{Float64, T}})
probs = Float64[]
results = Dict{Int64, Tuple{Symbol, T}}()
count = 0
for (key, value) in data
push!(probs, value[1])
results[count+=1] = (key, value[2])
end
results, Categorical(probs)
end
function rand_gene(lib::Library)
class, dist = lib.max_phenotype_dists[rand(lib.phenotype_probs)]
max_phenotype = rand(dist)
behavior, relationship = lib.kd_phenotype_relationships[rand(lib.relationship_probs)]
class, max_phenotype, behavior, relationship
end
"""
Constructs the guide library for `N` genes with `coverage` number of guides per
gene. Returns a tuple of guides and their relative frequencies (assigned randomly).
"""
function construct_library(setup::ScreenSetup, lib::Library)
barcodes = Barcode[]
N = setup.num_genes
coverage = setup.coverage
for gene in 1:N
gene_class, max_phenotype, gene_behavior, relationship = rand_gene(lib)
# get this gene's KD-phenotype relationship
kd_response = response(relationship)
for i in 1:coverage
barcode_quality, barcode_knockdown_dist = lib.knockdown_dist[rand(lib.knockdown_probs)]
barcode_knockdown = rand(barcode_knockdown_dist)
# phenotype of barcode given its knockdown efficiency
barcode_phenotype = kd_response(barcode_knockdown, max_phenotype)
push!(barcodes, Barcode(gene, barcode_knockdown, barcode_phenotype,
gene_behavior, gene_class))
end
end
# the expected z-score for a 90% confidence interval is 2x1.645=3.29
# construct a normal distribution with a σ = 1/3.29 so that there is
# a 10-fold difference between the 95th/5th percentiles
vals = 10.^rand(Normal(0, 1/3.29), N*coverage)
guide_freqs = vals/sum(vals)
barcodes, Categorical(guide_freqs)
end
"""
Constructs a delta function at a given δ value
"""
type Delta <: Distributions.Sampleable{Univariate, Discrete}
δ::Float64
end
Base.rand(d::Delta) = d.δ