Getting a certain output from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ah68
    New Member
    • Sep 2010
    • 2

    Getting a certain output from a file

    Hi, all.

    I'm very new to python and it's a requirement for my biology major to take at least one class in bioinformatics.

    I am having some difficulty with the homework my professor gave. The assignment is below:

    Write a Perl script that computes the complement
    of a DNA sequence. In other words, your script should convert all
    A's to T's, C's to G's, G's to C's, and T's to A's.

    The input is one sequence in FASTA format in a file called "dna.txt".

    For example if the file contains

    >human
    ACCGT

    then the output of the program should be TGGCA. Note that your program should work for
    any sequence in this format and not just the given example.

    _______________ _________

    How do I do this, because I have absolutely no idea. He wants a very basic code.

    Also, where are good tutorials online for Python? I did order a book but until I receive it I need some thing to help me with homework. Thank you, I really appreciate it.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Is your question about Perl or Python? In Python, you will want to use str method replace(). Example:
    Code:
    >>> "ATTAC".replace("A", "B")
    'BTTBC'
    >>>
    For online tutorials, use these search words: Python tutorial str replace

    Comment

    • ah68
      New Member
      • Sep 2010
      • 2

      #3
      It's Python. My university changed to using python this semester.

      So I tried what you suggested and did a search. I tried to do it one way I found which was the following:

      with open ("c:/temp/dna.fasta.txt", "r") as myfile:
      lines = myfile.readline s()
      print(lines)
      ['>human \n', 'ACCGT \n']

      str = "ACCGT";
      >>> print str.replace("A" , "T")
      SyntaxError: invalid syntax

      ^^ that way gave me an error & highlighted str

      Then I tried your way by doing this:

      "ACCGT".replace ("A", "T")
      'TCCGT'

      ^ This worked but it has to do it for each of the letters so I tried this:


      "ACCGT".rep lace ("A", "T" + "C", "G" + "T", "A" + "G", "C")

      Traceback (most recent call last):
      File "<pyshell#1 2>", line 1, in <module>
      "ACCGT".rep lace ("A", "T" + "C", "G" + "T", "A" + "G", "C")
      TypeError: replace() takes at most 3 arguments (5 given)


      How can I use a code to do replace all the letters at once without each time copying the last output and then making a new str.replace line?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Do not use str as a variable name, because the built-in function str() will be masked. Please use code tags when posting code.

        To replace multiple letters:
        Code:
        "ATTACGGCC".replace("A", "T").replace("C", "G").replace("T", "A").replace("G", "C")
        Doing it this way gives you the wrong answer though. There is a trick you can use to get around that. Replace "A" with "t" and "T" with "a", then convert the string to upper case with str method upper().

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          You can iterate over each letter individually and change it. If you know how to use a dictionary, you can store the "from" and "to" values in one dictionary, otherwise if/elif statements would probably be used.
          Code:
          with open ("c:/temp/dna.fasta.txt", "r") as myfile:
          lines = myfile.readlines()
          ##print(lines)
          for one_rec in lines:
              print "-" * 50
              print one_rec
              new_rec = []
              for each_char in one_rec:
                  if each_char == "A":
                      each_char = "T"
                  elif each_char == "C":
                      each_char = "G"
                  new_rec.append(each_char)
              print "".join(new_rec)
          Also, where are good tutorials online for Python
          http://www.freenetpages.co.uk/hp/alan.gauld/tutcont.htm

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Folks, I may be stoned or stupid, but it'll be quicker and more concise to make eight changes.

            Change the original string to an intermediate form
            Code:
            sequence.replace("A","W")
            sequence.replace("C","X")
            sequence.replace("G","Y")
            sequence.replace("T","Z")
            and then replace the intermediate form with the final form.
            Code:
            sequence.replace("Z","A")
            sequence.replace("Y","C")
            sequence.replace("X","G")
            sequence.replace("W","T")
            BTW, in perl, this becomes one statement, if I recall correctly:
            Code:
            sequence =~ s/ACGT/TGCA/g;

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Actually, I prefer the dictionary solution below as a general solution:
              Code:
              >>> dd = {"T": "A", "A": "T", "G": "C", "C": "G"}
              >>> seq = "ATTCGGCAACT"
              >>> "".join([dd[s] for s in seq])
              'TAAGCCGTTGA'
              >>>

              Comment

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                Folks, I may be stoned or stupid, but it'll be quicker and more concise to make eight changes.
                It won't be quicker, because you have to process each record 8 times, or however may replacements there are, and each replace requires 2 blocks of memory. The starting point should be to process the record once and append to a list so no intermediate blocks of memory are required (although, with short files the difference may be negligible).

                Actually, I prefer the dictionary solution below as a general solution
                Should we assume that all letters will be changed? I don't know. If all letters are not changed, then an if statement is required to test for membership in the dictionary, or first populate the dictionary with all letters, for ltr in string.uppercas e: dd[ltr]=ltr, and then change the dictionary to reflect the letters that should be changed.

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  Thanks,

                  I was sticking my nose into a forum where I'm still learning. I appreciate your patience

                  I saw a few solutions that would have mapped two genomes into one. I was just trying to show how to avoid the problem without being rude.

                  The dictionary solution is much better, and now I know how to do it.

                  Thanks!
                  Oralloy

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Should we assume that all letters will be changed? I don't know. If all letters are not changed, then an if statement is required to test for membership in the dictionary, or first populate the dictionary with all letters, for ltr in string.uppercas e: dd[ltr]=ltr, and then change the dictionary to reflect the letters that should be changed.
                    The dictionary would be defined for the letters that need to be changed. Dictionary method get() comes in handy if the dictionary does not contain all possible letters
                    Code:
                    >>> seq = "ATTCGGCAACTxyz"
                    >>> "".join([dd.get(s, s) for s in seq])
                    'TAAGCCGTTGAxyz'
                    >>>

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Oralloy,

                      Please come back anytime you want. The more viewpoints, the better!

                      BV

                      Comment

                      • Oralloy
                        Recognized Expert Contributor
                        • Jun 2010
                        • 988

                        #12
                        Folks,

                        I think the point of ah68's assignment was to introduce him to using computers as a tool, not to write a general genome manipulation tool.

                        Let's not confuse the man. I know that in industry the good folks all try to anticipate problems and prepare for them, even when they don't solve them. And I think its a good practice. If anyone working for me didn't try to think things through, I'd be seriously worried.

                        Anyway, that's my current 2 cents value.

                        Cheer!
                        Oralloy

                        Comment

                        Working...