李拖沓

V1

2022/11/17阅读:31主题:嫩青

2022-11-17 Finding a Motif in DNA

复习

Python range() 函数 | 菜鸟教程 (runoob.com)

Python range() 函数用法

  • start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
  • stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
  • step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

列表转字符串

python中list与string的转换_bufengzj的博客-CSDN博客_list转string

1.list转string 命令:''.join(list)

其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等

如:

list = [1, 2, 3, 4, 5]

''.join(list) 结果即为:12345

','.join(list) 结果即为:1,2,3,4,5

匿名函数

匿名函数 - 廖雪峰的官方网站 (liaoxuefeng.com)

匿名函数

最后更新: 2018/5/30 14:19 / 阅读: 95492400


当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便。

在Python中,对匿名函数提供了有限支持。还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x)的函数外,还可以直接传入匿名函数:

>>> list(map(lambda x: x * x, [123456789]))
[149162536496481]

通过对比可以看出,匿名函数lambda x: x * x实际上就是:

def f(x):
    return x * x

仿写

早上起来,实在难忘昨天看到的那个RNA翻译蛋白的代码

为了整点花活,我决定仿写一下他的解法

codonTable = """UUU F      CUU L      AUU I      GUU V
UUC F      CUC L      AUC I      GUC V
UUA L      CUA L      AUA I      GUA V
UUG L      CUG L      AUG M      GUG V
UCU S      CCU P      ACU T      GCU A
UCC S      CCC P      ACC T      GCC A
UCA S      CCA P      ACA T      GCA A
UCG S      CCG P      ACG T      GCG A
UAU Y      CAU H      AAU N      GAU D
UAC Y      CAC H      AAC N      GAC D
UAA Stop   CAA Q      AAA K      GAA E
UAG Stop   CAG Q      AAG K      GAG E
UGU C      CGU R      AGU S      GGU G
UGC C      CGC R      AGC S      GGC G
UGA Stop   CGA R      AGA R      GGA G
UGG W      CGG R      AGG R      GGG G """


transtring = codonTable.split()
codonDict = dict(zip(transtring[::2], transtring[1::2]))

rnaSeq = "AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA"

proteinlist = list(map(lambda i: codonDict[rnaSeq[i:i + 3]], range(0, len(rnaSeq) - 33)))

print("".join(proteinlist))

总结

又为了防止我以后自己都看不懂,总结一下:

  1. 遇到长串中间格式很复杂(带换行符)的字符串,要用三个冒号圈起来。

  2. 代码前半部分和原创的一样。

  3. lambda i: codonDict[rnaSeq[i:i + 3]] 这里调用的代码生成式,依照廖雪峰老师网站上的解释其实相当于

    def f(i):
        return codonDict[rnaSeq[i:i + 3]]
  4. 紧跟着匿名函数的是 i 的取值范围 range(0, len(rnaSeq) - 3, 3))

  5. 最后用 map() 函数遍历整个匿名函数,得到所有返回值。

  6. 然后用 list() 把这些结果转成列表,得到包含所有蛋白的字符列表。

  7. "".join() 根据CSDN查到的解释,可以把列表里的字符拼成字符串,打印出来即可。

Finding a Motif in DNA

ROSALIND | Finding a Motif in DNA

Problem

Given two strings ss and tt, tt is a substring of ss if tt is contained as a contiguous collection of symbols in ss (as a result, tt must be no longer than ss).

The position of a symbol in a string is the total number of symbols found to its left, including itself (e.g., the positions of all occurrences of 'U' in "AUGCUUCAGAAAGGUCUUACG" are 2, 5, 6, 15, 17, and 18). The symbol at position ii of ss is denoted by s[i]s[i].

A substring of ss can be represented as s[j:k]s[j:k], where jj and kk represent the starting and ending positions of the substring in ss; for example, if ss = "AUGCUUCAGAAAGGUCUUACG", then s[2:5]s[2:5] = "UGCU".

The location of a substring s[j:k]s[j:k] is its beginning position jj; note that tt will have multiple locations in ss if it occurs more than once as a substring of ss (see the Sample below).

Given: Two DNA strings ss and tt (each of length at most 1 kbp).

Return: All locations of tt as a substring of ss.

Sample Dataset

GATATATGCATATACTT
ATAT

Sample Output

2 4 10

思考

说实话,看完题目第一眼,我想到的是 re 正则的 findall() 方法,毕竟昨天才用过。

但是如果不用正则能不能做?毕竟RNA翻译蛋白的代码就是个例子。

我的解法

本来搞了半天,也想用匿名函数的,弄完发现可读性还不如这样,也不优雅。所以还是这样了,期待大神的解答。

s = "GATATATGCATATACTT"
t = "ATAT"

for i in range(len(s)):
    if s[i:i+len(t)] != t:
        continue
    print(i+1, end=" ")

大神解答

s1,s2 = open('rosalind_subs.txt').read().split('\r\n')

for i in range(len(s1)):
    if s1[i:].startswith(s2):
        print i+1,

简洁明了,优雅。

分类:

后端

标签:

Python

作者介绍

李拖沓
V1