001、 利用循环结构实现
[root@PC1 test01]# ls a.fa test.py [root@PC1 test01]# cat a.fa ## 测试DNA序列 AAAACCCGGT [root@PC1 test01]# cat test.py ## 程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read().strip()[::-1] str1 = str() for i in file: if i == "A": str1 += "T" elif i == "C": str1 += "G" elif i == "G": str1 += "C" elif i == "T": str1 += "A" else: print("anomanous letter!") exit print(str1) [root@PC1 test01]# python test.py ## 反向互补结果 ACCGGGTTTT
002、借助字典匹配碱基
[root@PC1 test01]# ls a.fa test.py [root@PC1 test01]# cat a.fa ## 测试DNA序列 AAAACCCGGT [root@PC1 test01]# cat test.py ## 求反向互补序列程序 #!/usr/bin/env python # -*- coding: utf-8 -*- in_file = open("a.fa", "r") file = in_file.read().strip()[::-1] in_file.close() dict1 = {"A":"T", "C":"G", "G":"C", "T":"A"} str1 = "" for i in file: if i not in dict1: print("anomanous letters!") break else: str1 += dict1[i] print(str1) [root@PC1 test01]# python test.py ## 转换结果 ACCGGGTTTT
标签:DNA,中求,python,str1,PC1,##,file,test01,root From: https://www.cnblogs.com/liujiaxin2018/p/17660903.html