#lang racket (require typed/racket) ;;; dds/rs ;;; Definitions for working with reaction systems. (require "utils.rkt") (provide ;; Structures reaction ;; Type names Species ReactionSystem ;; Functions enabled? list-enabled union-products apply-rs ht-str-triples->rs) ;;; ================= ;;; 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)] [products : (Setof Symbol)]) #:transparent) ;;; 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)))]) ;;; A reaction system is a dictionary mapping reaction names to ;;; reactions. (define-type ReactionSystem (HashTable Symbol reaction)) ;;; Returns the list of reaction names enabled on a given set. (: list-enabled (-> ReactionSystem (Setof Species) (Listof Symbol))) (define (list-enabled rs s) (for/list ([(name reaction) (in-hash rs)] #:when (enabled? reaction s)) name)) ;;; 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))))) ;;; 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))) ;;; ==================== ;;; 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))))