我想在蛋白质序列中每10个字符插入一个换行符:
seq="MSKNKSPLLNESEKMMSEMLPMKVSQSKLNYEEKVYIPTTIRNRKQHCFRRFFPYIALFQ"在Perl中,这非常简单:
$seq=~s/(.{10})/$1\n/g ; # does the job!
perl -e '$seq="MSKNKSPLLNESEKMMSEMLPMKVSQSKLNYEEKVYIPTTIRNRKQHCFRRFFPYIALFQ"; $seq=~s/(.{10})/$1\n/g; print $seq'
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ在朱莉娅身上
replace(seq, r"(.{10})" , "\n")不起作用,因为我不知道获取捕获组(.{10})并将其替换为自身+ "\n“的方法
julia> replace(seq, r"(.{10})" , "\n")
"\n\n\n\n\n\n"因此,要做到这一点,我需要两个步骤:
julia> a=matchall(r"(.{1,10})" ,seq)
6-element Array{SubString{UTF8String},1}:
"MSKNKSPLLN"
"ESEKMMSEML"
"PMKVSQSKLN"
"YEEKVYIPTT"
"IRNRKQHCFR"
"RFFPYIALFQ"
julia> b=join(a, "\n")
"MSKNKSPLLN\nESEKMMSEML\nPMKVSQSKLN\nYEEKVYIPTT\nIRNRKQHCFR\nRFFPYIALFQ"
julia> println(b)
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
# Caution :
a=matchall(r"(.{10})" ,seq) # wrong if seq is not exactly a multiple of 10 !
julia> seq
"MSKNKSPLLNESEKMMSEMLPMKVSQSKLNYEEKVYIPTTIRNRKQHCFRRFFPYIAL"
julia> matchall(r"(.{10})" ,seq)
5-element Array{SubString{UTF8String},1}:
"MSKNKSPLLN"
"ESEKMMSEML"
"PMKVSQSKLN"
"YEEKVYIPTT"
"IRNRKQHCFR"
julia> matchall(r"(.{1,10})" ,seq)
6-element Array{SubString{UTF8String},1}:
"MSKNKSPLLN"
"ESEKMMSEML"
"PMKVSQSKLN"
"YEEKVYIPTT"
"IRNRKQHCFR"
"RFFPYIAL" 有没有一步解决方案或者更好(更快)的方法?
只是为了好玩,一个包含所有这些有趣答案的基准测试!(随julia 5.0更新)
function loop(a)
last = 0
#create the interval, in your case 10
salt = 10
#iterate in string (starts in the 10th value, don't forget julia use 1 to first index)
for i in salt:salt+1:length(a)
# replace the string for a new one with '\n'
a = string(a[1:i], '\n', a[i+1:length(a)])
last = Int64(i)
end
# replace the rest
a = string(a[1:length(a) - last % salt + 1], '\n', a[length(a) - last % salt + 2:length(a)])
println(a)
end
function regex1(seq)
a=matchall(r"(.{1,10})" ,seq)
b=join(a, "\n")
println(b)
end
function regex2(seq)
a=join(split(replace(seq, r"(.{10})", s"\1 ")), "\n")
println(a)
end
function regex3(seq)
a=replace(seq, r"(.{10})", Base.SubstitutionString("\\1\n"))
a= chomp(a) # because there is a new line at the end
println(a)
end
function intrapad(seq::String)
buf = IOBuffer((length(seq)*11)>>3) # big enough buffer
for i=1:10:length(seq)
write(buf,SubString(seq,i,i+9),'\n')
end
#return
print(takebuf_string(buf))
end
function join_substring(seq)
a=join((SubString(seq,i,i+9) for i=1:10:length(seq)),'\n')
println(a)
end
seq="MSKNKSPLLNESEKMMSEMLPMKVSQSKLNYEEKVYIPTTIRNRKQHCFRRFFPYIALFQ"
for i = 1:5
println("loop :")
@time loop(seq)
println("regex1 :")
@time regex1(seq)
println("regex2 :")
@time regex2(seq)
println("regex3 :")
@time regex3(seq)
println("intrapad :")
@time intrapad(seq)
println("join substring :")
@time join_substring(seq)
end我将基准测试更改为执行5次@time,并在执行5次@time后在此处发布结果:
loop :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIA
LFQ
0.000013 seconds (53 allocations: 3.359 KB)
regex1 :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
0.000013 seconds (49 allocations: 1.344 KB)
regex2 :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
0.000017 seconds (47 allocations: 1.703 KB)
regex3 :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
0.000013 seconds (31 allocations: 976 bytes)
intrapad :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
0.000007 seconds (9 allocations: 608 bytes)
join substring :
MSKNKSPLLN
ESEKMMSEML
PMKVSQSKLN
YEEKVYIPTT
IRNRKQHCFR
RFFPYIALFQ
0.000012 seconds (21 allocations: 800 bytes)Intrapad现在是第一个;)
发布于 2016-11-11 22:15:16
我不知道你如何使用REGEX,但我认为它可以解决你的问题:
a = "oiaoueaoeuaoeuaoeuaoeuaoteuhasonetuhaonetuahounsaothunsaotuaosu"
last = 0
#create the interval, in your case 10
salt = 10
#iterate in string (starts in the 10th value, don't forget julia use 1 to first index)
for i in salt:salt+1:length(a)
# replace the string for a new one with '\n'
a = string(a[1:i], '\n', a[i+1:length(a)])
last = Int64(i)
end
# replace the rest
a = string(a[1:length(a) - last % salt + 1], '\n', a[length(a) - last % salt + 2:length(a)])
println(a)https://stackoverflow.com/questions/40545980
复制相似问题