Complement to DNA in output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bb64
    New Member
    • Sep 2010
    • 7

    Complement to DNA in output

    Write a Python 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.
    ----------------------------------------------------------
    ive been trying to figure out how to do this, and its really starting to bother me. i cant figure out what code to write to input ACCGT and receive an output of TGGCA. this is not a code to reverse the input, i need to tell the program to look for A's and replace them with T's and replace C's with G's. this is what ive tried so far, but still havent gotten it

    Code:
    with open("/Users/homemac/classes/bnfo135/dna.txt", "r") as myfile:
    	seq = myfile.readlines()
    	str(seq)
    	str.replace("A", "T")
    	str.replace("C", "G")
    	print(seq)
    
    	
    "['>human\\n', 'ACCGT\\n']"
    Traceback (most recent call last):
      File "<pyshell#8>", line 4, in <module>
        str.replace("A", "T")
    TypeError: replace() takes at least 2 arguments (1 given)
    
    >>> with open("/Users/homemac/classes/bnfo135/dna.txt", "r") as myfile:
    	seq = myfile.readlines()
    	str(seq)
    	str.replace("A", "T" + "C","G")
    	print(seq)
    
    	
    "['>human\\n', 'ACCGT\\n']"
    'A'
    ['>human\n', 'ACCGT\n']
    
    >>> with open("/Users/homemac/classes/bnfo135/dna.txt", "r") as myfile:
    	seq = myfile.readlines()
    	str(seq)
    	str.replace("A", "T" + "C","G")
    	print(str())
    
    	
    "['>human\\n', 'ACCGT\\n']"
    'A'
    Last edited by bvdet; Sep 14 '10, 03:35 AM. Reason: Add code tags. Please use them! [code] ...code goes here...[/code]
  • bb64
    New Member
    • Sep 2010
    • 7

    #2
    tried this, im getting closer and closer. but still not getting a single input value as the correct one.

    with open("/Users/homemac/classes/bnfo135/dna.txt", "r") as myfile:
    seq = myfile.readline s()
    str(seq)
    str.replace(str (seq), "A","T")
    str.replace(str (seq), "C","G")
    print(str(seq))


    "['>human\\n', 'ACCGT\\n']"
    "['>human\\n', 'TCCGT\\n']"
    "['>human\\n', 'AGGGT\\n']"
    ['>human\n', 'ACCGT\n']

    >>> with open("/Users/homemac/classes/bnfo135/dna.txt", "r") as myfile:
    seq = myfile.readline s()
    str(seq)
    str.replace(str (seq), "A","T")
    str.replace(str (seq), "C","G")
    print(str.repla ce)


    "['>human\\n', 'ACCGT\\n']"
    "['>human\\n', 'TCCGT\\n']"
    "['>human\\n', 'AGGGT\\n']"
    <method 'replace' of 'str' objects>
    >>>

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Take a look at the "Getting a certain output from a file" thread three more down. This is a common homework question and there are several other threads as well. A search for something like "DNA sequence" should produce some hits.

      Comment

      • bb64
        New Member
        • Sep 2010
        • 7

        #4
        i looked at that one and tried all those ways. none of them worked /: i think that guy is in my class, but i have no clue who he is. do you know what i could do to solve my problem ?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Here's a few hints. Please use code tags next time!
          Code:
          >>> lineList = ['>human\n', 'ACCGT\n']
          >>> seq = lineList[1].strip()
          >>> seq
          'ACCGT'
          >>> seq.replace("A", 't').replace('T', 'a').upper()
          'TCCGA'
          >>>

          Comment

          • bb64
            New Member
            • Sep 2010
            • 7

            #6
            any idea how to do it starting with the first line I use ?
            meaning I can't write out the dna sequence, I have to use the filepath

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              You need to take a good look at the hints I gave you. Your first and second lines of code open the file and read the file contents into a list of lines. I just changed the name of the variable to be more descriptive. You want the second item in the list which is accessed by list index.

              Comment

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                meaning I can't write out the dna sequence
                A link to a tutorial for reading and writing files.

                Comment

                Working...