Transform (in-situ) a human-readable DNA sequence into internal 0..4 bytes. - Java Data Structure

Java examples for Data Structure:DNA

Description

Transform (in-situ) a human-readable DNA sequence into internal 0..4 bytes.

Demo Code

/*//  w w w  . j ava 2  s. co  m
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement
 * for Academic Non-commercial Research Purposes only.
 *
 * If you did not receive a license accompanying this file, a copy must first be obtained by email
 * from support@realtimegenomics.com.  On downloading, using and/or continuing to use this source
 * code you accept the terms of that license agreement and any amendments to those terms that may
 * be made from time to time by Real Time Genomics Limited.
 */
//package com.java2s;

public class Main {
    /**
     * Transform (in-situ) a human-readable DNA sequence into internal 0..4 bytes.
     * @param a Eg. {'a','c','g','t','n'} will become {1,2,3,4,0}.
     * @param length length to convert
     * @return the encoded array (which will be the same array as <code>a</code>)
     */
    public static byte[] encodeArray(final byte[] a, final int length) {
        for (int k = 0; k < length; k++) {
            switch (a[k]) {
            case (byte) 'a':
            case (byte) 'A':
                a[k] = 1;
                break;
            case (byte) 'c':
            case (byte) 'C':
                a[k] = 2;
                break;
            case (byte) 'g':
            case (byte) 'G':
                a[k] = 3;
                break;
            case (byte) 't':
            case (byte) 'T':
                a[k] = 4;
                break;
            default:
                a[k] = 0;
                break;
            }
        }
        return a;
    }

    /**
     * Transform (in-situ) a human-readable DNA sequence into internal 0..4 bytes.
     * @param a Eg. {'a','c','g','t','n'} will become {1,2,3,4,0}.
     * @return the encoded array (which will be the same array as <code>a</code>)
     */
    public static byte[] encodeArray(final byte[] a) {
        return encodeArray(a, a.length);
    }
}

Related Tutorials