2020-03-01 20:25:01 +01:00
|
|
|
#lang racket
|
|
|
|
|
|
|
|
(require typed/racket)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
|
|
|
;;; dds/rs
|
|
|
|
|
|
|
|
;;; Definitions for working with reaction systems.
|
|
|
|
|
2020-03-01 20:25:01 +01:00
|
|
|
(require "utils.rkt")
|
2020-03-01 20:20:16 +01:00
|
|
|
|
2020-03-01 19:05:28 +01:00
|
|
|
(provide
|
|
|
|
;; Structures
|
|
|
|
reaction
|
2020-03-01 19:07:16 +01:00
|
|
|
;; Type names
|
2020-03-01 19:19:58 +01:00
|
|
|
Species ReactionSystem
|
2020-03-01 19:05:28 +01:00
|
|
|
;; Functions
|
2020-03-01 20:26:16 +01:00
|
|
|
enabled? list-enabled union-products apply-rs ht-str-triples->rs
|
|
|
|
;; Syntax
|
|
|
|
unorg-rs)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
|
|
|
;;; =================
|
|
|
|
;;; Basic definitions
|
|
|
|
;;; =================
|
|
|
|
|
|
|
|
;;; A species is a symbol.
|
|
|
|
(define-type Species Symbol)
|
|
|
|
|
|
|
|
;;; A reaction is a triple of sets, giving the reactants, the
|
|
|
|
;;; inhibitors, and the products, respectively.
|
|
|
|
(struct reaction ([reactants : (Setof Symbol)]
|
|
|
|
[inhibitors : (Setof Symbol)]
|
2020-03-01 19:54:53 +01:00
|
|
|
[products : (Setof Symbol)])
|
|
|
|
#:transparent)
|
2020-03-01 19:05:28 +01:00
|
|
|
|
|
|
|
;;; A reaction is enabled on a set if all of its reactants are in the
|
|
|
|
;;; set and none of its inhibitors are.
|
|
|
|
(: enabled? (-> reaction (Setof Symbol) Boolean))
|
|
|
|
(define/match (enabled? r s)
|
|
|
|
[((reaction r i p) s)
|
|
|
|
(and (subset? r s) (set-empty? (set-intersect i s)))])
|
2020-03-01 19:19:58 +01:00
|
|
|
|
|
|
|
;;; A reaction system is a dictionary mapping reaction names to
|
|
|
|
;;; reactions.
|
|
|
|
(define-type ReactionSystem (HashTable Symbol reaction))
|
2020-03-01 19:20:24 +01:00
|
|
|
|
|
|
|
;;; Returns the list of reaction names enabled on a given set.
|
2020-03-01 19:24:32 +01:00
|
|
|
(: list-enabled (-> ReactionSystem (Setof Species) (Listof Symbol)))
|
2020-03-01 19:20:24 +01:00
|
|
|
(define (list-enabled rs s)
|
|
|
|
(for/list ([(name reaction) (in-hash rs)]
|
|
|
|
#:when (enabled? reaction s))
|
|
|
|
name))
|
2020-03-01 19:47:38 +01:00
|
|
|
|
|
|
|
;;; Returns the union of the product sets of the given reactions in a
|
|
|
|
;;; reaction system.
|
|
|
|
;;;
|
|
|
|
;;; This function can be seen as producing the result of the
|
|
|
|
;;; application of the given reactions to a set. Clearly, it does not
|
|
|
|
;;; check whether the reactions are actually enabled.
|
|
|
|
(: union-products (-> ReactionSystem (Listof Symbol) (Setof Species)))
|
|
|
|
(define (union-products rs as)
|
|
|
|
(apply set-union
|
|
|
|
((inst set Species)) ; set-union requires at least one argument
|
|
|
|
(for/list : (Listof (Setof Species))
|
|
|
|
([a as]) (reaction-products (hash-ref rs a)))))
|
2020-03-01 19:51:53 +01:00
|
|
|
|
|
|
|
;;; Applies a reaction system to a set.
|
|
|
|
(: apply-rs (-> ReactionSystem (Setof Species) (Setof Species)))
|
|
|
|
(define (apply-rs rs s)
|
|
|
|
(let ([as (list-enabled rs s)])
|
|
|
|
(union-products rs as)))
|
2020-03-01 20:20:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
;;; ====================
|
|
|
|
;;; Org-mode interaction
|
|
|
|
;;; ====================
|
|
|
|
|
|
|
|
;;; This section contains some useful primitives for interoperating
|
|
|
|
;;; with org-mode.
|
|
|
|
|
|
|
|
;;; Reads a list of species from a string.
|
|
|
|
(: read-symbol-list (-> String (Listof Species)))
|
|
|
|
(define (read-symbol-list str)
|
|
|
|
(cast (string->any (string-append "(" str ")")) (Listof Species)))
|
|
|
|
|
|
|
|
;;; Converts a triple of strings to a reaction.
|
|
|
|
(: str-triple->reaction (-> (List String String String) reaction))
|
|
|
|
(define/match (str-triple->reaction lst)
|
|
|
|
[((list str-reactants str-inhibitors str-products))
|
|
|
|
(reaction (list->set (read-symbol-list str-reactants))
|
|
|
|
(list->set (read-symbol-list str-inhibitors))
|
|
|
|
(list->set (read-symbol-list str-products)))])
|
|
|
|
|
|
|
|
;;; Converts a hash table mapping reaction names to triples of strings
|
|
|
|
;;; to a reaction system.
|
|
|
|
(: ht-str-triples->rs (-> (HashTable Symbol (List String String String)) ReactionSystem))
|
|
|
|
(define (ht-str-triples->rs ht)
|
|
|
|
(for/hash : ReactionSystem
|
|
|
|
([(a triple) (in-hash ht)])
|
|
|
|
(values a (str-triple->reaction triple))))
|
2020-03-01 20:26:16 +01:00
|
|
|
|
|
|
|
;;; Chains ht-str-triples->rs with unorg.
|
|
|
|
(define (unorg-rs str) (ht-str-triples->rs (unorg str)))
|