File size: 10,127 Bytes
a0d03b2 | 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 | """
Biological sequence task generation and pool management.
Generates synthetic tasks for evaluating biological language models.
"""
import os
import json
import logging
import random
import numpy as np
from typing import List, Dict, Optional, Any
from dataclasses import dataclass, field
from pathlib import Path
logger = logging.getLogger(__name__)
# Standard amino acids
AMINO_ACIDS = list("ACDEFGHIKLMNPQRSTVWY")
# Standard nucleotides
NUCLEOTIDES = list("ACGT")
RNA_NUCLEOTIDES = list("ACGU")
def generate_random_protein(length: int) -> str:
"""Generate a random protein sequence."""
return "".join(random.choices(AMINO_ACIDS, k=length))
def generate_random_dna(length: int) -> str:
"""Generate a random DNA sequence."""
return "".join(random.choices(NUCLEOTIDES, k=length))
def generate_random_rna(length: int) -> str:
"""Generate a random RNA sequence."""
return "".join(random.choices(RNA_NUCLEOTIDES, k=length))
def add_motif(sequence: str, motif: str, position: Optional[int] = None) -> str:
"""Insert a motif into a sequence at given position."""
if position is None:
position = random.randint(0, len(sequence) - len(motif))
return sequence[:position] + motif + sequence[position + len(motif):]
@dataclass
class BioTask:
"""A biological sequence evaluation task."""
task_id: str
task_type: str # "protein", "dna", "rna"
task_family: str # e.g., "motif_recognition", "structure_prediction", "function_prediction"
# Task content
prompt: str # Instruction for the model
context: Optional[str] = None # Additional context (e.g., partial sequence)
target: Optional[str] = None # Expected output
# Evaluation
evaluation_metric: str = "sequence_identity" # How to score responses
expected_answer: Optional[str] = None
# Metadata
difficulty: float = 0.5
generation_seed: Optional[int] = None
def to_dict(self) -> Dict[str, Any]:
return {
"task_id": self.task_id,
"task_type": self.task_type,
"task_family": self.task_family,
"prompt": self.prompt,
"context": self.context,
"target": self.target,
"evaluation_metric": self.evaluation_metric,
"expected_answer": self.expected_answer,
"difficulty": self.difficulty,
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "BioTask":
return cls(**data)
class BioTaskPool:
"""Manages a pool of biological evaluation tasks."""
# Known biological motifs for task generation
PROTEIN_MOTIFS = {
"nuclear_localization": "PKKKRKV",
"atp_binding": "GXGXXG",
"zinc_finger": "CCHH",
"helix_turn_helix": "LXXLL",
"transmembrane": "AVLIVF",
}
DNA_MOTIFS = {
"tata_box": "TATAAA",
"gc_rich": "GCGCGC",
"promoter": "CAAT",
"terminator": "AATAAA",
}
RNA_MOTIFS = {
"shine_dalgarno": "AGGAGGU",
"polya_signal": "AAUAAA",
"kozak": "GCCRCC",
}
def __init__(self, seed: int = 42):
self.rng = np.random.RandomState(seed)
self.tasks: List[BioTask] = []
self._initialize_base_tasks()
def _initialize_base_tasks(self) -> None:
"""Create initial set of basic tasks."""
# Protein tasks
for i, (name, motif) in enumerate(self.PROTEIN_MOTIFS.items()):
task = BioTask(
task_id=f"protein_motif_{name}_{i}",
task_type="protein",
task_family="motif_recognition",
prompt=f"Identify if this protein sequence contains a {name} motif: ",
evaluation_metric="contains_substring",
expected_answer=motif,
difficulty=0.3 + i * 0.1,
)
self.tasks.append(task)
# DNA tasks
for i, (name, motif) in enumerate(self.DNA_MOTIFS.items()):
task = BioTask(
task_id=f"dna_motif_{name}_{i}",
task_type="dna",
task_family="motif_recognition",
prompt=f"Identify if this DNA sequence contains a {name} motif: ",
evaluation_metric="contains_substring",
expected_answer=motif,
difficulty=0.3 + i * 0.1,
)
self.tasks.append(task)
# Sequence completion tasks
for seq_type, alphabet in [("protein", AMINO_ACIDS), ("dna", NUCLEOTIDES), ("rna", RNA_NUCLEOTIDES)]:
for length in [10, 20, 50]:
target = "".join(self.rng.choice(alphabet, size=length))
prefix = target[:length // 2]
task = BioTask(
task_id=f"{seq_type}_complete_len{length}_{hash(target) % 10000}",
task_type=seq_type,
task_family="sequence_completion",
prompt=f"Complete this {seq_type} sequence: {prefix}",
context=prefix,
target=target,
expected_answer=target,
evaluation_metric="sequence_similarity",
difficulty=min(length / 100.0, 0.9),
)
self.tasks.append(task)
logger.info(f"Initialized task pool with {len(self.tasks)} base tasks")
def get_tasks(self, n: int = 10, filter_type: Optional[str] = None) -> List[BioTask]:
"""Sample n tasks from the pool."""
available = self.tasks
if filter_type:
available = [t for t in available if t.task_type == filter_type]
if len(available) <= n:
return available
return self.rng.choice(available, size=n, replace=False).tolist()
def generate_new_tasks(
self,
archive: Any,
num_tasks: int = 5,
difficulty_weights: Optional[Dict[str, float]] = None,
) -> List[BioTask]:
"""Generate new tasks targeting weaknesses in the archive."""
new_tasks = []
# Analyze which sequence types need more challenge
seq_types = ["protein", "dna", "rna"]
for i in range(num_tasks):
seq_type = random.choice(seq_types)
# Generate harder tasks (longer sequences, more complex motifs)
if seq_type == "protein":
length = random.randint(50, 200)
sequence = generate_random_protein(length)
# Add a known motif at random position
motif_name = random.choice(list(self.PROTEIN_MOTIFS.keys()))
motif = self.PROTEIN_MOTIFS[motif_name]
pos = random.randint(0, length - len(motif))
sequence = sequence[:pos] + motif + sequence[pos + len(motif):]
task = BioTask(
task_id=f"protein_gen_{len(self.tasks) + i}_{hash(sequence) % 10000}",
task_type="protein",
task_family="motif_localization",
prompt=f"Find the position of the {motif_name} motif in this protein: {sequence}",
context=sequence,
target=str(pos),
expected_answer=str(pos),
evaluation_metric="exact_match",
difficulty=0.6 + random.random() * 0.3,
)
elif seq_type == "dna":
length = random.randint(100, 500)
sequence = generate_random_dna(length)
motif_name = random.choice(list(self.DNA_MOTIFS.keys()))
motif = self.DNA_MOTIFS[motif_name]
pos = random.randint(0, length - len(motif))
sequence = sequence[:pos] + motif + sequence[pos + len(motif):]
task = BioTask(
task_id=f"dna_gen_{len(self.tasks) + i}_{hash(sequence) % 10000}",
task_type="dna",
task_family="regulatory_element_detection",
prompt=f"Find the {motif_name} regulatory element in: {sequence}",
context=sequence,
target=str(pos),
expected_answer=str(pos),
evaluation_metric="exact_match",
difficulty=0.5 + random.random() * 0.3,
)
else: # rna
length = random.randint(50, 300)
sequence = generate_random_rna(length)
task = BioTask(
task_id=f"rna_gen_{len(self.tasks) + i}_{hash(sequence) % 10000}",
task_type="rna",
task_family="structure_prediction",
prompt=f"Predict the secondary structure of this RNA: {sequence}",
context=sequence,
evaluation_metric="rna_structure_similarity",
difficulty=0.7 + random.random() * 0.2,
)
new_tasks.append(task)
# Add to pool
self.tasks.extend(new_tasks)
logger.info(f"Generated {len(new_tasks)} new tasks. Pool size: {len(self.tasks)}")
return new_tasks
def save(self, path: str) -> None:
"""Save task pool to disk."""
data = [t.to_dict() for t in self.tasks]
with open(path, "w") as f:
json.dump(data, f, indent=2)
logger.info(f"Saved task pool to {path}")
def load(self, path: str) -> None:
"""Load task pool from disk."""
with open(path, "r") as f:
data = json.load(f)
self.tasks = [BioTask.from_dict(d) for d in data]
logger.info(f"Loaded task pool with {len(self.tasks)} tasks from {path}")
class ProteinTask(BioTask):
"""Protein-specific task."""
pass
class DNATask(BioTask):
"""DNA-specific task."""
pass
class RNATask(BioTask):
"""RNA-specific task."""
pass
|