python中求DNA的反向互补序列
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
003、借助函数结构实现
[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"} def rev_com(dna, table): str1 = str() for i in dna: if i not in table: print("anomanous letter!") break else: str1 += table[i] return str1 print(rev_com(file, dict1)) [root@PC1 test01]# python test.py ## 反向互补结果 ACCGGGTTTT
。