File size: 3,074 Bytes
e918a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
Data models for Pathway Analysis Environment.

Supports pipeline-style episodes (count matrix + sample metadata + gene sets)
with PyDESeq2 differential expression, Fisher ORA, overlap-aware summaries,
and HTML step traces.
"""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from openenv.core.env_server import Action, Observation, State
from pydantic import Field


class PathwayAction(Action):
    """
    Action for the Pathway Analysis environment.

    action_type:
        - ``inspect_dataset``: describe available samples and conditions.
        - ``understand_experiment_design``: **(1)** Summarize groups (conditions, sample counts);
          optionally **(2)** validate ``condition_a``/``condition_b`` as reference/alternate for
          DGE (does not run DESeq2). Valid pairs feed ``run_differential_expression`` when DE omits
          conditions. **(3)** Pathway steps follow DE.
        - ``run_differential_expression``: PyDESeq2 contrast (needs ``condition_a`` /
          ``condition_b`` when using count-matrix cases).
        - ``run_pathway_enrichment``: ORA on DE genes vs pathway gene sets.
        - ``compare_pathways``: contrast exclusive vs shared DE support between two
          pathways (``pathway_a``, ``pathway_b``).
        - ``submit_answer``: submit ``hypothesis`` pathway name and end episode.
    """

    action_type: str
    condition_a: Optional[str] = None
    condition_b: Optional[str] = None
    gene_list: Optional[List[str]] = None
    hypothesis: Optional[str] = None
    pathway_a: Optional[str] = None
    pathway_b: Optional[str] = None


class PathwayObservation(Observation):
    """Observation with optional rich DE / ORA structures (JSON-serializable)."""

    message: str = ""
    available_conditions: List[str] = Field(default_factory=list)
    top_genes: List[str] = Field(default_factory=list)
    top_pathways: List[str] = Field(default_factory=list)
    de_genes: List[Dict[str, Any]] = Field(default_factory=list)
    pathway_enrichment: List[Dict[str, Any]] = Field(default_factory=list)
    pathway_comparison: Optional[Dict[str, Any]] = None
    overlap_summary: Optional[Dict[str, Any]] = None
    statistical_ambiguity: Optional[Dict[str, Any]] = None
    trace_path: Optional[str] = None
    experiment_design: Optional[Dict[str, Any]] = None


class PathwayState(State):
    """Agent-visible episode state (ground truth is never included)."""

    conditions: List[str] = Field(default_factory=list)
    de_run: bool = False
    enrichment_run: bool = False
    is_done: bool = False
    pipeline_mode: bool = False
    strict_mode: bool = False
    legacy_mode: bool = False
    eval_mode: bool = True
    max_steps: int = 30
    design_understood: bool = False
    validated_reference: Optional[str] = None
    validated_alternate: Optional[str] = None