Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow for IUPAC codes in GetCanonicalMotif() #239

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions trtools/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def test_GetCanonicalMotif():
assert(utils.GetCanonicalMotif("TTGTT")=="AAAAC")
assert(utils.GetCanonicalMotif("")=="")
assert(utils.GetCanonicalMotif("cag")=="AGC")
assert(utils.GetCanonicalMotif("AARRG")=="AARRG")
assert(utils.GetCanonicalMotif("YARRG")=="ARRGY")
aryarm marked this conversation as resolved.
Show resolved Hide resolved

# GetCanonicalOneStrand
def test_GetCanonicalOneStrand():
Expand All @@ -126,13 +128,25 @@ def test_GetCanonicalOneStrand():
assert(utils.GetCanonicalOneStrand("TTGTT")=="GTTTT")
assert(utils.GetCanonicalOneStrand("")=="")
assert(utils.GetCanonicalOneStrand("at")=="AT")
# Additional tests with IUPAC codes
assert(utils.GetCanonicalOneStrand("RY")=="RY")
assert(utils.GetCanonicalOneStrand("YR")=="RY")
assert(utils.GetCanonicalOneStrand("SW")=="SW")
assert(utils.GetCanonicalOneStrand("WS")=="SW")
assert(utils.GetCanonicalOneStrand("KM")=="KM")
assert(utils.GetCanonicalOneStrand("MK")=="KM")

# ReverseComplement
def test_ReverseComplement():
assert(utils.ReverseComplement("CGAT")=="ATCG")
assert(utils.ReverseComplement("")=="")
assert(utils.ReverseComplement("CGNT")=="ANCG")
assert(utils.ReverseComplement("ccga")=="TCGG")
# additional tests with IUPAC codes
assert(utils.ReverseComplement("RYASWKM")=="KMWSTRY")
# also test the characters that don't change
assert(utils.ReverseComplement("BDHV")=="BDHV")
assert(utils.ReverseComplement("N")=="N")

# InferRepeatSequence
def test_InferRepeatSequence():
Expand Down
33 changes: 15 additions & 18 deletions trtools/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import trtools.utils.common as common # pragma: no cover

nucToNumber={"A":0,"C":1,"G":2,"T":3}

def LoadSingleReader(
vcf_loc: str,
Expand Down Expand Up @@ -387,9 +386,9 @@ def GetCanonicalMotif(repseq):
repseq_r = GetCanonicalOneStrand(ReverseComplement(repseq))
aryarm marked this conversation as resolved.
Show resolved Hide resolved
# choose first seq alphabetically
for i in range(len(repseq_f)):
if nucToNumber[repseq_f[i]] < nucToNumber[repseq_r[i]]:
if repseq_f[i] < repseq_r[i]:
return repseq_f
if nucToNumber[repseq_r[i]] < nucToNumber[repseq_f[i]]:
if repseq_r[i] < repseq_f[i]:
return repseq_r
return repseq_f

Expand Down Expand Up @@ -420,16 +419,16 @@ def GetCanonicalOneStrand(repseq):
for i in range(size):
newseq = repseq[size-i:]+repseq[0:size-i]
for j in range(size):
if nucToNumber[newseq[j]] < nucToNumber[canonical[j]]:
if newseq[j] < canonical[j]:
canonical = newseq
elif nucToNumber[newseq[j]] > nucToNumber[canonical[j]]:
elif newseq[j] > canonical[j]:
break
return canonical

def ReverseComplement(seq):
r"""Get reverse complement of a sequence.

Converts everything to uppsercase.
Converts everything to uppercase and handles IUPAC codes.

Parameters
----------
Expand All @@ -445,21 +444,19 @@ def ReverseComplement(seq):
--------
>>> ReverseComplement("AGGCT")
'AGCCT'
>>> ReverseComplement("AGGCTRY")
'RYAGCCT'
"""
iupac_complement = {
'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A',
'R': 'Y', 'Y': 'R', 'S': 'S', 'W': 'W',
'K': 'M', 'M': 'K', 'B': 'V', 'D': 'H',
'H': 'D', 'V': 'B', 'N': 'N'
}
seq = seq.upper()
newseq = ""
size = len(seq)
for i in range(len(seq)):
char = seq[len(seq)-i-1]
if char == "A":
newseq += "T"
elif char == "G":
newseq += "C"
elif char == "C":
newseq += "G"
elif char == "T":
newseq += "A"
else: newseq += "N"
for char in reversed(seq):
newseq += iupac_complement.get(char, 'N')
return newseq

def InferRepeatSequence(seq, period):
Expand Down
Loading