0

I know that there exists a package that supports handling biological information such as Biojava , but I want to make a simple code which will convert a DNA sequence into a complement sequence of it

Here is my code...

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));
    }
}

This code gave result as NNNN

I already tried using String.concat() method, but it gave me nothing.

pts
  • 80,836
  • 20
  • 110
  • 183

3 Answers3

3

To make an string into an array of characters, you should use this code:

char[] raw = sequence.toCharArray();
char[] complement = sequence.toCharArray();

Also for comparing strings you should not use == operator, you should call .equals method on string.

Another good suggestion is using a HashMap for storing complements like this:

HashMap<Character, Character> complements = new HashMap<>();
complements.put('A', 'T');
complements.put('T', 'A');
complements.put('C', 'G');
complements.put('G', 'C');

And complement each character like this:

for (int i = 0; i < raw.length; ++i) {
    complement[i] = complements.get(raw[i]);
}

Full code:

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"));
  }
}
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
  • Instead of a `HashMap` you can use a `char` array of size `('T'+1)` and initialize the four entries `array['T']='A';`, `array['A']='T';`, etc. It will waste some array entries but be still more efficient than a `HashMap` in this case. – Holger Nov 29 '13 at 12:14
  • @Holger You're right, but i think `HashMap` is a neat and more expandable solution ;) – Mohammad Jafar Mashhadi Nov 30 '13 at 08:50
1

You're using == for String comparison, which is almost certainly not what you want (it checks whether the underlying objects used by the strings are the same, rather than whether the values are equivalent.) For the equivalence style behaviour you expect, use .equals().

So instead of:

if(raw[j] == "A")

You would use:

if(raw[j].equals("A"))
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • 1
    It might be a matter of preference, but it seems like a good habit to put the constant in front in the equals comparison. – Simon Verhoeven Nov 29 '13 at 10:59
  • @SimonVerhoeven Yes, for the reasons mentioned [here](http://stackoverflow.com/questions/5712100/interview-java-equals) – Slate Nov 29 '13 at 11:00
  • 2
    @SimonVerhoeven Often I'd agree, but in this case not so, since there's never a case where we expect `raw[j]` to be null (and thus, if it is for some reason because a bug has been introduced, putting the constant first will hide the NPE.) – Michael Berry Nov 29 '13 at 11:02
0

berry is correct. well, I have not yet my laptop available so could not run it.

in addition to berry's suggestion i would ask you to replace lines inside main() with below:

SeqComplement ob = new SeqComplement();
ob.sequence = "ATCG";
System.out.println(ob.Complement(ob.sequence));

let me know if this helps :-)