PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/GeneralInDel.R

http://github.com/sbotond/phylosim
R | 2702 lines | 852 code | 158 blank | 1692 comment | 120 complexity | a23736811e89e9bdda791d2d2ec6c5dc MD5 | raw file
Possible License(s): GPL-3.0
  1. ##
  2. ## Copyright 2009 Botond Sipos
  3. ## See the package description for licensing information.
  4. ##
  5. ##########################################################################/**
  6. #
  7. # @RdocClass GeneralInDel
  8. #
  9. # @title "The GeneralInDel class"
  10. #
  11. # \description{
  12. #
  13. # This is a class implementing the methods which are needed by both the
  14. # GeneralInsertor and GeneralDeletor process.
  15. #
  16. # @classhierarchy
  17. # }
  18. #
  19. # @synopsis
  20. #
  21. # \arguments{
  22. # \item{name}{The name of the object.}
  23. # \item{rate}{The general rate of the object.}
  24. # \item{propose.by}{A function used to propose events.}
  25. # \item{accept.by}{A function used to accept/reject events.}
  26. # \item{...}{Additional arguments.}
  27. # }
  28. #
  29. # \section{Fields and Methods}{
  30. # @allmethods
  31. # }
  32. #
  33. # \examples{
  34. # # create a GeneralInDel object
  35. # # rejecting half of the events
  36. # # and proposing sizes in the range 1:10
  37. # o<-GeneralInDel(
  38. # rate=1,
  39. # propose.by=function(process){sample(1:10,1)},
  40. # accept.by=function(){sample(c(TRUE,FALSE),1)}
  41. # );
  42. # # check if inherits from GeneralInDel
  43. # is.GeneralInDel(o)
  44. # # check if it has undefined rates
  45. # hasUndefinedRate(o)
  46. # # get object summary
  47. # summary(o)
  48. # # set/get proposeBy function via virtual field
  49. # o$proposeBy<-function(process){return(3)} # fixed event length
  50. # o$proposeBy
  51. # # set/get acceptBy function via virtual field
  52. # o$acceptBy<-function(){return(TRUE)} # accept all events
  53. # o$acceptBy
  54. # # get/set general rate
  55. # o$rate
  56. # o$rate<-2 # double the rate
  57. # # propose event length
  58. # proposeLength(o)
  59. # }
  60. #
  61. # @author
  62. #
  63. # \seealso{
  64. # Process GeneralInsertor GeneralDeletor GeneralSubstitution
  65. # }
  66. #
  67. #*/###########################################################################
  68. setConstructorS3(
  69. "GeneralInDel",
  70. function(
  71. name="Anonymous",
  72. rate=NA,
  73. propose.by=NA,
  74. accept.by=NA,
  75. ...
  76. ) {
  77. any.alphabet<-AnyAlphabet();
  78. this<-Process(
  79. alphabet=any.alphabet
  80. );
  81. this<-extend(
  82. this,
  83. "GeneralInDel",
  84. .rate=rate,
  85. .propose.by=NA,
  86. .accept.by=NA,
  87. .is.general.indel=TRUE
  88. );
  89. # Using virtual field to clear Id cache:
  90. this$name<-name;
  91. # setting propose.by
  92. if(!missing(propose.by) && is.function(propose.by)){
  93. this$proposeBy<-propose.by;
  94. }
  95. # setting accept.by
  96. if(!missing(accept.by) && is.function(accept.by)){
  97. this$acceptBy<-accept.by;
  98. }
  99. return(this);
  100. },
  101. enforceRCC=TRUE
  102. );
  103. ##
  104. ## Method: checkConsistency
  105. ##
  106. ###########################################################################/**
  107. #
  108. # @RdocMethod checkConsistency
  109. #
  110. # @title "Check object consistency"
  111. #
  112. # \description{
  113. # @get "title".
  114. # }
  115. #
  116. # @synopsis
  117. #
  118. # \arguments{
  119. # \item{this}{An object.}
  120. # \item{...}{Not used.}
  121. # }
  122. #
  123. #
  124. # \value{
  125. # Returns an invisible TRUE if no inconsistencies found in the object, throws
  126. # an error otherwise.
  127. # }
  128. #
  129. # @author
  130. #
  131. # \seealso{
  132. # @seeclass
  133. # }
  134. #
  135. #*/###########################################################################
  136. setMethodS3(
  137. "checkConsistency",
  138. class="GeneralInDel",
  139. function(
  140. this,
  141. ...
  142. ){
  143. wp<-this$writeProtected;
  144. if (wp) {
  145. this$writeProtected<-FALSE;
  146. }
  147. may.fail<-function(this) {
  148. if (!is.na(this$rate)) {
  149. this$rate<-this$rate;
  150. }
  151. if(!is.function(this$proposeBy)){
  152. if(!is.na(this$proposeBy)){
  153. throw("proposeBy is invalid!\n");
  154. }
  155. }
  156. if(!is.function(this$acceptBy)){
  157. if(!is.na(this$acceptBy)){
  158. throw("acceptBy is invalid!\n");
  159. }
  160. }
  161. }
  162. tryCatch(may.fail(this),finally=this$writeProtected<-wp);
  163. NextMethod();
  164. },
  165. private=FALSE,
  166. protected=FALSE,
  167. overwrite=FALSE,
  168. conflict="warning",
  169. validators=getOption("R.methodsS3:validators:setMethodS3")
  170. );
  171. ##
  172. ## Method: getRate
  173. ##
  174. ###########################################################################/**
  175. #
  176. # @RdocMethod getRate
  177. #
  178. # @title "Get the general rate"
  179. #
  180. # \description{
  181. # @get "title".
  182. # }
  183. #
  184. # @synopsis
  185. #
  186. # \arguments{
  187. # \item{this}{A GeneralInDel object.}
  188. # \item{...}{Not used.}
  189. # }
  190. #
  191. # \value{
  192. # A numeric vector of length one.
  193. # }
  194. #
  195. # \examples{
  196. # # create a GeneralInDel object
  197. # o<-GeneralInDel(rate=0.5)
  198. # # get/set general rate
  199. # getRate(o)
  200. # setRate(o, 1.5)
  201. # # get/set rate via virtual field
  202. # o$rate
  203. # o$rate<-0.3
  204. # o$rate
  205. # }
  206. #
  207. # @author
  208. #
  209. # \seealso{
  210. # @seeclass
  211. # }
  212. #
  213. #*/###########################################################################
  214. setMethodS3(
  215. "getRate",
  216. class="GeneralInDel",
  217. function(
  218. this,
  219. ...
  220. ){
  221. this$.rate;
  222. },
  223. private=FALSE,
  224. protected=FALSE,
  225. overwrite=FALSE,
  226. conflict="warning",
  227. validators=getOption("R.methodsS3:validators:setMethodS3")
  228. );
  229. ##
  230. ## Method: hasUndefinedRate
  231. ##
  232. ###########################################################################/**
  233. #
  234. # @RdocMethod hasUndefinedRate
  235. #
  236. # @title "Check whether the general rate of a GeneralInDel object is undefined"
  237. #
  238. # \description{
  239. # @get "title".
  240. # }
  241. #
  242. # @synopsis
  243. #
  244. # \arguments{
  245. # \item{this}{A GeneralInDel object.}
  246. # \item{...}{Not used.}
  247. # }
  248. #
  249. # \value{
  250. # TRUE or FALSE.
  251. # }
  252. #
  253. # \examples{
  254. # # create a GeneralInDel object
  255. # o<-GeneralInDel()
  256. # # check if the general rate is undefined
  257. # hasUndefinedRate(o)
  258. # # set general rate
  259. # o$rate<-1
  260. # # check rate again
  261. # hasUndefinedRate(o)
  262. # }
  263. #
  264. # @author
  265. #
  266. # \seealso{
  267. # @seeclass
  268. # }
  269. #
  270. #*/###########################################################################
  271. setMethodS3(
  272. "hasUndefinedRate",
  273. class="GeneralInDel",
  274. function(
  275. this,
  276. ...
  277. ){
  278. return(is.na(this$.rate));
  279. },
  280. private=FALSE,
  281. protected=FALSE,
  282. overwrite=FALSE,
  283. conflict="warning",
  284. validators=getOption("R.methodsS3:validators:setMethodS3")
  285. );
  286. ##
  287. ## Method: setRate
  288. ##
  289. ###########################################################################/**
  290. #
  291. # @RdocMethod setRate
  292. #
  293. # @title "Set the general rate"
  294. #
  295. # \description{
  296. # @get "title".
  297. # }
  298. #
  299. # @synopsis
  300. #
  301. # \arguments{
  302. # \item{this}{A GeneralInDel object.}
  303. # \item{value}{The new general rate (a numeric vector of length one).}
  304. # \item{...}{Not used.}
  305. # }
  306. #
  307. # \value{
  308. # The new general rate.
  309. # }
  310. #
  311. # \examples{
  312. # # create a GeneralInDel object
  313. # o<-GeneralInDel(rate=0.5)
  314. # # get/set general rate
  315. # getRate(o)
  316. # setRate(o, 1.5)
  317. # # get/set rate via virtual field
  318. # o$rate
  319. # o$rate<-0.3
  320. # o$rate
  321. # }
  322. #
  323. # @author
  324. #
  325. # \seealso{
  326. # @seeclass
  327. # }
  328. #
  329. #*/###########################################################################
  330. setMethodS3(
  331. "setRate",
  332. class="GeneralInDel",
  333. function(
  334. this,
  335. value,
  336. ...
  337. ){
  338. .checkWriteProtection(this);
  339. if(!exists(x="PSIM_FAST")){
  340. if(missing(value)) {
  341. throw("No new value provided!\n");}
  342. else if(!is.numeric(value)) {
  343. throw("Rate must be numeric!\n");
  344. }
  345. }
  346. this$.rate<-value;
  347. return(this$.rate);
  348. },
  349. private=FALSE,
  350. protected=FALSE,
  351. overwrite=FALSE,
  352. conflict="warning",
  353. validators=getOption("R.methodsS3:validators:setMethodS3")
  354. );
  355. ##
  356. ## Method: getProposeBy
  357. ##
  358. ###########################################################################/**
  359. #
  360. # @RdocMethod getProposeBy
  361. # \alias{getProposeBy.GeneralInsertor}
  362. #
  363. # @title "Get the function used for proposing indel lengths"
  364. #
  365. # \description{
  366. # @get "title".
  367. # }
  368. #
  369. # @synopsis
  370. #
  371. # \arguments{
  372. # \item{this}{A GeneralInDel object.}
  373. # \item{...}{Not used.}
  374. # }
  375. #
  376. # \value{
  377. # A function object.
  378. # }
  379. #
  380. # \examples{
  381. # # create a GeneralInDel object
  382. # # proposing events with a constant length of 5
  383. # o<-GeneralInDel(rate=1, propose.by=function(process){return(5)});
  384. # # set/get the proposeBy function
  385. # setProposeBy(o,value=function(process){return(6)})
  386. # getProposeBy(o)
  387. # # set/get proposeBy function via virtual field
  388. # o$proposeBy<-function(process){return(3)}
  389. # o$proposeBy
  390. # }
  391. #
  392. # @author
  393. #
  394. # \seealso{
  395. # @seeclass
  396. # }
  397. #
  398. #*/###########################################################################
  399. setMethodS3(
  400. "getProposeBy",
  401. class="GeneralInDel",
  402. function(
  403. this,
  404. ...
  405. ){
  406. this$.propose.by;
  407. },
  408. private=FALSE,
  409. protected=FALSE,
  410. overwrite=FALSE,
  411. conflict="warning",
  412. validators=getOption("R.methodsS3:validators:setMethodS3")
  413. );
  414. ##
  415. ## Method: setProposeBy
  416. ##
  417. ###########################################################################/**
  418. #
  419. # @RdocMethod setProposeBy
  420. # \alias{setProposeBy.GeneralInsertor}
  421. #
  422. # @title "Set the function used for proposing indel lengths"
  423. #
  424. # \description{
  425. # @get "title".
  426. #
  427. # The function must return a numeric vector of length one. The function must have an
  428. # argument named "process" which will hold the calling process object.
  429. # }
  430. #
  431. # @synopsis
  432. #
  433. # \arguments{
  434. # \item{this}{A GeneralInDel object.}
  435. # \item{value}{A function object returning a numeric vector of length one.}
  436. # \item{...}{Not used.}
  437. # }
  438. #
  439. # \value{
  440. # The function object (invisible).
  441. # }
  442. #
  443. # \examples{
  444. # # create a GeneralInDel object
  445. # # proposing events with a constant length of 5
  446. # o<-GeneralInDel(rate=1, propose.by=function(process){return(5)});
  447. # # set/get the proposeBy function
  448. # setProposeBy(o,value=function(process){return(6)})
  449. # getProposeBy(o)
  450. # # set/get proposeBy function via virtual field
  451. # o$proposeBy<-function(process){return(3)}
  452. # o$proposeBy
  453. # }
  454. #
  455. # @author
  456. #
  457. # \seealso{
  458. # @seeclass
  459. # }
  460. #
  461. #*/###########################################################################
  462. setMethodS3(
  463. "setProposeBy",
  464. class="GeneralInDel",
  465. function(
  466. this,
  467. value,
  468. ...
  469. ){
  470. .checkWriteProtection(this);
  471. if(!exists(x="PSIM_FAST")){
  472. if(missing(value)) {
  473. throw("No new value provided!\n");
  474. }
  475. else if(!is.function(value)){
  476. throw("The value of proposeBy must be a function.!\n");
  477. }
  478. }
  479. this$.propose.by<-value;
  480. return(invisible(this$.propose.by));
  481. },
  482. private=FALSE,
  483. protected=FALSE,
  484. overwrite=FALSE,
  485. conflict="warning",
  486. validators=getOption("R.methodsS3:validators:setMethodS3")
  487. );
  488. ##
  489. ## Method: getAcceptBy
  490. ##
  491. ###########################################################################/**
  492. #
  493. # @RdocMethod getAcceptBy
  494. # \alias{getAcceptBy.GeneralInsertor}
  495. #
  496. # @title "Get the function used for accepting/rejecting indel events"
  497. #
  498. # \description{
  499. # @get "title".
  500. # }
  501. #
  502. # @synopsis
  503. #
  504. # \arguments{
  505. # \item{this}{A GeneralInDel object.}
  506. # \item{...}{Not used.}
  507. # }
  508. #
  509. # \value{
  510. # A function object.
  511. # }
  512. #
  513. # \examples{
  514. # # create a GeneralInDel object
  515. # # rejecting half of the events
  516. # o<-GeneralInDel(
  517. # rate=1,
  518. # propose.by=function(process){return(5)},
  519. # accept.by=function( ){sample(c(TRUE,FALSE),1)}
  520. # );
  521. # # set/get the acceptBy function
  522. # setAcceptBy(o,value=function(){return(FALSE)}) # reject all events
  523. # getAcceptBy(o)
  524. # # set/get acceptBy function via virtual field
  525. # o$acceptBy<-function(){return(TRUE)} # accept all events
  526. # o$acceptBy
  527. # }
  528. #
  529. # @author
  530. #
  531. # \seealso{
  532. # @seeclass
  533. # }
  534. #
  535. #*/###########################################################################
  536. setMethodS3(
  537. "getAcceptBy",
  538. class="GeneralInDel",
  539. function(
  540. this,
  541. ...
  542. ){
  543. this$.accept.by;
  544. },
  545. private=FALSE,
  546. protected=FALSE,
  547. overwrite=FALSE,
  548. conflict="warning",
  549. validators=getOption("R.methodsS3:validators:setMethodS3")
  550. );
  551. ##
  552. ## Method: setAcceptBy
  553. ##
  554. ###########################################################################/**
  555. #
  556. # @RdocMethod setAcceptBy
  557. # \alias{setAcceptBy.GeneralInsertor}
  558. #
  559. # @title "Set the function used for accepting/rejecting indel events"
  560. #
  561. # \description{
  562. # @get "title".
  563. #
  564. # The function object must have the following arguments: process (the caller process), sequence (the target sequence),
  565. # window (a vector containing the positions affecting acceptance).
  566. # }
  567. #
  568. # @synopsis
  569. #
  570. # \arguments{
  571. # \item{this}{A GeneralInDel object.}
  572. # \item{value}{A function object.}
  573. # \item{...}{Not used.}
  574. # }
  575. #
  576. # \value{
  577. # The function object (invisible).
  578. # }
  579. #
  580. # \examples{
  581. # # create a GeneralInDel object
  582. # # rejecting half of the events
  583. # o<-GeneralInDel(
  584. # rate=1,
  585. # propose.by=function(process){return(5)},
  586. # accept.by=function( ){sample(c(TRUE,FALSE),1)}
  587. # );
  588. # # set/get the acceptBy function
  589. # setAcceptBy(o,value=function( ){return(FALSE)}) # reject all events
  590. # getAcceptBy(o)
  591. # # set/get acceptBy function via virtual field
  592. # o$acceptBy<-function(){return(TRUE)} # accept all events
  593. # o$acceptBy
  594. # }
  595. #
  596. # @author
  597. #
  598. # \seealso{
  599. # @seeclass
  600. # }
  601. #
  602. #*/###########################################################################
  603. setMethodS3(
  604. "setAcceptBy",
  605. class="GeneralInDel",
  606. function(
  607. this,
  608. value,
  609. ...
  610. ){
  611. .checkWriteProtection(this);
  612. if(!exists(x="PSIM_FAST")){
  613. if(missing(value)) {
  614. throw("No new value provided!\n");
  615. }
  616. else if(!is.function(value)){
  617. throw("The value of acceptBy must be a function.!\n");
  618. }
  619. }
  620. this$.accept.by<-value;
  621. return(invisible(this$.accept.by));
  622. },
  623. private=FALSE,
  624. protected=FALSE,
  625. overwrite=FALSE,
  626. conflict="warning",
  627. validators=getOption("R.methodsS3:validators:setMethodS3")
  628. );
  629. ##
  630. ## Method: proposeLength
  631. ##
  632. ###########################################################################/**
  633. #
  634. # @RdocMethod proposeLength
  635. #
  636. # @title "Propose indel length"
  637. #
  638. # \description{
  639. # @get "title".
  640. #
  641. # This method simply calls the function returned by the \code{getProposeBy} method.
  642. # }
  643. #
  644. # @synopsis
  645. #
  646. # \arguments{
  647. # \item{this}{A GeneralInDel object.}
  648. # \item{...}{Not used.}
  649. # }
  650. #
  651. # \value{
  652. # A numeric vector of length one (the indel length).
  653. # }
  654. #
  655. # \examples{
  656. # # create a GeneralInDel object
  657. # # proposing event lengths in the range 1:10
  658. # o<-GeneralInDel(rate=1, propose.by=function(process){sample(c(1:10),1)});
  659. # # propose indel length
  660. # proposeLength(o)
  661. # }
  662. #
  663. # @author
  664. #
  665. # \seealso{
  666. # @seeclass
  667. # }
  668. #
  669. #*/###########################################################################
  670. setMethodS3(
  671. "proposeLength",
  672. class="GeneralInDel",
  673. function(
  674. this,
  675. ...
  676. ){
  677. return( this$.propose.by(this));
  678. },
  679. private=FALSE,
  680. protected=FALSE,
  681. overwrite=FALSE,
  682. conflict="warning",
  683. validators=getOption("R.methodsS3:validators:setMethodS3")
  684. );
  685. ##
  686. ## Method: is.GeneralIndel
  687. ##
  688. ###########################################################################/**
  689. # @RdocDefault is.GeneralInDel
  690. #
  691. # @title "Check if an object inherits from the GeneralInDel class"
  692. #
  693. # \description{
  694. # @get "title".
  695. # }
  696. #
  697. # @synopsis
  698. #
  699. # \arguments{
  700. # \item{this}{An object.}
  701. # \item{...}{Not used.}
  702. #
  703. # }
  704. #
  705. # \value{
  706. # TRUE or FALSE.
  707. # }
  708. #
  709. # \examples{
  710. # # create some objects
  711. # o<-GeneralInDel(rate=1, propose.by=function(process){sample(c(1:10),1)});
  712. # x<-GTR()
  713. # # check if they inherit from GeneralInDel
  714. # is.GeneralInDel(o)
  715. # is.GeneralInDel(x)
  716. # }
  717. #
  718. #
  719. # @author
  720. #
  721. #*/###########################################################################
  722. setMethodS3(
  723. "is.GeneralInDel",
  724. class="default",
  725. function(
  726. this,
  727. ...
  728. ){
  729. if(!is.PSRoot(this)) {return(FALSE)}
  730. if(!is.null(this$.is.general.indel)){return(TRUE)}
  731. if ( inherits(this, "GeneralInDel")) {
  732. this$.is.general.indel<-TRUE;
  733. return(TRUE);
  734. } else {
  735. return(FALSE)
  736. }
  737. },
  738. private=FALSE,
  739. protected=FALSE,
  740. overwrite=FALSE,
  741. conflict="warning",
  742. validators=getOption("R.methodsS3:validators:setMethodS3")
  743. );
  744. ##
  745. ## Method: summary
  746. ##
  747. ###########################################################################/**
  748. #
  749. # @RdocMethod summary
  750. #
  751. # @title "Summarize the properties of an object"
  752. #
  753. # \description{
  754. # @get "title".
  755. # }
  756. #
  757. # @synopsis
  758. #
  759. # \arguments{
  760. # \item{object}{An object}
  761. # \item{...}{Not used.}
  762. # }
  763. #
  764. # \value{
  765. # Returns a PSRootSummary object.
  766. # }
  767. #
  768. # \examples{
  769. #
  770. # # create an object
  771. # a<-GeneralInDel(rate=1,propose.by=function(process){sample(c(1,2,3),1)})
  772. # # get a summary
  773. # summary(a)
  774. # }
  775. #
  776. # @author
  777. #
  778. # \seealso{
  779. # @seeclass
  780. # }
  781. #
  782. #*/###########################################################################
  783. setMethodS3(
  784. "summary",
  785. class="GeneralInDel",
  786. function(
  787. object,
  788. ...
  789. ){
  790. .addSummaryNameId(object);
  791. object$.summary$"General rate"<-object$rate;
  792. NextMethod();
  793. },
  794. private=FALSE,
  795. protected=FALSE,
  796. overwrite=FALSE,
  797. conflict="warning",
  798. validators=getOption("R.methodsS3:validators:setMethodS3")
  799. );
  800. ###########################################################################
  801. # Class:GeneralInsertor
  802. ##########################################################################/**
  803. #
  804. # @RdocClass GeneralInsertor
  805. #
  806. # @title "The GeneralInsertor class"
  807. #
  808. # \description{
  809. #
  810. # This is a class implementing a process generating insertion events.
  811. # The rate of each event is calculated as the product of the general rate of the process
  812. # and the "rate.multiplier" site-process specific parameter.
  813. # The simulation code calls the \code{Perform} method on the selected insertion event objects,
  814. # which call their insertion event handler to perform the insertion.
  815. #
  816. # The insert lengths are proposed by the function stored in the \code{proposeBy}
  817. # virtual field. The function must have the following arguments:
  818. # process (the insertion process object).
  819. #
  820. # The insertion events are accepted or rejected by the function stored in the \code{acceptBy} virtual field.
  821. # The function must have the following arguments: process (the insertion process object), sequence (the target sequence object),
  822. # window (a vector of positions affecting acceptance).
  823. # The probability of accepting an insertion is calculated as the product of the site-process-specific
  824. # "insertion.tolerance" parameters of the sites neighboring the insertion.
  825. # The number of sites considered is determined by the \code{acceptWin} virtual field.
  826. #
  827. # The insert is generated by the \code{generateInsert} method by calling the function stored in the \code{generateBy} virtual field.
  828. # The default generator function truncates/duplicates the sequence object stored in the \code{templateSeq} virtual field to get a sequence
  829. # having the sampled length. After constructing the Sequence object, it calls the \code{sampleStates.Sequence} method on the resulting object.
  830. # That means that if we start with a template sequence which has NA states, but has a substitution process attached, then the resulting sequence
  831. # will be different every time.
  832. #
  833. # Before inserting the sequence returned by \code{generateInsert}, the handler function will pass the object through the function stored in the
  834. # \code{insertHook} virtual field. This allows to perform arbitrary modifications on the inserted Sequence object.
  835. #
  836. # The sequence is inserted randomly on the left or the right of the target position.
  837. #
  838. # @classhierarchy
  839. # }
  840. #
  841. # @synopsis
  842. #
  843. # \arguments{
  844. # \item{name}{The name of the object.}
  845. # \item{rate}{The general rate of the object (no default).}
  846. # \item{propose.by}{A function used to propose events (no default).}
  847. # \item{accept.by}{A function used to accept/reject events (no default).}
  848. # \item{template.seq}{A Sequence object used as a template for generating insertions (no default).}
  849. # \item{insert.hook}{A function object, see \code{setInsertHook} (no default).}
  850. # \item{accept.win}{A window of sites affecting the acceptance of insert events.}
  851. # \item{...}{Additional arguments.}
  852. # }
  853. #
  854. # \section{Fields and Methods}{
  855. # @allmethods
  856. # }
  857. #
  858. # \examples{
  859. # # create a GeneralInsertor object
  860. # i<-GeneralInsertor(
  861. # name="GIN",
  862. # rate=1,
  863. # propose.by=function(process){4}, # fixed insert length
  864. # acceptBy=function(process,sequence,window){TRUE},# always accept insertions
  865. # template.seq=NucleotideSequence(string="A"),# a boring template sequence
  866. # insert.hook=function(seq){ return(seq)},# a boring insert hook
  867. # accept.win=2 #4 sites affecting acceptance
  868. # )
  869. # i
  870. # # check if object inherits from GeneralInsertor
  871. # is.GeneralInsertor(i)
  872. # # get object summary
  873. # summary(i)
  874. # # set/get general rate
  875. # i$rate<-0.5
  876. # i$rate
  877. # # set/get name
  878. # i$name<-"Ins"
  879. # i$name
  880. # # set/get proposeBy
  881. # # sample insertion length between 1 and 10
  882. # i$proposeBy<-function(process){sample(1:10,1)}
  883. # i$proposeBy
  884. # # set/get acceptBy
  885. # # reject half of the insertions
  886. # i$acceptBy<-function(process, sequence, window){ sample(c(TRUE,FALSE), 1) }
  887. # i$acceptBy
  888. # # get generateBy
  889. # i$generateBy
  890. # # set/get acceptWin
  891. # i$acceptWin<-1;
  892. # # set/get insert hook
  893. # i$insertHook<-function(
  894. # seq,
  895. # target.seq,
  896. # event.pos,
  897. # insert.pos
  898. # ){ attachProcess(seq, GTR() );seq}
  899. # i$insertHook
  900. # # set/get template sequence
  901. # i$templateSeq<-NucleotideSequence(
  902. # length=5,
  903. # processes=list(list(JC69()))
  904. # ) # length: 5, states: NA
  905. # i$templateSeq
  906. # # generate an insert sequence
  907. # generateInsert(i)
  908. # # create a sequence object and attach the process i
  909. # s<-NucleotideSequence(string="AAAAA",processes=list(list(i)))
  910. # # set rate multiplier
  911. # setRateMultipliers(s,i,2)
  912. # # get the list of active events from site 2
  913. # events<-getEventsAtSite(i,s$sites[[2]])
  914. # events
  915. # # set postition for event
  916. # e<-events[[1]]
  917. # e$.position<-2
  918. # # print sequence
  919. # s
  920. # # perform event
  921. # Perform(e)
  922. # # check sequence again
  923. # s
  924. # }
  925. #
  926. # @author
  927. #
  928. # \seealso{
  929. # GeneralInDel DiscreteInsertor ContinuousInsertor BrownianInsertor
  930. # }
  931. #
  932. #*/###########################################################################
  933. setConstructorS3(
  934. "GeneralInsertor",
  935. function(
  936. name="Anonymous",
  937. rate=NA,
  938. propose.by=NA,
  939. accept.by=NA,
  940. template.seq=NA,
  941. insert.hook=NA,
  942. accept.win=NA,
  943. ...
  944. ) {
  945. this<-GeneralInDel(
  946. rate=rate,
  947. propose.by=propose.by,
  948. accept.by=accept.by
  949. );
  950. this<-extend(
  951. this,
  952. "GeneralInsertor",
  953. .generate.by=NA,
  954. .handler.template=NA,
  955. .template.seq=NA,
  956. .insert.hook=NA,
  957. .accept.win=1,
  958. .is.general.insertor=TRUE
  959. );
  960. # Using virtual field to clear Id cache:
  961. this$name<-name;
  962. # Adding insertion tolerance parameter.
  963. .addSiteSpecificParameter(
  964. this,
  965. id="insertion.tolerance",
  966. name="Insertion tolerance parameter",
  967. value=as.double(1), # Accept all by default
  968. type="numeric"
  969. );
  970. if(!missing(template.seq)){
  971. this$templateSeq<-template.seq;
  972. }
  973. this$acceptBy<-function(process=NA,sequence=NA,range=NA){
  974. accept.prob<-c();
  975. for(site in sequence$.sites[range]){
  976. # Discard the site if the process is not attached to it:
  977. if(!isAttached(site, process)){
  978. next();
  979. }
  980. else {
  981. accept.prob<-c(accept.prob, getParameterAtSite(process, site, "insertion.tolerance")$value);
  982. }
  983. }
  984. accept.prob<-prod(as.numeric(accept.prob));
  985. # Accept/reject:
  986. return( sample(c(TRUE,FALSE),replace=FALSE,prob=c(accept.prob,(1-accept.prob)),size=1) );
  987. }
  988. ###
  989. this$generateBy<-function(process=NA,length=NA,target.seq=NA,event.pos=NA,insert.pos=NA){
  990. if(!exists(x="PSIM_FAST")){
  991. if(is.na(length) | (length(length) == 0) | length == 0){
  992. throw("Invalid insert length!\n");
  993. }
  994. else if(is.na(process$.template.seq)){
  995. throw("Cannot generate insert without template sequence!\n");
  996. }
  997. }
  998. times<-( ceiling( length/this$.template.seq$.length) );
  999. to.delete<-( ( (this$.template.seq$.length) * times) - length);
  1000. tmp<-clone(this$.template.seq);
  1001. if( (times-1) > 0){
  1002. for(i in 1:(times-1)){
  1003. insertSequence(tmp,process$.template.seq,tmp$length);
  1004. }
  1005. }
  1006. if(to.delete > 0){
  1007. deleteSubSequence(tmp,(tmp$length - to.delete + 1):tmp$length);
  1008. }
  1009. return(tmp);
  1010. }
  1011. if(!missing(insert.hook)){
  1012. this$insertHook<-insert.hook;
  1013. }
  1014. ###
  1015. this$.handler.template<-function(event=NA) {
  1016. if(!is.na(event)){
  1017. WINDOW.SIZE<-this$.accept.win;
  1018. # Using temporary varibales for clarity:
  1019. position<-event$.position;
  1020. process<-event$.process;
  1021. sequence<-event$.site$.sequence;
  1022. details<-list();
  1023. details$type<-"insertion";
  1024. # Propose the direction:
  1025. direction<-sample(c("LEFT","RIGHT"),replace=FALSE,size=1);
  1026. # Set insertion tolerance window:
  1027. window<-integer();
  1028. insert.pos<-position;
  1029. if(direction == "LEFT") {
  1030. window<-(position-WINDOW.SIZE):position;
  1031. insert.pos<-(position-1);
  1032. }
  1033. else if (direction == "RIGHT"){
  1034. window<-position:(position+WINDOW.SIZE);
  1035. }
  1036. else {
  1037. throw("You should never see this message!\n");
  1038. }
  1039. details$position<-insert.pos;
  1040. details$accepted<-FALSE;
  1041. # Discard illegal positions:
  1042. window<-window[ window > 0 & window <= sequence$.length];
  1043. if(process$.accept.by(process=process,sequence,window)){
  1044. details$accepted<-TRUE;
  1045. insert<-generateInsert(process,target.seq=sequence,event.pos=position,insert.pos=insert.pos);
  1046. details$length<-insert$length;
  1047. # Call the insert hook:
  1048. if(is.function(this$.insert.hook)){
  1049. insert<-this$.insert.hook(seq=insert,target.seq=sequence,event.pos=position,insert.pos=insert.pos);
  1050. }
  1051. insertSequence(sequence,insert, insert.pos,process=process);
  1052. }
  1053. return(details);
  1054. }
  1055. }
  1056. ###
  1057. return(this);
  1058. },
  1059. enforceRCC=TRUE
  1060. );
  1061. ##
  1062. ## Method: is.GeneralInsertor
  1063. ##
  1064. ###########################################################################/**
  1065. #
  1066. # @RdocDefault is.GeneralInsertor
  1067. #
  1068. # @title "Check whether an object inherits from GeneralInsertor"
  1069. #
  1070. # \description{
  1071. # @get "title".
  1072. # }
  1073. #
  1074. # @synopsis
  1075. #
  1076. # \arguments{
  1077. # \item{this}{An object.}
  1078. # \item{...}{Not used.}
  1079. #
  1080. # }
  1081. #
  1082. # \value{
  1083. # TRUE or FALSE.
  1084. # }
  1085. #
  1086. # \examples{
  1087. # # create some objects
  1088. # d<-GeneralDeletor()
  1089. # i<-GeneralInsertor()
  1090. # # check if they inherit from GeneralInsertor
  1091. # is.GeneralInsertor(i)
  1092. # is.GeneralInsertor(d)
  1093. # }
  1094. #
  1095. # @author
  1096. #
  1097. #*/###########################################################################
  1098. setMethodS3(
  1099. "is.GeneralInsertor",
  1100. class="default",
  1101. function(
  1102. this,
  1103. ...
  1104. ){
  1105. if(!is.PSRoot(this)) {return(FALSE)}
  1106. if(!is.null(this$.is.general.insertor)){return(TRUE)}
  1107. if ( inherits(this, "GeneralInsertor")) {
  1108. this$.is.general.insertor<-TRUE;
  1109. return(TRUE);
  1110. } else {
  1111. return(FALSE)
  1112. }
  1113. },
  1114. private=FALSE,
  1115. protected=FALSE,
  1116. overwrite=FALSE,
  1117. conflict="warning",
  1118. validators=getOption("R.methodsS3:validators:setMethodS3")
  1119. );
  1120. ##
  1121. ## Method: checkConsistency
  1122. ##
  1123. ###########################################################################/**
  1124. #
  1125. # @RdocMethod checkConsistency
  1126. #
  1127. # @title "Check object consistency"
  1128. #
  1129. # \description{
  1130. # @get "title".
  1131. # }
  1132. #
  1133. # @synopsis
  1134. #
  1135. # \arguments{
  1136. # \item{this}{An object.}
  1137. # \item{...}{Not used.}
  1138. # }
  1139. #
  1140. #
  1141. # \value{
  1142. # Returns an invisible TRUE if no inconsistencies found in the object, throws
  1143. # an error otherwise.
  1144. # }
  1145. #
  1146. # @author
  1147. #
  1148. # \seealso{
  1149. # @seeclass
  1150. # }
  1151. #
  1152. #*/###########################################################################
  1153. setMethodS3(
  1154. "checkConsistency",
  1155. class="GeneralInsertor",
  1156. function(
  1157. this,
  1158. ...
  1159. ){
  1160. wp<-this$writeProtected;
  1161. if (wp) {
  1162. this$writeProtected<-FALSE;
  1163. }
  1164. may.fail<-function(this) {
  1165. if (!is.na(this$templateSeq)) {
  1166. this$templateSeq<-this$templateSeq;
  1167. }
  1168. if(!is.function(this$generateBy)){
  1169. if(!is.na(this$generateBy)){
  1170. throw("generateBy is invalid!\n");
  1171. }
  1172. }
  1173. }
  1174. tryCatch(may.fail(this),finally=this$writeProtected<-wp);
  1175. NextMethod();
  1176. },
  1177. private=FALSE,
  1178. protected=FALSE,
  1179. overwrite=FALSE,
  1180. conflict="warning",
  1181. validators=getOption("R.methodsS3:validators:setMethodS3")
  1182. );
  1183. ##
  1184. ## Method: getEventsAtSite
  1185. ##
  1186. ###########################################################################/**
  1187. #
  1188. # @RdocMethod getEventsAtSite
  1189. #
  1190. # @title "Generate insertion event object given the state of the target site"
  1191. #
  1192. # \description{
  1193. # @get "title".
  1194. #
  1195. # This method generates a list with one insertion event. The rate of the
  1196. # event is calculated as the product of the general rate of the process
  1197. # and the "rate.multiplier" site-process specific parameter. An empty list is
  1198. # returned if the rate is zero or NA.
  1199. # }
  1200. #
  1201. # @synopsis
  1202. #
  1203. # \arguments{
  1204. # \item{this}{A GeneralInsertor object.}
  1205. # \item{target.site}{A Site object.}
  1206. # \item{...}{Not used.}
  1207. # }
  1208. #
  1209. # \value{
  1210. # A list of Event objects.
  1211. # }
  1212. #
  1213. # \examples{
  1214. # # create a sequence object
  1215. # s<-NucleotideSequence(string="AAAA")
  1216. # # create a GeneralInsertor process, provide template sequence.
  1217. # # propsed insert lengths:3, always accept.
  1218. # i<-GeneralInsertor(
  1219. # rate=0.5,
  1220. # template.seq=NucleotideSequence(string="GGG"),
  1221. # propose.by=function(process){3},
  1222. # accept.by=function(process,sequence,window){TRUE}
  1223. # )
  1224. # # attach process to site
  1225. # s$processes<-list(list(i));
  1226. # # set rate multiplier
  1227. # setRateMultipliers(s,i,2)
  1228. # # get the list of active events from site 2
  1229. # events<-getEventsAtSite(i,s$sites[[2]])
  1230. # events
  1231. # # set postition for event
  1232. # e<-events[[1]]
  1233. # e$.position<-2
  1234. # # print sequence
  1235. # s
  1236. # # perform event
  1237. # Perform(e)
  1238. # # check sequence again
  1239. # s
  1240. # }
  1241. #
  1242. # @author
  1243. #
  1244. # \seealso{
  1245. # GeneralInsertor GeneralInDel Process Event
  1246. # }
  1247. #
  1248. #*/###########################################################################
  1249. setMethodS3(
  1250. "getEventsAtSite",
  1251. class="GeneralInsertor",
  1252. function(
  1253. this,
  1254. target.site,
  1255. ...
  1256. ){
  1257. if(!exists(x="PSIM_FAST")){
  1258. if(missing(target.site)) {
  1259. throw("No target site provided!\n");
  1260. }
  1261. if(!is.Site(target.site)) {
  1262. throw("Target site invalid!\n");
  1263. }
  1264. else if(!is.function(this$.propose.by)) {
  1265. throw("proposeBy is not set, cannot propose insertion!\n");
  1266. }
  1267. else if (!is.function(this$.accept.by)){
  1268. throw("acceptBy is not set, cannot generate insertion event!\n");
  1269. }
  1270. }
  1271. # Just return an empty list if the rate is undefined or zero:
  1272. if( is.na(this$.rate) | this$.rate == 0) {
  1273. return(list());
  1274. }
  1275. # Clone the event template object:
  1276. insertion.event<-clone(this$.event.template);
  1277. # Set the target position passed in a temporary field:
  1278. insertion.event$.position<-target.site$.position;
  1279. # Set the target site:
  1280. insertion.event$.site<-target.site;
  1281. # Set event name:
  1282. insertion.event$.name<-"Insertion";
  1283. # Set the generator process:
  1284. insertion.event$.process<-this;
  1285. # Event rate is the product of the general rate and the
  1286. # site specific rate multiplier:
  1287. rate.multiplier<-target.site$.processes[[this$.id]]$site.params[["rate.multiplier"]]$value;
  1288. if(rate.multiplier == 0 ) {
  1289. return(list());
  1290. }
  1291. insertion.event$.rate<-(this$.rate * rate.multiplier );
  1292. # Set the handler for the insertion event:
  1293. insertion.event$.handler<-this$.handler.template;
  1294. # Return the event object in a list:
  1295. list(insertion.event);
  1296. },
  1297. private=FALSE,
  1298. protected=FALSE,
  1299. overwrite=FALSE,
  1300. conflict="warning",
  1301. validators=getOption("R.methodsS3:validators:setMethodS3")
  1302. );
  1303. ##
  1304. ## Method: generateInsert
  1305. ##
  1306. ###########################################################################/**
  1307. #
  1308. # @RdocMethod generateInsert
  1309. #
  1310. # @title "Generate an insert"
  1311. #
  1312. # \description{
  1313. # @get "title".
  1314. #
  1315. # This method uses the function stgored in the \code{proposeBy} virtual field to
  1316. # sample the insert length and then calls the function stored in the \code{generateBy}
  1317. # field to generate the insert.
  1318. #
  1319. # The default \code{generateBy} function set by the GeneralInsertor constructor truncates/repeats
  1320. # the template sequence stored in the \code{templateSeq} field to have the sequence with the right size
  1321. # and then calls the \code{sampleStates} method on the resulting object. That means that if we start with a
  1322. # template sequence which has NA states, but has a substitution process attached, then the resulting sequence
  1323. # will be different every time the \code{generateInsert} method is called.
  1324. #
  1325. # The \code{generateBy}, \code{proposeBy} and \code{templateSeq} fields must be set in order to use this method.
  1326. # }
  1327. #
  1328. # @synopsis
  1329. #
  1330. # \arguments{
  1331. # \item{this}{A GeneralInsertor object.}
  1332. # \item{length}{Generate an insert with the specified length if this argument is present.}
  1333. # \item{target.seq}{The Sequence object targeted by the insertion (optional). This argument is passed to the \code{generateBy} method.}
  1334. # \item{event.pos}{The position of the site proposing the insertion (optional). This argument is passed to the \code{generateBy} method.}
  1335. # \item{insert.pos}{The position of the insertion in the target sequence (optional). This argument is passed to the \code{generateBy} method.}
  1336. # \item{...}{Not used.}
  1337. # }
  1338. #
  1339. # \value{
  1340. # A Sequence object.
  1341. # }
  1342. #
  1343. # \examples{
  1344. # # build the template sequence
  1345. # ts<-NucleotideSequence(length = 10,processes=list(list(JC69())));
  1346. # # fix some site states
  1347. # setStates(ts,"A",c(1,2));
  1348. # setStates(ts,"T",c(5,6));
  1349. # setStates(ts,"C",c(9,10));
  1350. # # print template sequence
  1351. # ts
  1352. # # create a GeneralInsertor object
  1353. # i<-GeneralInsertor(
  1354. # rate=0.5,
  1355. # template.seq=ts,
  1356. # propose.by=function(process){sample(c(5:50),1)}, # inserts between 5 and 50
  1357. # )
  1358. # # generate some inserts
  1359. # generateInsert(i)
  1360. # generateInsert(i)
  1361. # generateInsert(i)
  1362. # generateInsert(i)
  1363. # }
  1364. #
  1365. # @author
  1366. #
  1367. # \seealso{
  1368. # @seeclass
  1369. # }
  1370. #
  1371. #*/###########################################################################
  1372. setMethodS3(
  1373. "generateInsert",
  1374. class="GeneralInsertor",
  1375. function(
  1376. this,
  1377. length=NA,
  1378. target.seq=NA,
  1379. event.pos=NA,
  1380. insert.pos=NA,
  1381. ...
  1382. ){
  1383. if(missing(length)){
  1384. length<-this$.propose.by(process=this);
  1385. }
  1386. insert<-this$.generate.by(process=this,length=length,target.seq=target.seq,event.pos=event.pos,insert.pos=insert.pos);
  1387. sampleStates(insert);
  1388. return(insert);
  1389. },
  1390. private=FALSE,
  1391. protected=FALSE,
  1392. overwrite=FALSE,
  1393. conflict="warning",
  1394. validators=getOption("R.methodsS3:validators:setMethodS3")
  1395. );
  1396. ##
  1397. ## Method: getGenerateBy
  1398. ##
  1399. ###########################################################################/**
  1400. #
  1401. # @RdocMethod getGenerateBy
  1402. #
  1403. # @title "Get the function object used for generating inserts"
  1404. #
  1405. # \description{
  1406. # @get "title".
  1407. # }
  1408. #
  1409. # @synopsis
  1410. #
  1411. # \arguments{
  1412. # \item{this}{A GeneralInsertor object.}
  1413. # \item{...}{Not used.}
  1414. # }
  1415. #
  1416. # \value{
  1417. # A function object.
  1418. # }
  1419. #
  1420. # \examples{
  1421. # # create a GeneralInsertor object
  1422. # i<-GeneralInsertor(
  1423. # rate=0.5,
  1424. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1425. # template.seq=NucleotideSequence(string="AAAAAAA")
  1426. # )
  1427. #
  1428. # # save insert generator
  1429. # old.gen<-getGenerateBy(i)
  1430. # # set a new insert generator
  1431. # i$generateBy<-function(
  1432. # process,
  1433. # length,
  1434. # target.seq,
  1435. # event.pos,
  1436. # insert.pos
  1437. # ){
  1438. # return(NucleotideSequence(string="AATTGGCC"))
  1439. # }
  1440. # # get the generator function
  1441. # i$generateBy
  1442. # # generate insert
  1443. # generateInsert(i)
  1444. # # restore old generator
  1445. # i$generateBy<-old.gen
  1446. # # generate insert
  1447. # generateInsert(i)
  1448. # }
  1449. #
  1450. # @author
  1451. #
  1452. # \seealso{
  1453. # @seeclass
  1454. # }
  1455. #
  1456. #*/###########################################################################
  1457. setMethodS3(
  1458. "getGenerateBy",
  1459. class="GeneralInsertor",
  1460. function(
  1461. this,
  1462. ...
  1463. ){
  1464. this$.generate.by;
  1465. },
  1466. private=FALSE,
  1467. protected=FALSE,
  1468. overwrite=FALSE,
  1469. conflict="warning",
  1470. validators=getOption("R.methodsS3:validators:setMethodS3")
  1471. );
  1472. ##
  1473. ## Method: setGenerateBy
  1474. ##
  1475. ###########################################################################/**
  1476. #
  1477. # @RdocMethod setGenerateBy
  1478. #
  1479. # @title "Set the function object used for generating inserts"
  1480. #
  1481. # \description{
  1482. # @get "title".
  1483. # The provided function must return a Sequence object whne called and must have the
  1484. # following arguments: process, length, target.seq, event.pos, insert.pos (see \code{generateInsert.GeneralInsertor}).
  1485. # }
  1486. #
  1487. # @synopsis
  1488. #
  1489. # \arguments{
  1490. # \item{this}{A GeneralInsertor object.}
  1491. # \item{value}{A function object.}
  1492. # \item{...}{Not used.}
  1493. # }
  1494. #
  1495. # \value{
  1496. # The function object.
  1497. # }
  1498. #
  1499. # \examples{
  1500. # # create a GeneralInsertor object
  1501. # i<-GeneralInsertor(
  1502. # rate=0.5,
  1503. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1504. # template.seq=NucleotideSequence(string="AAAAAAA")
  1505. # )
  1506. #
  1507. # # save insert generator
  1508. # old.gen<-getGenerateBy(i)
  1509. # # set a new insert generator
  1510. # i$generateBy<-function(
  1511. # process,
  1512. # length,
  1513. # target.seq,
  1514. # event.pos,
  1515. # insert.pos){
  1516. # return(NucleotideSequence(string="AATTGGCC"))
  1517. # }
  1518. # # get the generator function
  1519. # i$generateBy
  1520. # # generate insert
  1521. # generateInsert(i)
  1522. # # restore old generator
  1523. # i$generateBy<-old.gen
  1524. # # generate insert
  1525. # generateInsert(i)
  1526. # }
  1527. #
  1528. # @author
  1529. #
  1530. # \seealso{
  1531. # @seeclass
  1532. # }
  1533. #
  1534. #*/###########################################################################
  1535. setMethodS3(
  1536. "setGenerateBy",
  1537. class="GeneralInsertor",
  1538. function(
  1539. this,
  1540. value,
  1541. ...
  1542. ){
  1543. .checkWriteProtection(this);
  1544. if(!exists(x="PSIM_FAST")){
  1545. if(missing(value)) {
  1546. throw("No new value provided!\n");
  1547. }
  1548. else if(!is.function(value)){
  1549. throw("The value of generateBy must be a function.!\n");
  1550. }
  1551. }
  1552. this$.generate.by<-value;
  1553. return(this$.generate.by);
  1554. },
  1555. private=FALSE,
  1556. protected=FALSE,
  1557. overwrite=FALSE,
  1558. conflict="warning",
  1559. validators=getOption("R.methodsS3:validators:setMethodS3")
  1560. );
  1561. ##
  1562. ## Method: getTemplateSeq
  1563. ##
  1564. ###########################################################################/**
  1565. #
  1566. # @RdocMethod getTemplateSeq
  1567. #
  1568. # @title "Get the template sequence object"
  1569. #
  1570. # \description{
  1571. # @get "title".
  1572. # The template sequence object is used by the default \code{generateBy} function
  1573. # to generate insert sequences.
  1574. # }
  1575. #
  1576. # @synopsis
  1577. #
  1578. # \arguments{
  1579. # \item{this}{A GeneralInsertor object.}
  1580. # \item{...}{Not used.}
  1581. # }
  1582. #
  1583. # \value{
  1584. # A Sequence object or NA.
  1585. # }
  1586. #
  1587. # \examples{
  1588. # # create a GeneralInsertor object
  1589. # i<-GeneralInsertor(
  1590. # rate=0.5,
  1591. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1592. # template.seq=NucleotideSequence(string="AAAAAAA")
  1593. # )
  1594. # # get template sequence
  1595. # getTemplateSeq(i)
  1596. # # get template sequence via virtual field
  1597. # i$templateSeq
  1598. # # set template sequence
  1599. # setTemplateSeq(i, NucleotideSequence(string="C"));
  1600. # # generate insert
  1601. # generateInsert(i)
  1602. # # set template sequence via virtual field
  1603. # i$templateSeq<-NucleotideSequence(string="G")
  1604. # # generate insert
  1605. # generateInsert(i)
  1606. # }
  1607. #
  1608. # @author
  1609. #
  1610. # \seealso{
  1611. # @seeclass
  1612. # }
  1613. #
  1614. #*/###########################################################################
  1615. setMethodS3(
  1616. "getTemplateSeq",
  1617. class="GeneralInsertor",
  1618. function(
  1619. this,
  1620. ...
  1621. ){
  1622. this$.template.seq;
  1623. },
  1624. private=FALSE,
  1625. protected=FALSE,
  1626. overwrite=FALSE,
  1627. conflict="warning",
  1628. validators=getOption("R.methodsS3:validators:setMethodS3")
  1629. );
  1630. ##
  1631. ## Method: setTemplateSeq
  1632. ##
  1633. ###########################################################################/**
  1634. #
  1635. # @RdocMethod setTemplateSeq
  1636. #
  1637. # @title "Set the template sequence object"
  1638. #
  1639. # \description{
  1640. # @get "title".
  1641. # The template sequence object is used by the default \code{generateBy} function
  1642. # to generate insert sequences.
  1643. # }
  1644. #
  1645. # @synopsis
  1646. #
  1647. # \arguments{
  1648. # \item{this}{A GeneralInsertor object.}
  1649. # \item{value}{A Sequence object.}
  1650. # \item{...}{Not used.}
  1651. # }
  1652. #
  1653. # \value{
  1654. # The Sequence object.
  1655. # }
  1656. #
  1657. # \examples{
  1658. # # create a GeneralInsertor object
  1659. # i<-GeneralInsertor(
  1660. # rate=0.5,
  1661. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1662. # template.seq=NucleotideSequence(string="AAAAAAA")
  1663. # )
  1664. # # get template sequence
  1665. # getTemplateSeq(i)
  1666. # # get template sequence via virtual field
  1667. # i$templateSeq
  1668. # # set template sequence
  1669. # setTemplateSeq(i, NucleotideSequence(string="C"));
  1670. # # generate insert
  1671. # generateInsert(i)
  1672. # # set template sequence via virtual field
  1673. # i$templateSeq<-NucleotideSequence(string="G")
  1674. # # generate insert
  1675. # generateInsert(i)
  1676. # }
  1677. #
  1678. # @author
  1679. #
  1680. # \seealso{
  1681. # @seeclass
  1682. # }
  1683. #
  1684. #*/###########################################################################
  1685. setMethodS3(
  1686. "setTemplateSeq",
  1687. class="GeneralInsertor",
  1688. function(
  1689. this,
  1690. value,
  1691. ...
  1692. ){
  1693. .checkWriteProtection(this);
  1694. if(!exists(x="PSIM_FAST")){
  1695. if(missing(value)) {
  1696. throw("No new template sequence provided!\n");
  1697. }
  1698. else if(!is.Sequence(value)){
  1699. throw("Sequence object is invalid!\n");
  1700. }
  1701. else if(value$length == 0) {
  1702. throw("Cannot set template sequence of length zero!\n");
  1703. }
  1704. }
  1705. this$.template.seq<-clone(value);
  1706. for (site in this$.template.seq$.sites){
  1707. site$.ancestral<-this;
  1708. }
  1709. },
  1710. private=FALSE,
  1711. protected=FALSE,
  1712. overwrite=FALSE,
  1713. conflict="warning",
  1714. validators=getOption("R.methodsS3:validators:setMethodS3")
  1715. );
  1716. ##
  1717. ## Method: getAcceptWin
  1718. ##
  1719. ###########################################################################/**
  1720. #
  1721. # @RdocMethod getAcceptWin
  1722. #
  1723. # @title "Get the size of the acceptance window"
  1724. #
  1725. # \description{
  1726. # @get "title"
  1727. #
  1728. # This parameter determines the number of sites neighbouring the position (from left and right) of the insertion considered when accepting/rejecting
  1729. # a proposed insertion. The "insertion.tolerance" parameter is retrived from sites falling in the window specified by this parameter.
  1730. # The default value is 1, so the two neighbouring sites are considered by default.
  1731. #
  1732. # }
  1733. #
  1734. # @synopsis
  1735. #
  1736. # \arguments{
  1737. # \item{this}{A GeneralInsertor object.}
  1738. # \item{...}{Not used.}
  1739. # }
  1740. #
  1741. # \value{
  1742. # A numeric vector of length one.
  1743. # }
  1744. #
  1745. # \examples{
  1746. # # create a GeneralInsertor object
  1747. # i<-GeneralInsertor(rate=0.5);
  1748. # # get acceptance window size
  1749. # getAcceptWin(i)
  1750. # # get acceptance window size via virtual field
  1751. # i$acceptWin
  1752. # # set acceptance window size
  1753. # setAcceptWin(i,2)
  1754. # # set acceptance window size via virtual field
  1755. # i$acceptWin<-3
  1756. # i$acceptWin
  1757. # }
  1758. #
  1759. # @author
  1760. #
  1761. # \seealso{
  1762. # @seeclass
  1763. # }
  1764. #
  1765. #*/###########################################################################
  1766. setMethodS3(
  1767. "getAcceptWin",
  1768. class="GeneralInsertor",
  1769. function(
  1770. this,
  1771. ...
  1772. ){
  1773. this$.accept.win;
  1774. },
  1775. private=FALSE,
  1776. protected=FALSE,
  1777. overwrite=FALSE,
  1778. conflict="warning",
  1779. validators=getOption("R.methodsS3:validators:setMethodS3")
  1780. );
  1781. ##
  1782. ## Method: setAcceptWin
  1783. ##
  1784. ###########################################################################/**
  1785. #
  1786. # @RdocMethod setAcceptWin
  1787. #
  1788. # @title "Set the size of the acceptance window"
  1789. #
  1790. # \description{
  1791. # @get "title"
  1792. #
  1793. # This parameter determines the number of sites neighbouring the position (from left and right) of the insertion considered when accepting/rejecting
  1794. # a proposed insertion. The "insertion.tolerance" parameter is retrived from sites falling in the window specified by this parameter.
  1795. # The default value is 1, so the two neighbouring sites are considered by default.
  1796. #
  1797. # }
  1798. #
  1799. # @synopsis
  1800. #
  1801. # \arguments{
  1802. # \item{this}{A GeneralInsertor object.}
  1803. # \item{value}{An integer vector of length one.}
  1804. # \item{...}{Not used.}
  1805. # }
  1806. #
  1807. # \value{
  1808. # The new value.
  1809. # }
  1810. #
  1811. # \examples{
  1812. # # create a GeneralInsertor object
  1813. # i<-GeneralInsertor(rate=0.5);
  1814. # # get acceptance window size
  1815. # getAcceptWin(i)
  1816. # # get acceptance window size via virtual field
  1817. # i$acceptWin
  1818. # # set acceptance window size
  1819. # setAcceptWin(i,2)
  1820. # # set acceptance window size via virtual field
  1821. # i$acceptWin<-3
  1822. # i$acceptWin
  1823. # }
  1824. #
  1825. # @author
  1826. #
  1827. # \seealso{
  1828. # @seeclass
  1829. # }
  1830. #
  1831. #*/###########################################################################
  1832. setMethodS3(
  1833. "setAcceptWin",
  1834. class="GeneralInsertor",
  1835. function(
  1836. this,
  1837. value,
  1838. ...
  1839. ){
  1840. .checkWriteProtection(this);
  1841. if(!exists(x="PSIM_FAST")){
  1842. if(missing(value)){
  1843. throw("No new value provided");
  1844. }
  1845. else if(!all(is.numeric(value)) | (length(value) != 1)){
  1846. throw("The new value must be a numeric vector of length one.");
  1847. }
  1848. }
  1849. this$.accept.win<-floor(value);
  1850. return(this$.accept.win);
  1851. },
  1852. private=FALSE,
  1853. protected=FALSE,
  1854. overwrite=FALSE,
  1855. conflict="warning",
  1856. validators=getOption("R.methodsS3:validators:setMethodS3")
  1857. );
  1858. ##
  1859. ## Method: getInsertHook
  1860. ##
  1861. ###########################################################################/**
  1862. #
  1863. # @RdocMethod getInsertHook
  1864. #
  1865. # @title "Get the insert hook function"
  1866. #
  1867. # \description{
  1868. # @get "title".
  1869. #
  1870. # The insert hook allows to make various modifications on the insert before performing the insertion.
  1871. #
  1872. # The insert hook function is called by the insertion event handler function. The insert hook takes the
  1873. # sequence generated by the \code{generateInsert} method throught the "seq" argument. The function
  1874. # must return a Sequnece object, which will be inserted in the target sequence.
  1875. # }
  1876. #
  1877. # @synopsis
  1878. #
  1879. # \arguments{
  1880. # \item{this}{A GeneralInsertor object.}
  1881. # \item{...}{Not used.}
  1882. # }
  1883. #
  1884. # \value{
  1885. # A function object.
  1886. # }
  1887. #
  1888. # \examples{
  1889. # # create a GeneralInsertor object
  1890. # i<-GeneralInsertor(
  1891. # rate=0.5,
  1892. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1893. # template.seq=NucleotideSequence(length=5,processes=list(list(JC69())))
  1894. # )
  1895. # # set a dummy insert hook
  1896. # setInsertHook(i,function(seq){return(seq)})
  1897. # # set a new insert hook via virtual field
  1898. # i$insertHook<-function(seq){
  1899. # seq$processes<-list(list(GTR())) # replace the subsitution process
  1900. # return(seq)
  1901. # }
  1902. # # get the insert hook via virtual field
  1903. # i$insertHook
  1904. # # get the insert hook
  1905. # getInsertHook(i)
  1906. # }
  1907. #
  1908. # @author
  1909. #
  1910. # \seealso{
  1911. # @seeclass
  1912. # }
  1913. #
  1914. #*/###########################################################################
  1915. setMethodS3(
  1916. "getInsertHook",
  1917. class="GeneralInsertor",
  1918. function(
  1919. this,
  1920. ...
  1921. ){
  1922. this$.insert.hook;
  1923. },
  1924. private=FALSE,
  1925. protected=FALSE,
  1926. overwrite=FALSE,
  1927. conflict="warning",
  1928. validators=getOption("R.methodsS3:validators:setMethodS3")
  1929. );
  1930. ##
  1931. ## Method: setInsertHook
  1932. ##
  1933. ###########################################################################/**
  1934. #
  1935. # @RdocMethod setInsertHook
  1936. #
  1937. # @title "Set the insert hook function"
  1938. #
  1939. # \description{
  1940. # @get "title".
  1941. #
  1942. # The insert hook allows to make various modifications on the insert before performing the insertion.
  1943. # The function must have the following arguments: seq (the sequence object to insert), target.seq (the target Sequence object),
  1944. # event.pos (the position of the site which generated the insertion event), insert.pos (the position of the insertion).
  1945. #
  1946. # The insert hook function is called by the insertion event handler function. The insert hook takes the
  1947. # sequence generated by the \code{generateInsert} method throught the "seq" argument. The function
  1948. # must return a Sequnece object, which will be inserted in the target sequence.
  1949. # }
  1950. #
  1951. # @synopsis
  1952. #
  1953. # \arguments{
  1954. # \item{this}{A GeneralInsertor object.}
  1955. # \item{value}{A function object.}
  1956. # \item{...}{Not used.}
  1957. # }
  1958. #
  1959. # \value{
  1960. # The function object.
  1961. # }
  1962. #
  1963. # \examples{
  1964. # # create a GeneralInsertor object
  1965. # i<-GeneralInsertor(
  1966. # rate=0.5,
  1967. # propose.by=function(process){sample(c(5:10),1)}, # inserts between 5 and 10
  1968. # template.seq=NucleotideSequence(length=5,processes=list(list(JC69())))
  1969. # )
  1970. # # set a dummy insert hook
  1971. # setInsertHook(i,function(seq){return(seq)})
  1972. # # set a new insert hook via virtual field
  1973. # i$insertHook<-function(seq){
  1974. # seq$processes<-list(list(GTR())) # replace the subsitution process
  1975. # return(seq)
  1976. # }
  1977. # # get the insert hook via virtual field
  1978. # i$insertHook
  1979. # # get the insert hook
  1980. # getInsertHook(i)
  1981. # }
  1982. #
  1983. # @author
  1984. #
  1985. # \seealso{
  1986. # @seeclass
  1987. # }
  1988. #
  1989. #*/###########################################################################
  1990. setMethodS3(
  1991. "setInsertHook",
  1992. class="GeneralInsertor",
  1993. function(
  1994. this,
  1995. value,
  1996. ...
  1997. ){
  1998. .checkWriteProtection(this);
  1999. if(!is.Sequence(this$.template.seq)){
  2000. throw("Cannot set insert hook because the template sequence is not defined!\n");
  2001. }
  2002. if(missing(value)) {
  2003. throw("No new value provided!\n");
  2004. }
  2005. else if(!is.function(value)){
  2006. throw("The insert hook must be a function.!\n");
  2007. }
  2008. else if( length(intersect(names(formals(value)), "seq")) == 0 ){
  2009. throw("The insert hook function must have a an argument named \"seq\"");
  2010. }
  2011. else if(!is.Sequence(value(generateInsert(this,length=1)))){
  2012. throw("The insert hook function must return a Sequence object!\n");
  2013. } else {
  2014. this$.insert.hook<-value;
  2015. }
  2016. return(this$.insert.hook);
  2017. },
  2018. private=FALSE,
  2019. protected=FALSE,
  2020. overwrite=FALSE,
  2021. conflict="warning",
  2022. validators=getOption("R.methodsS3:validators:setMethodS3")
  2023. );
  2024. ##
  2025. ## Method: summary
  2026. ##
  2027. ###########################################################################/**
  2028. #
  2029. # @RdocMethod summary
  2030. #
  2031. # @title "Summarize the properties of an object"
  2032. #
  2033. # \description{
  2034. # @get "title".
  2035. # }
  2036. #
  2037. # @synopsis
  2038. #
  2039. # \arguments{
  2040. # \item{object}{An object}
  2041. # \item{...}{Not used.}
  2042. # }
  2043. #
  2044. # \value{
  2045. # Returns a PSRootSummary object.
  2046. # }
  2047. #
  2048. # \examples{
  2049. #
  2050. # # create an object
  2051. # a<-GeneralInsertor(rate=1)
  2052. # # get a summary
  2053. # summary(a)
  2054. # }
  2055. #
  2056. # @author
  2057. #
  2058. # \seealso{
  2059. # @seeclass
  2060. # }
  2061. #
  2062. #*/###########################################################################
  2063. setMethodS3(
  2064. "summary",
  2065. class="GeneralInsertor",
  2066. function(
  2067. object,
  2068. ...
  2069. ){
  2070. .addSummaryNameId(object);
  2071. object$.summary$"Accept window size"<-object$.accept.win;
  2072. NextMethod();
  2073. },
  2074. private=FALSE,
  2075. protected=FALSE,
  2076. overwrite=FALSE,
  2077. conflict="warning",
  2078. validators=getOption("R.methodsS3:validators:setMethodS3")
  2079. );
  2080. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  2081. ###########################################################################
  2082. # Class:GeneralDeletor
  2083. ##########################################################################/**
  2084. #
  2085. # @RdocClass GeneralDeletor
  2086. #
  2087. # @title "The GeneralDeletor class"
  2088. #
  2089. # \description{
  2090. # This is the class implementing a process generating deletion events.
  2091. # The rates of the deletion events are calculated as the product of the general rate
  2092. # of the process and the "rate.multiplier" site-process-specific parameter.
  2093. #
  2094. # The simulation code calls the \code{Perform} method on the selected
  2095. # deletion event objects, which call their handler function to perform the deletion.
  2096. #
  2097. # The deletion lengths are proposed by the function stored in the \code{proposeBy} virtual field.
  2098. # The function must have the following arguments: process (the process object), sequence (the target sequence),
  2099. # position (the position of the site which generated the event).
  2100. #
  2101. # The deletion randomly affects the sites from the left or from the right of the target position (but never both).
  2102. # Positions which are out of range are discarded.
  2103. #
  2104. # The proposed deletions are accepted or rejected by the function stored in the \code{acceptBy} virtual field.
  2105. # The function must have the following arguments: process (the deletion prcoess), sequence (the target sequence), range (a vector of positions
  2106. # affected by the deletion).
  2107. #
  2108. # The probability of accepting a deletion is calculated as the product of the "deletion.tolerance" site-process-specific
  2109. # parameters from the sites affected by the deletion event.
  2110. #
  2111. # @classhierarchy
  2112. # }
  2113. #
  2114. # @synopsis
  2115. #
  2116. # \arguments{
  2117. # \item{name}{The name of the object.}
  2118. # \item{rate}{The general rate of the object.}
  2119. # \item{propose.by}{A function used to propose events.}
  2120. # \item{accept.by}{A function used to accept/reject events.}
  2121. # \item{...}{Additional arguments.}
  2122. # }
  2123. #
  2124. # \section{Fields and Methods}{
  2125. # @allmethods
  2126. # }
  2127. #
  2128. # \examples{
  2129. # # create a GeneralDeletor object
  2130. # # proposed deletion length: 4, always accept
  2131. # d<-GeneralDeletor(
  2132. # name = "DEL",
  2133. # rate = 1,
  2134. # propose.by=function(process, sequence, position){ 4 },
  2135. # accept.by=function(process, sequence, range){ TRUE }
  2136. # )
  2137. # d
  2138. # # check if object inherits from GeneralDeletor
  2139. # is.GeneralDeletor(d)
  2140. # # get object summary
  2141. # summary(d)
  2142. # # set/get name
  2143. # d$name<-"Del Bosque"
  2144. # d$name
  2145. # # set/get rate
  2146. # d$rate<-0.5
  2147. # d$rate
  2148. # # set/get proposeBy
  2149. # # propose deletion lengths between 3 and 6
  2150. # d$proposeBy<-function(process, sequence, position){ sample(3:6,1) }
  2151. # d$proposeBy
  2152. # # set/get acceptBy
  2153. # # reject half of the events
  2154. # d$acceptBy<-function(process, sequence, range){ sample(c(TRUE, FALSE), 1)}
  2155. # d$acceptBy
  2156. # # create a sequence object, attach process
  2157. # s<-NucleotideSequence(string="AATTGGCCCCGGTTAA", processes=list(list(d)))
  2158. # # set the rate multiplier
  2159. # setRateMultipliers(s,d,2)
  2160. # # get the list of active events at site 6
  2161. # events<-getEventsAtSite(d,s$sites[[6]])
  2162. # events;
  2163. # # print sequence
  2164. # s
  2165. # # set the position for the event object
  2166. # e<-events[[1]];
  2167. # e$.position<-6;
  2168. # # perform the deletion event
  2169. # Perform(e)
  2170. # # check the results
  2171. # s
  2172. # }
  2173. #
  2174. # @author
  2175. #
  2176. # \seealso{
  2177. # GeneralInDel DiscreteDeletor ContinuousDeletor FastFieldDeletor
  2178. # }
  2179. #
  2180. #*/###########################################################################
  2181. setConstructorS3(
  2182. "GeneralDeletor",
  2183. function(
  2184. name="Anonymous",
  2185. rate=NA,
  2186. propose.by=NA,
  2187. accept.by=NA,
  2188. ...
  2189. ) {
  2190. this<-GeneralInDel(
  2191. rate=rate,
  2192. propose.by=propose.by,
  2193. accept.by=accept.by
  2194. );
  2195. this<-extend(
  2196. this,
  2197. "GeneralDeletor",
  2198. .handler.template=NA,
  2199. .is.general.deletor=TRUE
  2200. );
  2201. # Using virtual field to clear Id cache:
  2202. this$name<-name;
  2203. # Adding insertion tolerance parameter.
  2204. .addSiteSpecificParameter(
  2205. this,
  2206. id="deletion.tolerance",
  2207. name="Deletion tolerance parameter",
  2208. value=as.double(1), # Accept all by default
  2209. type="numeric"
  2210. );
  2211. this$acceptBy<-function(process=NA,sequence=NA,range=NA){
  2212. accept.prob<-c();
  2213. for(site in sequence$.sites[range]){
  2214. # Reject if the range contains a site which is not attached to
  2215. # the process:
  2216. if(!isAttached(site, process)){
  2217. return(FALSE);
  2218. }
  2219. accept.prob<-c(accept.prob, getParameterAtSite(process, site, "deletion.tolerance")$value);
  2220. }
  2221. # Calculate the product of the per-site
  2222. # acceptance probabilities.
  2223. accept.prob<-prod(as.numeric(accept.prob));
  2224. # Accept/reject:
  2225. return( sample(c(TRUE,FALSE),replace=FALSE,prob=c(accept.prob,(1-accept.prob)),size=1) );
  2226. }
  2227. this$.handler.template<-function(event=NA) {
  2228. if(!is.na(event)){
  2229. # Using temporary varibales for clarity:
  2230. position<-event$.position;
  2231. process<-event$.process;
  2232. sequence<-event$.site$.sequence;
  2233. details<-list();
  2234. details$type<-"deletion";
  2235. details$accepted<-FALSE;
  2236. # Propose a sequence length:
  2237. length<-process$.propose.by(process=process,seq=sequence, pos=position);
  2238. # Propose the direction:
  2239. direction<-sample(c("LEFT","RIGHT"),replace=FALSE,size=1);
  2240. # Calculate the sites to delete:
  2241. range<-numeric();
  2242. if(direction == "RIGHT") {
  2243. range<-position:(position+length-1);
  2244. } else if(direction == "LEFT") {
  2245. range<-(position-length+1):position;
  2246. } else {
  2247. throw("You should never see this message!\n");
  2248. }
  2249. # Discard potential negative values and values larger than the sequence length:
  2250. range<-range[ range > 0 & range <= sequence$.length];
  2251. details$range<-c(min(range),max(range));
  2252. # Perform the deletion if it is accepted:
  2253. if (process$.accept.by(process=process,sequence=sequence,range=range) == TRUE) {
  2254. details$accepted<-TRUE;
  2255. deleteSubSequence(sequence,range);
  2256. }
  2257. # Return event details:
  2258. return(details);
  2259. }
  2260. }
  2261. return(this);
  2262. },
  2263. enforceRCC=TRUE
  2264. );
  2265. ##
  2266. ## Method: is.GeneralDeletor
  2267. ##
  2268. ###########################################################################/**
  2269. #
  2270. # @RdocDefault is.GeneralDeletor
  2271. #
  2272. # @title "Check whether an object inherits from GeneralDeletor"
  2273. #
  2274. # \description{
  2275. # @get "title".
  2276. # }
  2277. #
  2278. # @synopsis
  2279. #
  2280. # \arguments{
  2281. # \item{this}{An object.}
  2282. # \item{...}{Not used.}
  2283. #
  2284. # }
  2285. #
  2286. # \value{
  2287. # TRUE or FALSE.
  2288. # }
  2289. #
  2290. # \examples{
  2291. # # create some objects
  2292. # d<-GeneralDeletor()
  2293. # i<-GeneralInsertor()
  2294. # # check if they inherit from GeneralDeletor
  2295. # is.GeneralDeletor(d)
  2296. # is.GeneralDeletor(i)
  2297. # }
  2298. #
  2299. #
  2300. # @author
  2301. #
  2302. #*/###########################################################################
  2303. setMethodS3(
  2304. "is.GeneralDeletor",
  2305. class="default",
  2306. function(
  2307. this,
  2308. ...
  2309. ){
  2310. if(!is.PSRoot(this)) {return(FALSE)}
  2311. if(!is.null(this$.is.general.deletor)){return(TRUE)}
  2312. if ( inherits(this, "GeneralDeletor")) {
  2313. this$.is.general.deletor<-TRUE;
  2314. return(TRUE);
  2315. } else {
  2316. return(FALSE)
  2317. }
  2318. },
  2319. private=FALSE,
  2320. protected=FALSE,
  2321. overwrite=FALSE,
  2322. conflict="warning",
  2323. validators=getOption("R.methodsS3:validators:setMethodS3")
  2324. );
  2325. ##
  2326. ## Method: checkConsistency
  2327. ##
  2328. ###########################################################################/**
  2329. #
  2330. # @RdocMethod checkConsistency
  2331. #
  2332. # @title "Check object consistency"
  2333. #
  2334. # \description{
  2335. # @get "title".
  2336. # }
  2337. #
  2338. # @synopsis
  2339. #
  2340. # \arguments{
  2341. # \item{this}{An object.}
  2342. # \item{...}{Not used.}
  2343. # }
  2344. #
  2345. #
  2346. # \value{
  2347. # Returns an invisible TRUE if no inconsistencies found in the object, throws
  2348. # an error otherwise.
  2349. # }
  2350. #
  2351. # @author
  2352. #
  2353. # \seealso{
  2354. # @seeclass
  2355. # }
  2356. #
  2357. #*/###########################################################################
  2358. setMethodS3(
  2359. "checkConsistency",
  2360. class="GeneralDeletor",
  2361. function(
  2362. this,
  2363. ...
  2364. ){
  2365. NextMethod();
  2366. },
  2367. private=FALSE,
  2368. protected=FALSE,
  2369. overwrite=FALSE,
  2370. conflict="warning",
  2371. validators=getOption("R.methodsS3:validators:setMethodS3")
  2372. );
  2373. ##
  2374. ## Method: getEventsAtSite
  2375. ##
  2376. ###########################################################################/**
  2377. #
  2378. # @RdocMethod getEventsAtSite
  2379. #
  2380. # @title "Title"
  2381. #
  2382. # \description{
  2383. # @get "title".
  2384. #
  2385. # This method generates a list containing a single deletion event object. The rate
  2386. # of the event is calculated as the product of the general rate of the process and the
  2387. # "rate.multiplier" site-process specific parameter. An empty list is
  2388. # returned if the rate is zero or NA.
  2389. # }
  2390. #
  2391. # @synopsis
  2392. #
  2393. # \arguments{
  2394. # \item{this}{A GeneralDeletor object.}
  2395. # \item{target.site}{The target Site object.}
  2396. # \item{...}{Not used.}
  2397. # }
  2398. #
  2399. # \value{
  2400. # A list of event objects.
  2401. # }
  2402. #
  2403. # \examples{
  2404. # # create the Sequence object
  2405. # s<-NucleotideSequence(string="ATGCCCGGCGGATTTATTA");
  2406. # # create a GeneralDeletor object
  2407. # # proposed deletion length: 4, always accept
  2408. # d<-GeneralDeletor(
  2409. # name = "Del Bosque",
  2410. # rate = 0.5,
  2411. # propose.by=function(process, sequence, position){ 4 },
  2412. # accept.by=function(process, sequence, range){ TRUE }
  2413. # )
  2414. # # attach process to site
  2415. # attachProcess(s,d);
  2416. # # set the rate multiplier
  2417. # setRateMultipliers(s,d,2)
  2418. # # get the list of active events at site 6
  2419. # events<-getEventsAtSite(d,s$sites[[6]])
  2420. # events;
  2421. # # print sequence
  2422. # s
  2423. # # set the position for the event object
  2424. # e<-events[[1]];
  2425. # e$.position<-6;
  2426. # # perform the deletion event
  2427. # Perform(e)
  2428. # # check the results
  2429. # s
  2430. # }
  2431. #
  2432. # @author
  2433. #
  2434. # \seealso{
  2435. # @seeclass
  2436. # }
  2437. #
  2438. #*/###########################################################################
  2439. setMethodS3(
  2440. "getEventsAtSite",
  2441. class="GeneralDeletor",
  2442. function(
  2443. this,
  2444. target.site,
  2445. ...
  2446. ){
  2447. if(!exists(x="PSIM_FAST")){
  2448. if(missing(target.site)) {
  2449. throw("No target site provided!\n");
  2450. }
  2451. if(!is.Site(target.site)) {
  2452. throw("Target site invalid!\n");
  2453. }
  2454. else if(!is.function(this$.propose.by)) {
  2455. throw("proposeBy is not set, cannot propose deletion!\n");
  2456. }
  2457. else if (!is.function(this$.accept.by)){
  2458. throw("acceptBy is not set, cannot generate deletion event deletion!\n");
  2459. }
  2460. # Complain if sequence has a zero length:
  2461. if(target.site$.sequence$.length == 0) {
  2462. throw("Sequence has zero length so there is nothing to delete! How did you get here anyway?\n");
  2463. }
  2464. }
  2465. # Clone the event template object:
  2466. deletion.event<-clone(this$.event.template);
  2467. # Set the target position passed in a temporary field:
  2468. deletion.event$.position<-target.site$.position;
  2469. # Set the target site:
  2470. deletion.event$.site<-target.site;
  2471. # Set event name:
  2472. deletion.event$.name<-"Deletion";
  2473. # Set the genrator process:
  2474. deletion.event$.process<-this;
  2475. # Event rate is the product of the general rate and the
  2476. # site specific rate multiplier:
  2477. rate.multiplier<-target.site$.processes[[this$.id]]$site.params[["rate.multiplier"]]$value;
  2478. deletion.event$.rate<-(this$.rate * rate.multiplier);
  2479. # Set the handler for the deletion event:
  2480. deletion.event$.handler<-this$.handler.template;
  2481. # Return the event object in a list:
  2482. list(deletion.event);
  2483. },
  2484. private=FALSE,
  2485. protected=FALSE,
  2486. overwrite=FALSE,
  2487. conflict="warning",
  2488. validators=getOption("R.methodsS3:validators:setMethodS3")
  2489. );
  2490. ##
  2491. ## Method: summary
  2492. ##
  2493. ###########################################################################/**
  2494. #
  2495. # @RdocMethod summary
  2496. #
  2497. # @title "Summarize the properties of an object"
  2498. #
  2499. # \description{
  2500. # @get "title".
  2501. # }
  2502. #
  2503. # @synopsis
  2504. #
  2505. # \arguments{
  2506. # \item{object}{An object}
  2507. # \item{...}{Not used.}
  2508. # }
  2509. #
  2510. # \value{
  2511. # Returns a PSRootSummary object.
  2512. # }
  2513. #
  2514. # \examples{
  2515. #
  2516. # # create an object
  2517. # a<-GeneralDeletor(rate=1,name="Del Bosque")
  2518. # # get a summary
  2519. # summary(a)
  2520. # }
  2521. #
  2522. # @author
  2523. #
  2524. # \seealso{
  2525. # @seeclass
  2526. # }
  2527. #
  2528. #*/###########################################################################
  2529. setMethodS3(
  2530. "summary",
  2531. class="GeneralDeletor",
  2532. function(
  2533. object,
  2534. ...
  2535. ){
  2536. .addSummaryNameId(object);
  2537. NextMethod();
  2538. },
  2539. private=FALSE,
  2540. protected=FALSE,
  2541. overwrite=FALSE,
  2542. conflict="warning",
  2543. validators=getOption("R.methodsS3:validators:setMethodS3")
  2544. );