-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscreteInsertor.R
644 lines (609 loc) · 13.4 KB
/
DiscreteInsertor.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
##
## Copyright 2009 Botond Sipos
## See the package description for licensing information.
##
##########################################################################/**
#
# @RdocClass DiscreteInsertor
#
# @title "The DiscreteInsertor class"
#
# \description{
# This class implements a process which performs insertions with
# lengths sampled from a user-specified discrete distribution.
# See \code{GeneralInsertor} for how the insertion process works.
#
# @classhierarchy
# }
#
# @synopsis
#
# \arguments{
# \item{name}{The name of the object.}
# \item{rate}{The general rate.}
# \item{sizes}{The insertion sizes to propose.}
# \item{probs}{A vector with the probabilites of the insertion sizes.}
# \item{...}{Additional arguments.}
# }
#
# \section{Fields and Methods}{
# @allmethods
# }
#
# \examples{
# # create a DiscreteInsertor process
# i<-DiscreteInsertor(
# name="Mii",
# rate=0.25,
# sizes=c(1,2),
# probs=c(1/2,1/2)
# )
# # set template sequence
# i$templateSeq<-NucleotideSequence(string="C")
# # get object summary
# summary(i)
# # set/get insertion sizes
# i$sizes<-1:3
# i$sizes
# # set/get length probabilities
# i$probs<-c(3,2,1)/6
# i$probs
# # plot length distribution
# plot(i)
#
# # The following code illustrates how to use
# # a DiscreteInsertor process in a simulation
#
# # create a sequence object and attach process i to it
# s<-NucleotideSequence(string="AAAAAAAAAAGGGGAAAAAAAAAA",processes=list(list(i)))
# # set the insertion tolerance to zero in range 11:15
# # creating a region rejecting all insertions
# setInsertionTolerance(s,i,0,11:15)
# # get insertion tolerances
# getInsertionTolerance(s,i)
# # create a simulation object
# sim<-PhyloSim(root.seq=s,phylo=rcoal(2))
# # simulate
# Simulate(sim)
# # print resulting alignment
# sim$alignment
# }
#
# @author
#
# \seealso{
# GeneralInsertor ContinuousInsertor GeneralInDel
# }
#
#*/###########################################################################
setConstructorS3(
"DiscreteInsertor",
function(
name="Anonymous",
rate=NA,
sizes=NA,
probs=NA,
...
) {
this<-GeneralInsertor(
name=NA,
rate=rate,
propose.by=NA,
accept.by=NA,
generate.by=NA,
...
);
this<-extend(
this,
"DiscreteInsertor",
.sizes=NA,
.probs=NA
);
# Using virtual field to clear Id cache:
this$name<-name;
STATIC<-TRUE;
if(!missing(sizes)) {
this$sizes<-sizes;
STATIC<-FALSE;
}
if(!missing(probs)) {
this$probs<-probs;
STATIC<-FALSE;
}
this$proposeBy<-function(process=NA,...){
if( !is.numeric(process$.sizes) | !is.numeric(process$.probs) ){
throw("Cannot propose insert length because the length distribution is not defined properly!\n");
}
if(length(process$.sizes) == 1){
return(process$.sizes[[1]]);
} else {
return(sample(x=process$.sizes,size=1,replace=FALSE,prob=process$.probs));
}
}
return(this);
},
enforceRCC=TRUE
);
##
## Method: checkConsistency
##
###########################################################################/**
#
# @RdocMethod checkConsistency
#
# @title "Check object consistency"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{An object.}
# \item{...}{Not used.}
# }
#
#
# \value{
# Returns an invisible TRUE if no inconsistencies found in the object, throws
# an error otherwise.
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"checkConsistency",
class="DiscreteInsertor",
function(
this,
...
){
wp<-this$writeProtected;
if (wp) {
this$writeProtected<-FALSE;
}
may.fail<-function(this) {
if (is.numeric(this$sizes)) {
this$sizes<-this$sizes;
}
else if (!is.na(this$sizes)){
throw("Insertion size vector is invalid!\n");
}
if (is.numeric(this$probs)) {
this$probs<-this$probs;
}
else if (!is.na(this$probs)){
throw("Insertion size probability vector is invalid!\n");
}
}
tryCatch(may.fail(this),finally=this$writeProtected<-wp);
NextMethod();
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getSizes
##
###########################################################################/**
#
# @RdocMethod getSizes
#
# @title "Get the sizes of the proposed insertions"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A DiscreteInsertor object.}
# \item{...}{Not used.}
# }
#
# \value{
# A vector of integers.
# }
#
# \examples{
# # create a DiscreteInsertor object
# i<-DiscreteInsertor(rate=1)
# # set insertion sizes
# setSizes(i,c(1,2,3))
# # get insertion sizes
# getSizes(i)
# # set/get sizes via virtual field
# i$sizes<-1:10
# i$sizes
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getSizes",
class="DiscreteInsertor",
function(
this,
...
){
this$.sizes;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setSizes
##
###########################################################################/**
#
# @RdocMethod setSizes
#
# @title "Set the sizes of the proposed insertions"
#
# \description{
# @get "title".
#
# The provided numeric vector is rounded.
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A DiscreteInsertor object.}
# \item{value}{A numeric vector.}
# \item{...}{Not used.}
# }
#
# \value{
# A vector of integers (invisible).
# }
#
# \examples{
# # create a DiscreteInsertor object
# i<-DiscreteInsertor(rate=1)
# # set insertion sizes
# setSizes(i,c(1,2,3))
# # get insertion sizes
# getSizes(i)
# # set/get sizes via virtual field
# i$sizes<-1:10
# i$sizes
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setSizes",
class="DiscreteInsertor",
function(
this,
value,
...
){
.checkWriteProtection(this);
if (missing(value)) {
throw("No new value provided!\n");
} else if (!is.numeric(value)) {
throw("The new value must be numeric vector!\n");
} else {
if(length(value) == 0 ) {
warning("Deletion size vector has zero length!\n");
}
this$.sizes<-round(value);
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: getProbs
##
###########################################################################/**
#
# @RdocMethod getProbs
#
# @title "Get the insertion length probabilities"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A DiscreteInsertor object.}
# \item{...}{Not used.}
# }
#
# \value{
# A numeric vector with the insertion length probabilities.
# }
#
# \examples{
# # create a DiscreteInsertor object
# i<-DiscreteInsertor(rate=1, sizes=1:3)
# # set/get length probabilities
# setProbs(i,c(1/3,1/3,1/3)) # equal probabilites
# getProbs(i)
# # set/get length probabilities via virtual field
# x<-c(2,2,1)
# # normalize x
# x<-x/sum(x)
# i$probs<-x
# i$probs
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"getProbs",
class="DiscreteInsertor",
function(
this,
...
){
this$.probs;
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: setProbs
##
###########################################################################/**
#
# @RdocMethod setProbs
#
# @title "Set the insertion length probabilities"
#
# \description{
# @get "title".
#
# The \code{sizes} virtual field must be set before setting the length probabilities.
# The length of the provided numeric vector must match with the length of the vector
# stored in the \code{sizes} virtual field. The vector is rescaled if the values do not
# sum to one and a warning is issued.
# }
#
# @synopsis
#
# \arguments{
# \item{this}{A DiscreteInsertor object.}
# \item{value}{A numeric vector containg the length probabilities.}
# \item{...}{Not used.}
# }
#
# \value{
# The vector of probabilities.
# }
#
# \examples{
# # create a DiscreteInsertor object
# i<-DiscreteInsertor(rate=1, sizes=1:3)
# # set/get length probabilities
# setProbs(i,c(1/3,1/3,1/3)) # equal probabilites
# getProbs(i)
# # set/get length probabilities via virtual field
# x<-c(2,2,1)
# # normalize x
# x<-x/sum(x)
# i$probs<-x
# i$probs
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"setProbs",
class="DiscreteInsertor",
function(
this,
value,
...
){
.checkWriteProtection(this);
if (missing(value)) {
throw("No new value provided!\n");
}
else if(!is.numeric(this$.sizes)) {
throw("Cannot set probabilities because insert sizes vector is not defined!\n");
}
else if (!is.numeric(value)) {
throw("The new value must be a numeric vector!\n");
}
else if(length(value) != length(this$.sizes)) {
throw("The length of the probabilities vector must be the same as the length of the insertion sizes vector");
}
else if( length(value[value < 0 ]) != 0 ) {
throw("The elements of the probability vector must not be negative!\n");
}
else {
if(!isTRUE(all.equal(sum(value),1.0))){
value<-(value/sum(value));
warning("The provided values were rescaled in order to sum to one!\n");
}
this$.probs<-value;
}
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: plot
##
###########################################################################/**
#
# @RdocMethod plot
#
# @title "Plot the insertion length distribution"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{x}{A DiscreteInsertor object.}
# \item{...}{Not used.}
# }
#
# \value{
# The DiscreteInsertor object (invisible).
# }
#
# \examples{
# i<-DiscreteInsertor(
# name="MyDiscIns",
# sizes=1:6,
# probs=c(0.25000000, 0.16666667, 0.08333333, 0.08333333, 0.16666667, 0.25000000)
# )
# # plot the insertion length distribution
# plot(i)
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"plot",
class="DiscreteInsertor",
function(
x,
...
){
this<-x;
if( !is.numeric(this$sizes) | !is.numeric(this$probs) ){
warning("Insertion length distribution is not defined properly! Nothing to plot here!\n");
return();
}
plot.default(
x=this$sizes,
y=this$probs,
col=c("red"),
lwd=2,
type="h",
main=paste("Insertion size distribution for:",this$id),
xlab="Size",
ylab="Probability",
ylim=c(0,1),
xaxt="n"
);
axis(side=1, at=this$sizes, labels=this$sizes);
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);
##
## Method: summary
##
###########################################################################/**
#
# @RdocMethod summary
#
# @title "Summarize the properties of an object"
#
# \description{
# @get "title".
# }
#
# @synopsis
#
# \arguments{
# \item{object}{An object}
# \item{...}{Not used.}
# }
#
# \value{
# Returns a PSRootSummary object.
# }
#
# \examples{
#
# # create an object
# a<-DiscreteInsertor(rate=1,sizes=c(1,2),probs=c(1/2,1/2))
# # get a summary
# summary(a)
# }
#
# @author
#
# \seealso{
# @seeclass
# }
#
#*/###########################################################################
setMethodS3(
"summary",
class="DiscreteInsertor",
function(
object,
...
){
this<-object;
.addSummaryNameId(this);
expected.length<-NA;
sd<-NA;
if( is.numeric(this$sizes) & is.numeric(this$probs)) {
expected.length<-weighted.mean(this$sizes, this$probs);
sd<-sqrt(sum( (this$sizes - expected.length)^2 * this$probs ));
}
this$.summary$"Expected insertion length"<-expected.length;
this$.summary$"Standard deviation of insertion length"<-format(sd);
NextMethod();
},
private=FALSE,
protected=FALSE,
overwrite=FALSE,
conflict="warning",
validators=getOption("R.methodsS3:validators:setMethodS3")
);