Commit ac3f2aaa authored by Pauline Pommeret's avatar Pauline Pommeret
Browse files

[trx] local_match function

parent 9a677c99
#!/usr/bin/env python2.7
# XXX
"""
XXX
* match local
* sur toute la séquence
* totex
"""
def local_match(string, TRX):
"""
Tries to match ``string`` (a 4 letter long string) with the patterns in
``TRX`` (TRX scale dictionary).
Returns the value associated to ``string`` or raises a NoMatch Exception
"""
potential = []
# Iteration on the keys of the TRX dictionary
for pattern in TRX.keys():
# Test whether the 2 central nucleotides are in the pattern
if re.search(string[1:3], pattern):
# Only one pattern 2 letters long is possible and it must be kept
# in case the tetranucleotides don't match the whole string
if len(pattern) == 2:
potential.append(pattern)
# Only one 4 letters long pattern is possible.
elif re.search(string[0], pattern[1:3]) and re.search(string[-1], pattern[7:9]):
potential.append(pattern)
if not potential:
# If the potential list is empty, clearly something gone wrong
raise NoMatch("No match found, please check your sequence and TRX scale.")
else:
# There might be up to 2 patterns in `potential`: a 2 letter long and a
# 4 letter long. The selected pattern is the longest one (specificity)
return TRX[max(potential)]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment