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 @   小鲨鱼2018  阅读(159)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-08-26 R语言中apply函数的用法
2022-08-26 R语言中colSums和rowSums函数
2022-08-26 R语言中绘制小提琴图
2021-08-26 c语言中限制用户输入整数
2021-08-26 c语言 输入验证(限制输入正数)
点击右上角即可分享
微信分享提示