Respect explicit DNA strand direction during transcription
#8
by emberian - opened
Files changed (2)
1 unmodified line234567834 unmodified lines43444546474849505152535455565725 unmodified lines838485868788899091921 unmodified line"""organ_biology_v1 - molecular-biology sequence operations, exact (the standard genetic code, NCBI translation table 1):- DNA complement (A-T, C-G) and reverse complement (5'->3' of the other strand);- transcription of a DNA coding (sense) strand to mRNA (T -> U); of a template strand when it is called the template(reverse complement, then T -> U);- translation of mRNA / DNA coding sequence to protein from the first codon given, one-letter codes, "*" at a stop andtranslation ends there (stated); the amino acid a single codon encodes;- GC content = (G + C) / length x 100;34 unmodified linesreturn Nonereturn rdef _seq(self, t):# upper-case runs (spaces / hyphens only between codon triplets), or one lower-case token of 6+ bases -# dev: a case-insensitive, space-tolerant pattern read "codon CCG code" as CCGCm = re.search(r"\b([ACGTU]{3,}(?:[ \-][ACGTU]{3})*)\b", t) or re.search(r"\b([acgtu]{6,})\b", t)if not m:return Nones = clean(m.group(1))return s if len(s) >= 3 and re.fullmatch(r"[ACGTU]+", s) else Nonedef _answer(self, text):t = " ".join(str(text or "").split())low = t.lower()25 unmodified linesif re.search(r"\btranscri\w*\b|\bmrna\b(?!.*\btranslat)", low) and not re.search(r"\btranslat\w*\b|\bprotein\b|\bamino acids?\b", low):if "U" in s:return Noneif re.search(r"\btemplate\b|\bantisense\b|\bnon-?coding strand\b", low):m = "".join(COMP[b] for b in reversed(s)).replace("T", "U")return self._say("transcribe", "The mRNA is %s (the template strand read 3'->5', complementary bases, U for T; written 5'->3')." % m, m)m = s.replace("T", "U")return self._say("transcribe", "The mRNA is %s (same as the coding strand with U in place of T)." % m, m)# --- translationif re.search(r"\btranslat\w*\b|\bprotein\b|\bamino acid sequence\b|\bpeptide\b", low):1 unmodified line234567834 unmodified lines434445464748495051525354555657585960616263646566676869707172737425 unmodified lines1001011021031041051061071081091101111121131141 unmodified line"""organ_biology_v1 - molecular-biology sequence operations, exact (the standard genetic code, NCBI translation table 1):- DNA complement (A-T, C-G) and reverse complement (5'->3' of the other strand);- transcription of a DNA coding (sense) strand to mRNA (T -> U); of a template strand when it is called the template(complement, then T -> U, respecting explicit strand-end labels; unlabelled strands are assumed 5'->3');- translation of mRNA / DNA coding sequence to protein from the first codon given, one-letter codes, "*" at a stop andtranslation ends there (stated); the amino acid a single codon encodes;- GC content = (G + C) / length x 100;34 unmodified linesreturn Nonereturn rdef _seq_match(self, t):# upper-case runs (spaces / hyphens only between codon triplets), or one lower-case token of 6+ bases -# dev: a case-insensitive, space-tolerant pattern read "codon CCG code" as CCGCreturn re.search(r"\b([ACGTU]{3,}(?:[ \-][ACGTU]{3})*)\b", t) or re.search(r"\b([acgtu]{6,})\b", t)def _seq(self, t):m = self._seq_match(t)if not m:return Nones = clean(m.group(1))return s if len(s) >= 3 and re.fullmatch(r"[ACGTU]+", s) else Nonedef _strand_start(self, t):# Bind end labels to the same sequence that _seq selected, not to a# requested output direction elsewhere in the question.m = self._seq_match(t)left = re.search(r"([35])['′’]\s*[-–—]?\s*$", t[:m.start(1)])right = re.match(r"\s*[-–—]?\s*([35])['′’]", t[m.end(1):])if left and right and left.group(1) == right.group(1):return Noneif left:return int(left.group(1))if right:return 5 if right.group(1) == "3" else 3return 5def _answer(self, text):t = " ".join(str(text or "").split())low = t.lower()25 unmodified linesif re.search(r"\btranscri\w*\b|\bmrna\b(?!.*\btranslat)", low) and not re.search(r"\btranslat\w*\b|\bprotein\b|\bamino acids?\b", low):if "U" in s:return Nonestart = self._strand_start(t)if start is None:return self._bad("A DNA strand must have opposite 3' and 5' ends.")if re.search(r"\btemplate\b|\bantisense\b|\bnon-?coding strand\b", low):template = s if start == 3 else s[::-1]m = "".join(COMP[b] for b in template).replace("T", "U")return self._say("transcribe", "The mRNA is %s (the template strand read 3'->5', complementary bases, U for T; written 5'->3')." % m, m)coding = s if start == 5 else s[::-1]m = coding.replace("T", "U")return self._say("transcribe", "The mRNA is %s (same as the coding strand with U in place of T)." % m, m)# --- translationif re.search(r"\btranslat\w*\b|\bprotein\b|\bamino acid sequence\b|\bpeptide\b", low):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100"""Portable tests for the untrusted Python biology organ (no Engine or stores).
Run: python3 -B -m unittest discover -s tests/biology -vBIOLOGY_SOURCE optionally selects an unpatched organ for before/after checks.RNA is synthesized 5'->3' against a template read 3'->5':https://www.ncbi.nlm.nih.gov/books/NBK21121/"""import importlib.utilimport itertoolsimport jsonimport osfrom pathlib import Pathimport sysimport unittest
ROOT = Path(__file__).resolve().parents[2]ORGAN_DIR = ROOT / 'root/specialists/biology'SOURCE = Path(os.environ.get('BIOLOGY_SOURCE', ORGAN_DIR / 'organ_biology_v1.py'))sys.path.insert(0, str(ORGAN_DIR))try: spec = importlib.util.spec_from_file_location('tested_biology', SOURCE) biology = importlib.util.module_from_spec(spec) spec.loader.exec_module(biology) spec = importlib.util.spec_from_file_location('tested_biology_scorer', ORGAN_DIR / 'biology_scorer.py') scorer = importlib.util.module_from_spec(spec) spec.loader.exec_module(scorer)finally: sys.path.pop(0)
class TranscriptionDirectionTests(unittest.TestCase): def setUp(self): self.organ = biology.BiologyOrgan()
def value(self, question): reply = self.organ.answer(question) self.assertIsNotNone(reply) self.assertEqual(reply['status'], 'answered') self.assertEqual(reply['kind'], 'transcribe') return reply['value']
def test_template_written_three_to_five(self): self.assertEqual(self.value("Transcribe the DNA template strand 3'-TACGGA-5' into mRNA."), 'AUGCCU')
def test_same_template_written_five_to_three(self): self.assertEqual(self.value("Transcribe the DNA template strand 5'-AGGCAT-3' into mRNA."), 'AUGCCU')
def test_coding_written_three_to_five(self): self.assertEqual(self.value("Transcribe the DNA coding strand 3'-TCCGTA-5' into mRNA."), 'AUGCCU')
def test_coding_written_five_to_three(self): self.assertEqual(self.value("Transcribe the DNA coding strand 5'-ATGCCT-3' into mRNA."), 'AUGCCU')
def test_prime_and_separator_spellings(self): for prime in ("'", '′', '’'): for separator in ('-', '–', '—', ' '): with self.subTest(prime=prime, separator=separator): q = f'Transcribe DNA template 3{prime}{separator}TAC GGA{separator}5{prime} into mRNA.' self.assertEqual(self.value(q), 'AUGCCU')
def test_single_endpoint_labels(self): for strand in ("3'-TACGGA", "TACGGA-5'"): with self.subTest(strand=strand): self.assertEqual(self.value(f'Transcribe DNA template {strand} into mRNA.'), 'AUGCCU')
def test_conflicting_endpoint_labels_decline(self): for end in ('3', '5'): with self.subTest(end=end): r = self.organ.answer(f"Transcribe DNA template {end}'-TACGGA-{end}' into mRNA.") self.assertEqual(r['status'], 'withheld') self.assertIsNone(r['value'])
def test_output_direction_does_not_override_template_labels(self): self.assertEqual(self.value("Give 5' to 3' mRNA transcribed from DNA template 3'-TACGGA-5'."), 'AUGCCU') self.assertEqual(self.value("Transcribe DNA template TACGGA into mRNA written 5' to 3'."), 'UCCGUA')
def test_unlabelled_default_stays_five_to_three(self): self.assertEqual(self.value('Transcribe the DNA template strand AGGCAT into mRNA.'), 'AUGCCU') self.assertEqual(self.value('Transcribe the DNA coding strand ATGCCT into mRNA.'), 'AUGCCU')
def test_exhaustive_four_base_template_polarity(self): # An independent direct lookup reads the template in polymerase order. # Its reversed spelling describes the same physical strand. rna_for_base = {'A': 'U', 'C': 'G', 'G': 'C', 'T': 'A'} for bases in itertools.product('ACGT', repeat=4): template = ''.join(bases) expected = ''.join(rna_for_base[b] for b in bases) for strand in (f"3'-{template}-5'", f"5'-{template[::-1]}-3'"): with self.subTest(strand=strand): self.assertEqual(self.value(f'Transcribe DNA template {strand} into mRNA.'), expected)
def test_shipped_panel_with_own_scorer(self): for row in json.loads((ORGAN_DIR / 'biology_panel_v1.json').read_text()): with self.subTest(split=row['split'], question=row['q']): ok, _ = scorer.ok(row, self.organ.answer(row['q'])) self.assertTrue(ok)
if __name__ == '__main__': unittest.main()root/specialists/biology/organ_biology_v1.py+27-5
tests/biology/test_transcription_direction.pyAdded+100