python中将DNA链转换为RNA链

 

001、利用循环结构

[root@PC1 test01]# ls
a.fa  test.py
[root@PC1 test01]# cat a.fa        ## 测试DNA序列
GATGGAACTTGACTACGTAAATT
[root@PC1 test01]# cat test.py     ## 转换程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-

in_file = open("a.fa", "r")

str1 = ""
for i in in_file:
        i = i.strip()
        for j in i:
                if j == "T":
                        str1 += "U"
                else:
                        str1 += j
in_file.close()
print(str1)
[root@PC1 test01]# python test.py    ## 转换结果
GAUGGAACUUGACUACGUAAAUU

 

002、利用循环结构

[root@PC1 test01]# ls
a.fa  test.py
[root@PC1 test01]# cat a.fa         ## 测试DNA序列
GATGGAACTTGACTACGTAAATT
[root@PC1 test01]# cat test.py      ## 转换程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-

in_file = open("a.fa", "r")

file = in_file.read().strip()

for i in file:
        if i == "T":
                print("U", end = "")
        else:
                print(i, end = "")
in_file.close()
print("")
[root@PC1 test01]# python3 test.py   ## 转换结果
GAUGGAACUUGACUACGUAAAUU

 

003、利用字符串替换实现

[root@PC1 test01]# ls
a.fa  test.py
[root@PC1 test01]# cat a.fa       ## 测试DNA序列
GATGGAACTTGACTACGTAAATT
[root@PC1 test01]# cat test.py    ## 转换程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-

in_file = open("a.fa", "r")

file = in_file.read().strip()
print(file.replace("T", "U"))
[root@PC1 test01]# python test.py    ## 转换结果
GAUGGAACUUGACUACGUAAAUU

 

004、利用函数结构实现

[root@PC1 test01]# ls
a.fa  test.py
[root@PC1 test01]# cat a.fa          ## 测试DNA序列
GATGGAACTTGACTACGTAAATT
[root@PC1 test01]# cat test.py       ## 转换程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-

in_file = open("a.fa", "r")
file = in_file.read().strip()

def trans(dna):
        return dna.upper().replace("T", "U")
print(trans(dna = file))
[root@PC1 test01]# python3 test.py       ## 转换结果
GAUGGAACUUGACUACGUAAAUU

 

参考:

https://mp.weixin.qq.com/s?__biz=MzIxMjQxMDYxNA==&mid=2247484157&idx=2&sn=869266deba638d49aa5e0bc6d2b0428c&chksm=9747cb64a03042720674040dbf19031021d298e3df300810c51883a07812419427021fd781aa&cur_album_id=1635727573621997580&scene=190#rd

 

posted @ 2023-08-26 19:54  小鲨鱼2018  阅读(121)  评论(0编辑  收藏  举报