Compiles and manages regex patterns for oligo searching.
# Example oligo design for targeting position 42 in a geneoligo_template ="GAACGTTATCCGCGTNNNACGTTCGAAGCTGGT"# ^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^# 5' targeting | 3' targeting# codon# Create the regex pattern for finding this oligo in sequencing dataoligo_regex = OligoRegex(oligo_template)# Example sequencing read pairsread1 ="ATCGAACGTTATCCGCGTATGACGTTCGAAGCTGGTCG"# ^^^# codon = ATG (Met)read2 ="CGATCGGTTCGAACGTCTTCACAGCATTG"# Find the codonfound_codon = oligo_regex.find_codon(read1, read2)print(f"Found codon: {found_codon}") # Should print "ATG"# Translate to amino acidamino_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 GTCif 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 teststest_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
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 patterntry: OligoLoader.validate_oligos(["GCGGATTACATTNNNAAATAACATCGT"])print("✓ Valid oligo with one NNN passed validation")exceptValueErroras e:print(f"✗ Valid oligo failed validation: {str(e)}")# Test 2: Oligo with no NNN patterntry: OligoLoader.validate_oligos(["GCGGATTACATTGCTAAATAACATCGT"])print("✗ Failed to detect missing NNN pattern")exceptValueErroras e:print(f"✓ Correctly detected missing NNN pattern: {str(e)}")# Test 3: Oligo with multiple NNN patternstry: OligoLoader.validate_oligos(["GCGGATTACATTNNNAAATAACNNNGT"])print("✗ Failed to detect multiple NNN patterns")exceptValueErroras 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")exceptValueErroras e:print(f"✓ Correctly detected incorrect NN format: {str(e)}")# Test 5: Oligo with invalid characterstry: OligoLoader.validate_oligos(["TGACNNNXYZ"])print("✗ Failed to detect invalid characters")exceptValueErroras e:print(f"✓ Correctly detected invalid characters: {str(e)}")print("\nSingle oligo validation tests completed")# Run the teststest_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