我创建了一个程序,用户必须输入一个序列,程序必须将每三个字母与之前在程序中输入的数据进行比较。并且总数据为25个案例,这意味着程序必须将Userseq的每个3个字符与总共25个案例进行比较(如果每个3个字符为25个语句,则为25个)。然后在完成前3个字符后,它将移动到下一个3字符,直到userseq完成。如果前三个char例如等于之前输入的数据之一,则必须在输出中显示它。我想用另一种方法来减少代码,这可以避免我的所有if语句。我还有另外一个问题,就是我要求程序执行相同的操作,直到第10组userseq的第3组相同的比较。如果用户输入的少于10个字符的3个字符组(这意味着少于30个字符),则程序会说“未处理的异常:System.ArgumentOutofrangeException:索引和长度必须引用字符串中的位置。这是我的代码。 NB,那些代码我通过仅改变userseq号码(userseq1,userseq2 ..)写了10次。是否有办法写入程序,为每组3个字符重复相同的工作,直到userseq结束? / p>
Console.Write("Enter your sequecnce: ");
string Userseq = Console.ReadLine();
string[] aa1 = { "TTT", "TTC" };
string[] aa2 = { "TTA", "TTG" };
string[] aa3 = { "CTT", "CTC", "CTA", "CTG" };
string[] aa4 = { "ATT", "ATC", "ATA" };
string[] aa5 = { "ATG" };
string[] aa6 = { "GTT", "GTC", "GTA", "GTG" };
string[] aa7 = { "TCT", "TCC", "TTCA", "TCG" };
string[] aa8 = { "CCT", "CCC", "CCA", "CCG" };
string[] aa9 = { "ACT", "ACC", "ACA", "ACG" };
string[] aa10 = { "GCT", "GCC", "GCA", "GCG" };
string[] aa11 = { "TAT", "TAC" };
string[] aa12 = { "TAA", "TAG" };
string[] aa13 = { "CAT", "CAC" };
string[] aa14 = { "CAA", "CAG" };
string[] aa15 = { "AAT", "AAC" };
string[] aa16 = { "AAA", "AAG" };
string[] aa17 = { "GAT", "GAC" };
string[] aa18 = { "GAA", "GAG" };
string[] aa19 = { "TGT", "TGC" };
string[] aa20 = { "TGA" };
string[] aa21 = { "TGG" };
string[] aa22 = { "CGT", "CGC", "CGA", "CGG" };
string[] aa23 = { "AGT", "AGC" };
string[] aa24 = { "AGA", "AGG" };
string[] aa25 = { "GGT", "GGC", "GGA", "GGG" };
string Userseq1 = Userseq.Substring(0, 3);
if (aa1.Contains(Userseq1))
{
Console.Write("Phe" + " ");
}
else if (aa2.Contains(Userseq1))
{
Console.Write("Leu" + " ");
}
else if (aa3.Contains(Userseq1))
{
Console.Write("Leu" + " ");
}
else if (aa4.Contains(Userseq1))
{
Console.Write("Ile" + " ");
}
else if (aa5.Contains(Userseq1))
{
Console.Write("Met" + " ");
}
else if (aa6.Contains(Userseq1))
{
Console.Write("Val" + " ");
}
else if (aa7.Contains(Userseq1))
{
Console.Write("Ser" + " ");
}
else if (aa8.Contains(Userseq1))
{
Console.Write("Pro" + " ");
}
else if (aa9.Contains(Userseq1))
{
Console.Write("Thr" + " ");
}
else if (aa10.Contains(Userseq1))
{
Console.Write("Ala" + " ");
}
else if (aa11.Contains(Userseq1))
{
Console.Write("Tyr" + " ");
}
else if (aa12.Contains(Userseq1))
{
Console.Write("STOP" + " ");
}
else if (aa13.Contains(Userseq1))
{
Console.Write("His" + " ");
}
else if (aa14.Contains(Userseq1))
{
Console.Write("Gin" + " ");
}
else if (aa15.Contains(Userseq1))
{
Console.Write("Asn" + " ");
}
else if (aa16.Contains(Userseq1))
{
Console.Write("Lys" + " ");
}
else if (aa17.Contains(Userseq1))
{
Console.Write("Asp" + " ");
}
else if (aa18.Contains(Userseq1))
{
Console.Write("Glu" + " ");
}
else if (aa19.Contains(Userseq1))
{
Console.Write("Cys" + " ");
}
else if (aa20.Contains(Userseq1))
{
Console.Write("STOP" + " ");
}
else if (aa21.Contains(Userseq1))
{
Console.Write("Trp" + " ");
}
else if (aa22.Contains(Userseq1))
{
Console.Write("Arg" + " ");
}
else if (aa23.Contains(Userseq1))
{
Console.Write("Ser" + " ");
}
else if (aa24.Contains(Userseq1))
{
Console.Write("Arg" + " ");
}
else if (aa25.Contains(Userseq1))
{
Console.Write("Gly" + " ");
}
else
{
Console.WriteLine(" \n You entered an invalid character. Other than A, T, C and G. Please check and retry.");
}
答案 0 :(得分:6)
创建一个具有Name和Sequence属性的类来保存每个序列,例如:
class EnzymeSequence
{
public string Name {get;set;}
public string[] Enzymes {get;set;}
public EnzymeSequence(string name,string[] enzymes)
{
Name=name;
Enzymes=enzymes;
}
}
并创建一个数组或序列列表,您可以在其中添加所需的所有序列:
var sequences=new EnzymeSequence[]{
new EnzymeSequence("Phe",new[]{{ "TTT", "TTC" }),
...
};
要查找序列,您可以写:
var match=sequences.FirstOrDefault(ez=>ez.Enzymes.Contains(Userseq1));
结果将是包含用户输入内容的序列或null。
if (match==null)
{
Console.WriteLine(" \n You entered an invalid character. Other than A, T, C and G. Please check and retry.");
}
else
{
Console.Write("{0} ",match.Name);
}
如果要查找具有某种酶的所有序列:
var matches=sequences.Where(ez=>ez.Enzymes.Contains(Userseq1));
如果您只想要匹配的名称:
var names=sequences.Where(ez=>ez.Enzymes.Contains(Userseq1))
.Select(ez=>ez.Name);
var line=String.Join(" ",names);
Concole.WriteLine(line);
如果您有 lot 序列,可以使用Parallel LINQ并行执行查找:
var names=sequences.AsParallel()
.Where(ez=>ez.Enzymes.Contains(Userseq1))
.Select(ez=>ez.Name);
var line=String.Join(" ",names);
Concole.WriteLine(line);
如果涉及序列的批次,则PLINQ将仅显示改进,大约数千。
另一个选择是创建一个将酶与序列匹配的“反向”字典。由于许多序列可以包含相同的酶,我们需要在创建由酶键入的词典之前,通过它们的酶对序列 - 酶对进行分组:
var enzymeSequences=(from seq in sequences
from enzyme in seq.Enzymes
let pair = new { seq.Name, enzyme }
group pair by enzyme into enzymeSeqs
select enzymeSeqs);
var enzymeDict= enzymeSequences.ToDictionary(
grp => grp.Key,
grp=> grp.Select(pair=>pair.Name)
.ToArray());
var matches = enzymeDict["AGG"];
Console.WriteLine(String.Join(" ", matches));
答案 1 :(得分:0)
我会采用使用Dictionary的方法,如下所示:
public class TestClass
{
private Dictionary<string, List<string>> sequences;
public TestClass()
{
sequences = new Dictionary<string, List<string>>();
sequences.Add("Phe", new List<string>() {"TTT", "TTC"});
// Repeat for all the sequences.
}
public string GetSequenceType(string sequence)
{
foreach (var key in sequences.Keys)
{
if (sequences[key].Contains(sequence)
return key;
}
return null;
}
}