1

I´m working on an assignment in python. I have to find a function or a program that converts this list to the corresponding DNA sequence.

bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']

print(dna)

AGTCTAGCTAG  #the output

I think maybe I can make a definition function to convert those bases to corresponding letters. How can I do that? Any ideas?

PS: I began to learn python very recently, so I hope you guys can help me :)

6 Answers6

4

Yes a few! as you begin not a long time ago, let do something simple:

bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']

dna = "" # You begin with the dna empty

for sequence in bases_list: # you go throw every chain
  dna += sequence[0] # you add the first letter with the op [0] of lists

print(dna) # finally print

You can read about the basis of loop here https://wiki.python.org/moin/ForLoop

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
2

Most succinct:

print("".join([x[0] for x in bases_list]))

dna = "".join(x[0] for x in bases_list])
print(dna)

otherwise:

dna = ""
for base in bases_list:
    dna += base[0] . # get 1st letter from base
print(dna)
Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
2

Let's use string indexing and join:

dna = ''.join(i[0] for i in bases_list)
print(dna)

Output:

AGTCTAGCTAG
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • What does the join syntax actually? I never saw that in our course :) – strawberrrry Oct 31 '17 at 13:02
  • @berr Join takes each element from an iterable and concatenates them into one string with or with a separator. [See docs](https://docs.python.org/3.6/library/stdtypes.html#str.join) – Scott Boston Oct 31 '17 at 13:04
2

How about a map?

In [276]: ''.join(map(lambda x: x[0], bases_list))
Out[276]: 'AGTCTAGCTAG'

str.join is used to take a list (or any iterable) of strings and join them together. map applies the same function to every element in an iterable.

Quick note. I ran this code on a terminal, but if you're running this in a script, don't forget to assign the result back to another variable.

seq = ''.join(map(lambda x: x[0], bases_list))
print(seq)
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Nice one, for advanced users ;) I'm thinking another simple way but I don't came with Ideas, do you have any else? – developer_hatch Oct 31 '17 at 13:05
  • @DamianLattenero No, I believe every simple option has been discussed in these answers. Anything else will need some thinking :) – cs95 Oct 31 '17 at 13:06
0

For completeness I thought I would add the itemgetter https://docs.python.org/3/library/operator.html#operator.itemgetter solution.

In this case, it gets the character in index (0) of every element in bases_list. It is said to be slightly faster and cleaner than lambda operator.itemgetter or lambda

from operator import itemgetter

bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine',
              'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']
dna = "".join((map(itemgetter(0),bases_list)))

print (dna)

result:

AGTCTAGCTAG
ragardner
  • 1,836
  • 5
  • 22
  • 45
0

If you want a function that converts the base name to its corresponding letter, you are lucky here, because it is simply the first letter of the base name.

Taking the first letter of a string is easily done by "indexing". For instance:

base = "Adenosine"
letter = base[0]
# letter should now be "A"

In a simple case like this, one would probably not bother defining a specific function, but it is possible and may look as follows:

def base2letter(base):
    """This function converts the name of a base into the corresponding letter."""
    return base[0]

Now you can apply the function to every base name in your list. The usual way to do this in python is by using a "list comprehension":

letters_list = [base2letter(base) for base in bases_list]

What you have now is the list of the letters: ["A", "G", "T", "C", "T", "A", "G", "C", "T", "A", "G"]

The next step is to join them together into a string. An efficient way to do this is to use the join method that is defined for strings. It sticks the string between the successive elements it is given: "_".join(["A", "B", "C"]) will result in "A_B_C". Here you want to put the letters together with nothing between them, so you have to use the empty string:

dna = "".join(letters_list)

Note that progressively building the string in a for loop by concatenating new letters one by one to the current state of the string is possible (see https://stackoverflow.com/a/47036161/1878788). This may be expected of you if python is just used to give you a general idea of how programming languages using an "imperative" style work. However, it is not efficient in python, that is why many answers did not use this technique.

bli
  • 7,549
  • 7
  • 48
  • 94