user3049049
user3049049

Reputation: 25

Java String Concatenation using Array

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.

Upvotes: 0

Views: 326

Answers (3)

Mohammad Jafar Mashhadi
Mohammad Jafar Mashhadi

Reputation: 4251

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

Upvotes: 3

milin4evr
milin4evr

Reputation: 1

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 :-)

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72284

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"))

Upvotes: 1

Related Questions