001package com.hfg.bio.seq; 002 003 004 005import java.util.Arrays; 006 007import com.hfg.exception.ProgrammingException; 008import com.hfg.util.StringBuilderPlus; 009 010//------------------------------------------------------------------------------ 011/** 012 Container for sequence quality scores. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class SeqQualityScores implements Cloneable 039{ 040 private SeqQualityScoreScheme mScheme; 041 private String mQualityString; 042 private short[] mQualityScores; 043 044 private static final String sQualityChars = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; 045 private static SeqQualityScoreScheme sDefaultScheme = SeqQualityScoreScheme.sanger; 046 047 //########################################################################### 048 // CONSTRUCTORS 049 //########################################################################### 050 051 //--------------------------------------------------------------------------- 052 public SeqQualityScores(String inEncodedQualityString) 053 { 054 this(inEncodedQualityString, sDefaultScheme); 055 } 056 057 //--------------------------------------------------------------------------- 058 public SeqQualityScores(String inEncodedQualityString, SeqQualityScoreScheme inScheme) 059 { 060 mQualityString = inEncodedQualityString; 061 mScheme = inScheme; 062 } 063 064 //--------------------------------------------------------------------------- 065 public SeqQualityScores(short[] inQualityScores) 066 { 067 this(inQualityScores, sDefaultScheme); 068 } 069 070 //--------------------------------------------------------------------------- 071 public SeqQualityScores(short[] inQualityScores, SeqQualityScoreScheme inScheme) 072 { 073 mQualityScores = inQualityScores; 074 mScheme = inScheme; 075 } 076 077 //########################################################################### 078 // PUBLIC METHODS 079 //########################################################################### 080 081 //-------------------------------------------------------------------------- 082 @Override 083 public SeqQualityScores clone() 084 { 085 SeqQualityScores theClone; 086 try 087 { 088 theClone = (SeqQualityScores) super.clone(); 089 } 090 catch (CloneNotSupportedException e) 091 { 092 throw new ProgrammingException(e); 093 } 094 095 theClone.mQualityScores = Arrays.copyOfRange(mQualityScores, 0, mQualityScores.length); 096 097 return theClone; 098 } 099 100 //--------------------------------------------------------------------------- 101 public SeqQualityScoreScheme getScheme() 102 { 103 return mScheme; 104 } 105 106 //--------------------------------------------------------------------------- 107 @Override 108 public String toString() 109 { 110 return getEncodedQualityString(); 111 } 112 113 //--------------------------------------------------------------------------- 114 public String getEncodedQualityString() 115 { 116 if (null == mQualityString 117 && mQualityScores != null) 118 { 119 mQualityString = encodeScores(mQualityScores); 120 } 121 122 return mQualityString; 123 } 124 125 //--------------------------------------------------------------------------- 126 public short[] getQualityScores() 127 { 128 if (null == mQualityScores 129 && mQualityString != null) 130 { 131 mQualityScores = decodeScores(mQualityString); 132 } 133 134 return mQualityScores; 135 } 136 137 //--------------------------------------------------------------------------- 138 private static String encodeScores(short[] inScores) 139 { 140 StringBuilderPlus buffer = new StringBuilderPlus(); 141 for (short score : inScores) 142 { 143 buffer.append(sQualityChars.charAt(score)); 144 } 145 146 return buffer.toString(); 147 } 148 149 //--------------------------------------------------------------------------- 150 private static short[] decodeScores(String inScoreString) 151 { 152 short[] scores = new short[inScoreString.length()]; 153 for (int index = 0; index < inScoreString.length(); index++) 154 { 155 char scoreChar = inScoreString.charAt(index); 156 157 scores[index] = (short) sQualityChars.indexOf(scoreChar); 158 } 159 160 return scores; 161 } 162}