集册 Java实例教程 改造人类

改造人类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

383
将人类可读的DNA序列转换为内部0..4字节。

/*

 * 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.

 */


public class Main{
/** 
 来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

    /**

     * Transform a human-readable DNA sequence into internal 0..4 bytes.

     * @param str Eg. <code>"ACGTN"</code> will become {1,2,3,4,0}.

     * @return the encoded array

     */

    public static byte[] encodeString(final String str) {

        return encodeArray(str.getBytes());

    }

    /**

     * 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':/**n o w  j a v a  . c o m**/

                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;

            }

        }

        
展开阅读全文