determines the complement of a sequence of nucleotides. - Java Data Structure

Java examples for Data Structure:DNA

Description

determines the complement of a sequence of nucleotides.

Demo Code

/*/*from w  w w.  j  a va  2s .com*/
 **  DNAUtils
 **  (c) Copyright 1997, Neomorphic Sofware, Inc.
 **  All Rights Reserved
 **
 **  CONFIDENTIAL
 **  DO NOT DISTRIBUTE
 **
 **  File: DNAUtils.java
 **
 */
import java.util.List;
import java.util.ArrayList;

public class Main{
    /**
     * determines the complement of a sequence of nucleotides.
     *
     * @param buf a string of nucleotide codes
     *            each of which is replaced
     *            with it's complementary code.
     * @see #complement
     */
    protected static void complementBuffer(StringBuffer buf) {
        char base;
        for (int i = 0; i < buf.length(); i++) {
            base = buf.charAt(i);
            buf.setCharAt(i, complement(base));
        }
    }
    /**
     * determines the complement of a sequence of nucleotides.
     *
     * @param s a string of nucleotide codes.
     * @return the complementary codes.
     */
    public static String complement(String s) {
        if (s == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer(s);
        DNAUtils.complementBuffer(buf);
        return buf.toString();
    }
    /**
     * determines the complement of a nucleotide
     * 
     * @param base a character reperesenting a nucleotide
     * @return the character which represents the complement to the input base
     */
    public static char complement(char base) {
        char complement = base;
        if (base == 'a') {
            complement = 't';
        } else if (base == 'c') {
            complement = 'g';
        } else if (base == 'g') {
            complement = 'c';
        } else if (base == 't') {
            complement = 'a';
        } else if (base == 'A') {
            complement = 'T';
        } else if (base == 'C') {
            complement = 'G';
        } else if (base == 'G') {
            complement = 'C';
        } else if (base == 'T') {
            complement = 'A';
        }

        if ((complement == base) && (base != 'n') && (base != 'N'))
            throw new IllegalArgumentException(
                    "Could not find complement for '" + base + "'");

        return complement;
    }
}

Related Tutorials