DNA字符串由一个由四个字符组成的字母组成,A,C,G, and T给出了一个字符串,
ATGTTTAAA我想把它分成它的组成密码子
ATG TTT AAA
codons = ["ATG","TTT","AAA"]密码子编码蛋白质,它们是多余的(表格)
我在D中有一个DNA串,想把它分裂成一系列的密码子,然后翻译/映射密码子到氨基酸。
std.algorithm有一个拆分器函数,它需要一个分隔符,而std.regex拆分器函数也需要一个正则表达式来拆分字符串。是否有一种不带分隔符的拆分字符串的惯用方法?
发布于 2015-05-04 11:34:45
看起来你在找chunks
import std.range : chunks;
import std.encoding : AsciiString;
import std.algorithm : map;
AsciiString ascii(string literal)
{
return cast(AsciiString) literal;
}
void main()
{
auto input = ascii("ATGTTTAAA");
auto codons = input.chunks(3);
auto aminoacids = codons.map!(
(codon) {
if (codon == ascii("ATG"))
return "M";
// ...
}
);
}请注意,我在这里使用的是encoding.html#.AsciiString,而不是普通的字符串文本。这是为了避免昂贵的UTF-8解码,这是为string做的,永远不适用于实际的DNA序列。我记得以前,对于类似的生物信息学代码来说,有显著的性能差异。
发布于 2015-05-04 11:46:03
如果您只需要3个字符的组,则可以使用std.range.chunks。
import std.conv : to;
import std.range : chunks;
import std.algorithm : map, equal;
enum seq = "ATGTTTAAA";
auto codons = seq.chunks(3).map!(x => x.to!string);
assert(codons.equal(["ATG", "TTT", "AAA"]));块的前端类型是Take!string,因此您可能需要也可能不需要map!(x => x.to!string),这取决于您想要如何使用结果。
例如,如果您只想打印它们:
foreach(codon ; "ATGTTTAAA".chunks(3)) { writeln(codon); }发布于 2015-05-04 11:14:50
import std.algorithm;
import std.regex;
import std.stdio;
int main()
{
auto seq = "ATGTTTAAA";
auto rex = regex(r"[AGT]{3}");
auto codons = matchAll(seq, rex).map!"a[0]";
writeln(codons);
return 0;
}https://stackoverflow.com/questions/30027850
复制相似问题