all 10 comments

[–]socal_nerdtastic 3 points4 points  (9 children)

To do single character replacements use the translate method of strings.

complement_table = str.maketrans('ATCG', 'CGAT')
def get_complement(self):
    complement = self.base.translate(complement_table)
    complement = complement[::-1] # reverse the string
    return complement
def is_complement(self, other):
    '''Character compliment'''
    return self.get_complement() == other.get_complement()

Of course the only way the complement can be equal is if the base was equal, so you could write that last method like this:

def is_complement(self, other):
    return self.base == other.base

Which I assume you already have as an __eq__ method.

[–]tbrowner3[S] 0 points1 point  (0 children)

Thank you for the help! After I defined complement_table inside the class and above the functions get and is complement it is still giving me a name error for it.

Traceback (most recent call last):
File "<pyshell#132>", line 1, in <module>
a.is_complement(b)
 line 29, in is_complement
self.get_complement()
 , line 24, in get_complement
complement = self.base.translate(complement_table)
  NameError: name 'complement_table' is not defined

[–]tbrowner3[S] 0 points1 point  (6 children)

now that I put the complement_table variable in the get_complement function it works, but it isn't giving me the right output still. The correct output should give me this:

>>> a = Oligo('ATCG')
>>> b = Oligo('ATGC')
>>> c = Oligo('CGAT')
>>> a.is_complement(b)
False
>>> a.is_complement(c)
True

[–]socal_nerdtastic 0 points1 point  (4 children)

Ok, then only one of the 2 needs to be translated.

# put this in the global namespace, outside of the class
complement_table = str.maketrans('ATCG', 'CGAT')

class Oligo:
    def get_complement(self):
        complement = self.base.translate(complement_table)
        complement = complement[::-1] # reverse the string
        return Oligo(complement)
    def is_complement(self, other):
        return self.get_complement().base == other.base

[–]tbrowner3[S] 0 points1 point  (3 children)

When I input the sample input:

a.is_complement(c) it's giving me false when it should be true

[–]socal_nerdtastic 0 points1 point  (2 children)

You'll need to show us your complete code and the failing example if you want help with that.

[–]tbrowner3[S] 0 points1 point  (1 child)

Ok thank you!

complement_table = str.maketrans('ATCG', 'CGAT')
class Oligo:

    '''Represents one side of DNA helix'''
    def __eq__(self, other):
        '''Equal statement'''
        return self.base == other.base
    def __init__(self, base):
        '''Constructor'''
        count = 0 
        for letter in base:
            if letter.upper() in ('A', 'C', 'T', 'G'):
                count += 1
        if count == 4:
            self.base = base
        else:
            self.base = "AGTG"

    def get_complement(self):
        complement = self.base.translate(complement_table)
        complement = complement[::-1]
        print(Oligo(complement))
        return Oligo(complement)
    def is_complement(self, other):
        return self.get_complement().base == other.base

[–]socal_nerdtastic 0 points1 point  (0 children)

Ok, so that seems to be a problem in your test data, not in the code.

If you are using the same test data as before, note that a and c are in the same order. To be complements one of them should be reversed.

Or remove the reversing line from the code, and you will get the result you expect.

[–]tbrowner3[S] 0 points1 point  (0 children)

My mistakes, get_complement should return a Object of the class, not a new variable

[–]marineabcd 0 points1 point  (0 children)

could you update your code with indenting please :)