-3

How could I have every three letters of this string: ATGCCACTATGGTAG to be saved in an array, with a comma separating every three letters. This is what I have: The parameter sequence is the above jumble of letters.

public static void listCodons(String sequence){
      int length = sequence.length();
      String[] listOfCodons = new String[length]; 
      for(int i = 0; i< length; i++){
            //This is where I'm not sure what to do
            listOfCodons = sequence[i]+sequence[i+1]+sequence[i+2]; 
         } 
      }
      System.out.print(Arrays.toString(listOfCodons));
}
Marina Claire
  • 103
  • 1
  • 1
  • 11

5 Answers5

1

First of all, this is syntactically incorrect code, there are many errors. The first one is two } after for loop, second one detonating end of method, so the System.out.print after that is out of scope of listCodons method, hence it's an error.

The second mistake is in java, characters in string can't be accessed with [index], you have to use .charAt(index) instead.

If I understood your problem correctly, you want your parameter ATGCCACTATGGTAG to become an array of ["ATG", "CCA", "CTA", "TGG", "TAG"]. If that's the case, here is the solution, with fixed problems:

public static void listCodons(String sequence) {
    int length = sequence.length();
    String[] listOfCodons = new String[length / 3];
    for (int i = 0; i < listOfCodons.length; i++) {
        listOfCodons[i] = sequence.substring(i * 3, i * 3 + 3);
    }
    System.out.print(Arrays.toString(listOfCodons));
}

Use 1/3 of length of sting, since we take 3 characters at a time. i * 3 is current position at string times 3 to get to correct beginning of the tag and i * 3 + 3 is to take next 3 characters from this position. Only problem here is if the tag length isn't consistent, but there is no information about that.

Marko Gresak
  • 7,950
  • 5
  • 40
  • 46
  • this makes an array out of bounds exception – Marina Claire Feb 05 '15 at 07:29
  • @MarinaClaire how? I tested it, there is no way there is array out of bounds exception. I said it will not give expected output for inconsistent lengths, but it works for arbitrary string length, I tested it for lengths of 0 to 1M, it's not throwing any exceptions. Are you sure you're using exact code as posting here and that it's not something else you're doing? – Marko Gresak Feb 05 '15 at 07:36
0

Based on the link posted by @shikjohari answer on SO you could solve it like

public class Split {

    public static void listCodons(String sequence) {
        String[] listOfCodons = sequence.split("(?<=\\G...)");
        System.out.print(Arrays.toString(listOfCodons));
    }

    public static void main(String[] args) throws Exception {
        listCodons("ATGCCACTATGGTAG");
    }
}

Explanation copied from the mentioned link: The regex (?<=\G...) matches an empty string that has the last match (\G) followed by three characters (...) before it ((?<= )).

Community
  • 1
  • 1
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

More smarter and easier to read is the way with the Guava Libraries

That's the code:

public static void listCodons(String sequence)
{
    Iterable<String> pieces = Splitter.fixedLength(3).split(sequence);

    // note this is Java 8 Lambda Syntax
    pieces.forEach(pi -> System.out.println(pi));

    // If you can not use Java 8 take the following:
    for (String piece : pieces)
    {
        System.out.println(piece);
    }
}
Manu Zi
  • 2,300
  • 6
  • 33
  • 67
  • Might be a bit easier to read, but harder to understand. Also, `Splitter` is not supported in standard java libraries, it's a part of [guava](https://github.com/google/guava). – Marko Gresak Feb 05 '15 at 07:20
  • What is harder to understand on this snippet? She does not have it limited to use only the standard java library. – Manu Zi Feb 05 '15 at 07:28
  • It's not for me, but could easily be for someone new, I doubt someone new to java will jump right into new stuff of java 8. And why would one use a library just to split a string? Oh right, we're talking about java, better import 10 libraries just to get my simple test program working. – Marko Gresak Feb 05 '15 at 07:32
  • do you know about effective java? https://prezi.com/hbzxzub1ckhp/effective-java-item-47/ had she said it is for a "test program". don't get mad bro ;-) – Manu Zi Feb 05 '15 at 07:38
  • I agree on using the libraries, but people learn to use libraries to do the smallest things and the project just grows. Learn *when* to use them, not just to use them. If you can do it with 2 more lines of native code, why add a whole library? I might be too much of DIY person, but the mirror side of this are programmers that create a dependancy hell in no time. Good luck managing project like that. – Marko Gresak Feb 05 '15 at 07:44
0

Let's assume that the length of your sequence is 9, and you want to group every three characters. This gives you the following grouping:

  • characters 0, 1, and 2;
  • characters 3, 4, and 5;
  • characters 6, 7, and 8.

Notice how the first character in each group is indexed by a multiple of 3. You can use this idea in your code.

public static void listCodons(String sequence){
      /** If your sequence has n characters, then
      /*  dividing it into groups of three results in
      /*  n/3 groups. */
      int length = sequence.length();
      String[] listOfCodons = new String[length/3];

      for(int i = 0; i< length/3; i++) {
          /** Remember: the first character of each codon
          /*  is indexed in the sequence by a multiple
          /*  of three. */
          listOfCodons[i] = sequence.substring(3*i, 3*(i+1));
      }
      System.out.print(Arrays.toString(listOfCodons));
}

So what is going on? Let's use your original example ATGCCACTATGGTAG. The sequence has a length of 15. Thus, we will get in the end 15/3=5 codons. Each codon i will consist of the characters indexed by 3*i, 3*i+1, and 3*i+2. So you take the corresponding substring and add it to the list of codons.

Hope this helps.

jadhachem
  • 1,123
  • 2
  • 11
  • 19
0
Try this code :

public static void listCodons(String sequence){
        int length = (sequence.length()/3);
        String[] listOfCodons = new String[length]; 
        int j=0,i=0;
        while(i<sequence.length() && j<listOfCodons.length){
            listOfCodons[j++]=sequence.substring(i, i+3);
            i+=3;
        }
        System.out.println(Arrays.toString(listOfCodons));
    }
Amulya Koppula
  • 150
  • 1
  • 5