Core

Core utilities and classes for OligoSeeker

Type Definitions

DNA Utilities


source

DNAUtils

 DNAUtils ()

Utility class for DNA sequence operations.

Oligo Regular Expression Matching


source

OligoRegex

 OligoRegex (oligo:str)

Compiles and manages regex patterns for oligo searching.

# Example oligo design for targeting position 42 in a gene
oligo_template = "GAACGTTATCCGCGTNNNACGTTCGAAGCTGGT"
#                 ^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^
#                 5' targeting     |   3' targeting
#                                 codon

# Create the regex pattern for finding this oligo in sequencing data
oligo_regex = OligoRegex(oligo_template)

# Example sequencing read pairs
read1 = "ATCGAACGTTATCCGCGTATGACGTTCGAAGCTGGTCG"
#                          ^^^
#                          codon = ATG (Met)
read2 = "CGATCGGTTCGAACGTCTTCACAGCATTG"

# Find the codon
found_codon = oligo_regex.find_codon(read1, read2)
print(f"Found codon: {found_codon}")  # Should print "ATG"

# Translate to amino acid
amino_acid = str(Seq(found_codon).translate())
print(f"Amino acid: {amino_acid}")
Found codon: ATG
Amino acid: M
def test_oligo_regex():
    """Test the OligoRegex class for finding codons in reads."""
    
    # Test 1: Basic functionality with forward match
    oligo = "GAACNNNCAT"
    regex = OligoRegex(oligo)
    read1 = "ACGTGAACATGCATTGC"  # Contains GAACATGCAT with ATG as the codon
    read2 = "GCAACTGTAGCGTACGT"  # No match
    
    codon = regex.find_codon(read1, read2)
    print(f"Test 1: Forward match - Oligo: {oligo}, Codon found: {codon}")
    expected = "ATG"
    if codon == expected:
        print(f"✓ Correctly found codon {expected} in forward direction")
    else:
        print(f"✗ Expected {expected}, got {codon}")
    
    # Test 2: Match in second read
    oligo = "GAACNNNCAT"
    regex = OligoRegex(oligo)
    read1 = "ACGTCGATCGATCG"  # No match
    read2 = "ACGTGAACCGGCATCG"  # Contains GAACCGGCAT with CGG as the codon
    
    codon = regex.find_codon(read1, read2)
    print(f"Test 2: Match in second read - Oligo: {oligo}, Codon found: {codon}")
    expected = "CGG"
    if codon == expected:
        print(f"✓ Correctly found codon {expected} in second read")
    else:
        print(f"✗ Expected {expected}, got {codon}")
    
    # Test 3: Reverse complement match
    oligo = "GAACNNNCAT"
    regex = OligoRegex(oligo)
    rev_comp = DNAUtils.reverse_complement(oligo.replace("NNN", "GTC"))  # ATGGTCGTTC
    read1 = "ACGTATGGTCGTTCGCA"  # Contains reverse complement with GTC as codon
    read2 = "GCAACTGTAGCGTACGT"  # No match
    
    codon = regex.find_codon(read1, read2)
    print(f"Test 3: Reverse complement match - Oligo: {oligo}, Codon found: {codon}")
    expected = "GAC"  # Reverse complement of GTC
    if codon == expected:
        print(f"✓ Correctly found reverse complement codon {expected}")
    else:
        print(f"✗ Expected {expected}, got {codon}")
        
    # Test 5: No match
    oligo = "GAACNNNCAT"
    regex = OligoRegex(oligo)
    read1 = "ACGTCGATCGATCG"  # No match
    read2 = "GCAACTGTAGCGTACGT"  # No match
    
    codon = regex.find_codon(read1, read2)
    print(f"Test 5: No match - Oligo: {oligo}, Result: {codon}")
    if codon == "none":
        print("✓ Correctly returned 'none' for no match")
    else:
        print(f"✗ Expected 'none', got {codon}")

# Run the tests
test_oligo_regex()
Test 1: Forward match - Oligo: GAACNNNCAT, Codon found: ATG
✓ Correctly found codon ATG in forward direction
Test 2: Match in second read - Oligo: GAACNNNCAT, Codon found: CGG
✓ Correctly found codon CGG in second read
Test 3: Reverse complement match - Oligo: GAACNNNCAT, Codon found: GAC
✓ Correctly found reverse complement codon GAC
Test 5: No match - Oligo: GAACNNNCAT, Result: none
✓ Correctly returned 'none' for no match

Oligo Loading and Validation


source

OligoLoader

 OligoLoader ()

Loads and validates oligo sequences from different sources.

def test_oligo_loader_single_oligo_validation():
    """Test OligoLoader validation with a single oligo per test."""
   
    # Test 1: Valid oligo with one NNN pattern
    try:
        OligoLoader.validate_oligos(["GCGGATTACATTNNNAAATAACATCGT"])
        print("✓ Valid oligo with one NNN passed validation")
    except ValueError as e:
        print(f"✗ Valid oligo failed validation: {str(e)}")
    
    # Test 2: Oligo with no NNN pattern
    try:
        OligoLoader.validate_oligos(["GCGGATTACATTGCTAAATAACATCGT"])
        print("✗ Failed to detect missing NNN pattern")
    except ValueError as e:
        print(f"✓ Correctly detected missing NNN pattern: {str(e)}")
    
    # Test 3: Oligo with multiple NNN patterns
    try:
        OligoLoader.validate_oligos(["GCGGATTACATTNNNAAATAACNNNGT"])
        print("✗ Failed to detect multiple NNN patterns")
    except ValueError as e:
        print(f"✓ Correctly detected multiple NNN patterns: {str(e)}")
    
    # Test 4: Oligo with incorrect NNN format (NN instead of NNN)
    try:
        OligoLoader.validate_oligos(["TGACNNTAG"])
        print("✗ Failed to detect incorrect NN format")
    except ValueError as e:
        print(f"✓ Correctly detected incorrect NN format: {str(e)}")
    
    # Test 5: Oligo with invalid characters
    try:
        OligoLoader.validate_oligos(["TGACNNNXYZ"])
        print("✗ Failed to detect invalid characters")
    except ValueError as e:
        print(f"✓ Correctly detected invalid characters: {str(e)}")
    
    print("\nSingle oligo validation tests completed")

# Run the tests
test_oligo_loader_single_oligo_validation()
✓ Valid oligo with one NNN passed validation
✓ Correctly detected missing NNN pattern: GCGGATTACATTGCTAAATAACATCGT (contains 0 NNN patterns, must have exactly 1)
✓ Correctly detected multiple NNN patterns: GCGGATTACATTNNNAAATAACNNNGT (contains 2 NNN patterns, must have exactly 1)
✓ Correctly detected incorrect NN format: TGACNNTAG (contains 0 NNN patterns, must have exactly 1)
✓ Correctly detected invalid characters: Invalid characters found in oligos: X,Z,Y

Single oligo validation tests completed
#OligoLoader.validate_oligos(["GCGGATTACATTGCTAAATAACATCGT"])