||
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)]
>>>
################################
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-5 06:25
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社