Java examples for Data Structure:DNA
determines the reverse complement of a sequence of nucleotides.
/*//www. j av a 2 s. co m ** 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 reverse complement of a sequence of nucleotides. * * @param s a string of nucleotide codes. * @return the complementary codes in reverse order. */ public static String reverseComplement(String s) { if (s == null) { return null; } StringBuffer buf = new StringBuffer(s.length()); int j = 0; for (int i = s.length() - 1; i >= 0; i--) { buf.append(s.charAt(i)); j++; } complementBuffer(buf); return buf.toString(); } /** * 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; } }