我知道有一个包支持处理生物信息,比如Biojava,但我想编写一个简单的代码,将DNA序列转换为它的补码序列
这是我的代码。
public class SeqComplement{
static String sequence ;
String Complement(String sequence){
// this.sequence = sequence;
// String complement;
// complement = "N";
// for(int i = 0 ; i< sequence.length() ; i++){
String[] raw = new String[sequence.length()];
String[] complement = new String[sequence.length()];
String ComplementSeq = "N";
for(int i = 0 ; i <sequence.length() ; i++){
String sub = sequence.substring(i,i+1);
raw[i] = sub;
}
for(int j = 0 ; j < sequence.length();j++){
if(raw[j] == "A"){
complement[j] = "T";
}
else if (raw[j] == "T"){
complement[j] = "A";
}
else if (raw[j] == "G"){
complement[j] = "C";
}
else if (raw[j] == "C"){
complement[j] = "G";
}
else{
complement[j] = "N";
}
}
for(int k = 0 ; k < complement.length ; k++){
ComplementSeq+=complement[k];
}
return ComplementSeq.substring(1);
}
public static void main(String[] args){
SeqComplement.sequence = "ATCG";
SeqComplement ob = new SeqComplement();
System.out.println(ob.Complement(ob.sequence));
}
}此代码给出的结果为NNNN
我已经尝试过使用String.concat()方法,但它什么也没有得到。
发布于 2013-11-29 19:18:40
要将字符串转换为字符数组,应使用以下代码:
char[] raw = sequence.toCharArray();
char[] complement = sequence.toCharArray();为了比较字符串,你应该使用 ==运算符,你应该在字符串上调用.equals方法。
另一个好的建议是使用HashMap来存储补码,如下所示:
HashMap<Character, Character> complements = new HashMap<>();
complements.put('A', 'T');
complements.put('T', 'A');
complements.put('C', 'G');
complements.put('G', 'C');并像这样补充每个字符:
for (int i = 0; i < raw.length; ++i) {
complement[i] = complements.get(raw[i]);
}完整代码:
public class SeqComplement {
private static HashMap<Character, Character> complements = new HashMap<>();
static {
complements.put('A', 'T');
complements.put('T', 'A');
complements.put('C', 'G');
complements.put('G', 'C');
}
public String makeComplement(String input) {
char[] inputArray = input.toCharArray();
char[] output = new char[inputArray.length];
for (int i = 0; i < inputArray.length; ++i) {
if (complements.containsKey(inputArray[i]) {
output[i] = SeqComplement.complements.get(inputArray[i]);
} else {
output[i] = 'N';
}
}
return String.valueOf(output);
}
public static void main(String[] args) {
System.out.println(new SeqComplement().makeComplement("AATCGTAGC"));
}
}发布于 2013-11-29 18:54:41
使用==进行字符串比较,这几乎肯定不是您想要的(它检查字符串使用的底层对象是否相同,而不是值是否相等)。对于您期望的等价样式行为,请使用.equals()。
因此,不是:
if(raw[j] == "A")您将使用:
if(raw[j].equals("A"))发布于 2013-11-29 19:06:53
贝瑞是对的。嗯,我还没有我的笔记本电脑,所以无法运行它。
除了berry的建议之外,我还要求您将main()中的代码行替换为以下内容:
SeqComplement ob = new SeqComplement();
ob.sequence = "ATCG";
System.out.println(ob.Complement(ob.sequence));如果有帮助,请让我知道:-)
https://stackoverflow.com/questions/20283903
复制相似问题