1

The code I'm writing is supposed to find all the open reading frames (orfs) of a genetic sequence on the forward and reverse complement strands of DNA. To make the reverse strand of DNA, I intended to use str.maketrans() to map complementary bases to each other.

#!/usr/bin/env python3.3

import re
import sys
from argparse import ArgumentParser

pattern = re.compile(r'(?=(ATG(?:...)*?)(?=TAG|TGA|TAA))')

dna_seq = 'ATGACGGCTTGTTTCTTTTCTGTGGCTGCGTGA'

    def find_orfs(dna_seq):
        """
        finds all possible open reading frames (orfs)

        :param dna_seq: str, dna sequence
        :return: list, possible open reading frames
        """

        r_comp = dna_seq[::-1].translate(str.maketrans("ATGC","TACG"))
        return list(pattern.findall(dna) + pattern.findall(r_comp))

When I run this in the interpreter it works! It returns the correct answer:

['ATGACGGCTTGTTTCTTTTCTGTGGCTGCG']

When I run this as a script (version 3.3) I get AttributeError!

AttributeError: type object 'str' has no attribute 'maketrans'

But when I dir(str) in the interpreter (version 3.3), I see maketrans! What gives!?

After reading about the change to bytes.maketrans(), I tried this to no avail. What can I do to get the same functionality of maketrans() in python3.3?

Thomas Matthew
  • 2,321
  • 3
  • 27
  • 46
  • 1
    What system are you using? If you are using Mac OSX, for example, the default python (the interactive python) is 2.7 (or 2.x equivalent). Since python3 has various syntax changes, that might be affecting the different results. Otherwise, what 'doesn't work'? Do you get an error? Exception? Wrong result (if so, post it)? – Andriko13 May 06 '15 at 00:31
  • I'm using version3.3 (script and interpreter), running in Ubuntu 13.1, and the error is an attribute error (even though maketrans attribute appears in the str directory) – Thomas Matthew May 06 '15 at 00:35
  • 1
    Check whether your shebang returns the correct python. After your imports, do this: `print(sys.version)`. Does it return `3.x`? – Andriko13 May 06 '15 at 00:35
  • python 2.7.5+ ! how can I fix my shebang? – Thomas Matthew May 06 '15 at 00:38
  • 1
    That's your problem :) You can use the less portable shebang, by doing something like this: `#!/usr/local/bin/python3.3` (make sure your python3.3 is stored in that path, if not, replace usr/local/bin with the correct one). – Andriko13 May 06 '15 at 00:42
  • You're probably right, seeing as that the directory I have on my shebang doesn't exist! Is there a quick way to search all directories to find where python3 is? – Thomas Matthew May 06 '15 at 00:51
  • 1
    Sure. Go to your interactive interpreter, import `sys`, and run `print(sys.path)`. That should print the path to whatever version of python you are using. – Andriko13 May 06 '15 at 00:55
  • 1
    Just a suggestion, if you want to run the script with a certain interpreter and ignore the shebang, you can always use `python3.3 something.py` in the terminal after changing the directory containing the `something.py`. Usually this is done when you are running some mathematical calculation and will only need to run the script a few times. – Andriko13 May 06 '15 at 01:00

1 Answers1

1

It seems that your shebang line returns a 2.7.x version of the python interpreter. You can specify the direct path by using #!/usr/local/bin/python3.3 (change the path to fit the location of the interpreter) to make it work if you're not worried about portability (allowing other users to use your file). For a post on the differences between #!/usr/bin/env python vs #!/usr/local/bin/python, you can look here. Basically, the former will use the interpreter that first appears on the environment's $PATH, which in your case is python2.7.

Edit

OP was running the script using the shell with the following command: python myscript.py. This uses the default interpreter (2.7, in their case), which does not recognize the maketrans method. Running the script with python3.3 myscript.py solved the problem.

Community
  • 1
  • 1
Andriko13
  • 962
  • 1
  • 6
  • 34
  • Found where my python was hiding! If I am concerned with portability, how can I be sure that some other user is also running python3.3? Is it then appropriate to use `#!/usr/bin/env python3.3`? – Thomas Matthew May 06 '15 at 01:03
  • 1
    Ah it wouldn't necessarily be appropriate considering that it failed for you (and would inevitable fail for *some* of the other users). However, I am afraid that is a different question answered somewhere on SO already. I'm sorry I can't help with that, as I've never really been concerned with portability with any of my python apps. – Andriko13 May 06 '15 at 01:06
  • After changing the shebang to the location of python, and printing the version number, it still returns the 2.7.x version number! Is my default python version taking precedence over the shebang? – Thomas Matthew May 06 '15 at 01:11
  • How do you compile your script? – Andriko13 May 06 '15 at 01:12
  • 2
    Do you use a program such as TextMate? Make your shebang the following: `#!/usr/bin/env python3`. Go into terminal, after saving your .py file, and cd into the directory containing the script. Then run `chmod u+x yourscripthere.py`. Once this is complete, run your script using `./yourscripthere.py`. – Andriko13 May 06 '15 at 01:14
  • Not sure about compiling it, but I run it with `$ python test.py`, which I'm now realizing is probably making it revert back to the default python version. However, when I run it with just `$ test.py`, I get 'test.py: command not found'. It looks like my shebang is being ignored, how can I get the interpreter to recognize it? – Thomas Matthew May 06 '15 at 01:19
  • 2
    You should be running it with `$ python3.3 test.py`!! If you want to simply run `test.py` (which will take into account the shebang), use the chmod command to make it executable. – Andriko13 May 06 '15 at 01:21
  • Perfection. Thank you for your in-depth assistance (and your patience), Andriko! – Thomas Matthew May 06 '15 at 01:23
  • No problem. Happy to assist :) – Andriko13 May 06 '15 at 01:30