pujvzi的个人博客分享 http://blog.sciencenet.cn/u/pujvzi

博文

Python练习

已有 2308 次阅读 2018-12-24 20:23 |个人分类:生信|系统分类:科研笔记


Python练习

(1)生成随机序列

(2)反向互补序列

(3)查找序列



(1)生成随机序列

#########生成随机序列#############

def RandomSeq(SeqLength):

  #import random

  #import

  myT = ""

  myRT = list(range(0,int(SeqLength)))

  i = -1

  while len(myT) < SeqLength:

    b = random.choice('ACTG')

    myT = myT + b

    rb = ""

    if b == "A":

      rb = "T"

    elif b == "T":

      rb = "A"

    elif b == "C":

      rb = 'G'

    elif b == "G":

      rb = "C"

    myRT[i] = rb

    i = i-1

  myRT = "".join(myRT)

  return [myT,myRT]

################################

>>> RandomSeq(20)

['CGTCAGGCCTGTGAATCAGC', 'GCTGATTCACAGGCCTGACG']

>>>

################################


(2)反向互补序列

##############反向互补##################

def revCom(seq):

  seq = list(seq)

  myL = len(seq)

  #myRC = list(range(0,myL))

  End = -1

  myRC = ""

  while End >= 0-myL:

    b = seq[End]

    rb = ""

    if b == "A":

      rb = "T"

    elif b == "T":

      rb = "A"

    elif b == "C":

      rb = 'G'

    elif b == "G":

      rb = "C"

    End = End-1

    myRC = myRC+rb

  return myRC


################################

>>> revCom("CGTCAGGCCTGTGAATCAGC")

'GCTGATTCACAGGCCTGACG'

>>>

################################



(3)查找序列

#############找序列#############

[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]


在这里,pattern举例:

pattern = re.compile("ACGT[AT]GG")

string = "ACGTAGGAGCTGTCGATGCACGTTGGAAATGC"

################################

>>> string = "ACGTAGGAGCTGTCGATGCACGTTGGAAATGC"

>>> import re

>>> pattern = re.compile("ACGT[AT]GG")

>>> [(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]

[(0, 7), (19, 26)]

>>>

################################



https://blog.sciencenet.cn/blog-331314-1153334.html

上一篇:睾丸生殖细胞的免疫荧光标记
下一篇:寻找合适的Cas9靶位点的脚本
收藏 IP: 61.140.255.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-12-5 06:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部