在C#中将二进制编码为DNA序列
encoding binary to DNA sequence inC#
我想根据真值将二进制序列编码为 DNA 序列 table :
00=A
01=C
10=G
11=T
例如:11000110=``TACG
通过使用 C#,我的问题是 DNA 序列未正确转换。有人可以帮我吗?
我写的代码是这样的:
string ds = Convert.ToString(result , 2);
;
int l = ds.Length;
for (int dd= 0; dd < l; dd = dd + 2)
{
if (ds.Contains("00"))
{
ds = ds.Replace("00", "A");
}
if (ds.Contains("01"))
{
ds = ds.Replace("01", "C");
}
if (ds.Contains("10"))
{
ds = ds.Replace("10", "G");
}
else
{
ds = ds.Replace("11", "T");
}
}
listBox7.Items.Add(ds);
这是我的建议。事实上,有成千上万种可能的解决方案。
string binary = "011001010101000100101";
var codes = new Dictionary<string, string> {
{"00", "A"},
{"01", "C"},
{"10", "G"},
{"11", "T"}
};
StringBuilder builder = new StringBuilder();
for(int i = 0; i + 1 < binary.Length; i = i + 2) {
var localCode = string.Format("{0}{1}", binary[i], binary[i+1]);
string buffer;
var output = codes.TryGetValue(localCode, out buffer) ? buffer : string.Empty;
builder.Append(output);
}
string result = builder.ToString();
是这样的吗?
var dna = DNA("11000110");
string DNA(string input)
{
var dict = new Dictionary<string, string>() { { "11", "T" }, { "00", "A" },
{ "01", "C" }, { "10", "G" } };
int inx = 0;
return string.Concat(input.GroupBy(x => inx++ / 2).Select(g => dict[string.Concat(g)]));
}
结果:dna = TACG
我想根据真值将二进制序列编码为 DNA 序列 table :
00=A
01=C
10=G
11=T
例如:11000110=``TACG
通过使用 C#,我的问题是 DNA 序列未正确转换。有人可以帮我吗?
我写的代码是这样的:
string ds = Convert.ToString(result , 2);
;
int l = ds.Length;
for (int dd= 0; dd < l; dd = dd + 2)
{
if (ds.Contains("00"))
{
ds = ds.Replace("00", "A");
}
if (ds.Contains("01"))
{
ds = ds.Replace("01", "C");
}
if (ds.Contains("10"))
{
ds = ds.Replace("10", "G");
}
else
{
ds = ds.Replace("11", "T");
}
}
listBox7.Items.Add(ds);
这是我的建议。事实上,有成千上万种可能的解决方案。
string binary = "011001010101000100101";
var codes = new Dictionary<string, string> {
{"00", "A"},
{"01", "C"},
{"10", "G"},
{"11", "T"}
};
StringBuilder builder = new StringBuilder();
for(int i = 0; i + 1 < binary.Length; i = i + 2) {
var localCode = string.Format("{0}{1}", binary[i], binary[i+1]);
string buffer;
var output = codes.TryGetValue(localCode, out buffer) ? buffer : string.Empty;
builder.Append(output);
}
string result = builder.ToString();
是这样的吗?
var dna = DNA("11000110");
string DNA(string input)
{
var dict = new Dictionary<string, string>() { { "11", "T" }, { "00", "A" },
{ "01", "C" }, { "10", "G" } };
int inx = 0;
return string.Concat(input.GroupBy(x => inx++ / 2).Select(g => dict[string.Concat(g)]));
}
结果:dna = TACG