• python 中获取反向序列及互补序列


    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

  • 相关阅读:
    Mysql Window 解压版卸载
    maven jdk 版本配置
    单点登录原理与简单实现
    JAVA HASHMAP的死循环
    Intellij IDEA 4种配置热部署的方法
    springmvc 返回 404 解决
    设计模式 -- 模版模式
    安装Oracle Database 11g 找不到文件“WFMLRSVCApp.ear” .
    红黑树 -- 增删查改
    TIJ -- CountDownLatch
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16577908.html
Copyright © 2020-2023  润新知