001、获取反向序列
>>> myseq = 'AGCTGGCTA' >>> myseq[::-1] ## 利用切片实现 'ATCGGTCGA' >>> temp = [] >>> for i in reversed(myseq): ## 借助reversed函数实现 ... temp.append(i) ... >>> temp ['A', 'T', 'C', 'G', 'G', 'T', 'C', 'G', 'A'] >>> "".join(temp) 'ATCGGTCGA'
002、获取互补序列
>>> myseq = 'AGCTGGCTA' ## 测试字符串 >>> myseq = myseq.replace("A", "t") ## 防止碱基混淆,先转换为互补碱基的小写字母 >>> myseq = myseq.replace("T", "a") >>> myseq = myseq.replace("C", "g") >>> myseq = myseq.replace("G", "c") >>> myseq 'tcgaccgat' >>> myseq = myseq.upper() ## 整个字符串转换为大小 >>> myseq 'TCGACCGAT'
003、获取反向互补序列
方法1、
>>> myseq = 'AGCTGGCTA' >>> str1 = "" >>> for i in myseq: ## 利用循环获取互补序列 ... if i == "A": ... str1 += "T" ... if i == "T": ... str1 += "A" ... if i == "C": ... str1 += "G" ... if i == "G": ... str1 += "C" ... >>> str1 'TCGACCGAT' >>> str1[::-1] ## 获取反向序列 'TAGCCAGCT'
方法2:
>>> myseq = 'AGCTGGCTA' >>> dict1 = {"A":"T", "T":"A", "C":"G", "G":"C"} ## 匹配模板 >>> [dict1[k] for k in myseq] ## 利用列表内循环结构获取互补序列 ['T', 'C', 'G', 'A', 'C', 'C', 'G', 'A', 'T'] >>> [dict1[k] for k in myseq][::-1] ['T', 'A', 'G', 'C', 'C', 'A', 'G', 'C', 'T'] >>> "".join([dict1[k] for k in myseq][::-1]) ## 取反并转换为字符串 'TAGCCAGCT'
参考:https://mp.weixin.qq.com/s?__biz=MzkyMTI1MTYxNA==&mid=2247493739&idx=1&sn=f690c93761307e6ec9bb77cca2eb4619&chksm=c184d21af6f35b0cda1d964ed896adee1091e1f615b7f6be0caf2508105275ca3ae66889c58e&mpshare=1&scene=23&srcid=0811LY0ghlyV0yNXki8WcW6m&sharer_sharetime=1660215059305&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd