001package com.hfg.bio.seq; 002 003 004import java.io.InputStream; 005import java.io.InputStreamReader; 006import java.io.IOException; 007import java.io.Reader; 008 009import com.hfg.bio.taxonomy.ncbi.NCBITaxon; 010import com.hfg.util.CompareUtil; 011import com.hfg.util.io.StreamUtil; 012import com.hfg.xml.XMLNode; 013 014//------------------------------------------------------------------------------ 015/** 016 * Biological DNA or RNA sequence. 017 * 018 * @author J. Alex Taylor, hairyfatguy.com 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class NucleicAcid extends BioSequencePlusImpl 042{ 043 //########################################################################## 044 // PRIVATE FIELDS 045 //########################################################################## 046 047 private SeqQualityScores mQualityScores; 048 049 050 //########################################################################## 051 // CONSTRUCTORS 052 //########################################################################## 053 054 //-------------------------------------------------------------------------- 055 public NucleicAcid() 056 { 057 super(); 058 } 059 060 //-------------------------------------------------------------------------- 061 public NucleicAcid(XMLNode inXML) 062 { 063 super(inXML); 064 } 065 066 //########################################################################## 067 // PUBLIC METHODS 068 //########################################################################## 069 070 //-------------------------------------------------------------------------- 071 @Override 072 public NucleicAcid clone() 073 { 074 clearCalculatedProperties(); 075 076 NucleicAcid theClone = (NucleicAcid) super.clone(); 077 078 if (mQualityScores != null) 079 { 080 theClone.mQualityScores = mQualityScores.clone(); 081 } 082 083 return theClone; 084 } 085 086 //--------------------------------------------------------------------------- 087 @Override 088 public int compareTo(Object inObj2) 089 { 090 int result = -1; 091 092 if (this == inObj2) 093 { 094 result = 0; 095 } 096 else if (inObj2 != null 097 && inObj2 instanceof NucleicAcid) 098 { 099 NucleicAcid nucleicAcid2 = (NucleicAcid) inObj2; 100 101 // First compare the lengths 102 result = CompareUtil.compare(length(), nucleicAcid2.length()); 103 104 if (0 == result) 105 { 106 // Second compare the sequences themselves 107 result = CompareUtil.compare(getSequence(), nucleicAcid2.getSequence()); 108 } 109 } 110 111 return result; 112 } 113 114 //-------------------------------------------------------------------------- 115 public BioSequenceType getType() 116 { 117 return BioSequenceType.NUCLEIC_ACID; 118 } 119 120 //-------------------------------------------------------------------------- 121 @Override 122 public NucleicAcid setID(String inValue) 123 { 124 return (NucleicAcid) super.setID(inValue); 125 } 126 127 //-------------------------------------------------------------------------- 128 @Override 129 public NucleicAcid setDescription(CharSequence inValue) 130 { 131 return (NucleicAcid) super.setDescription(inValue); 132 } 133 134 135 //-------------------------------------------------------------------------- 136 @Override 137 public NucleicAcid setNCBITaxon(NCBITaxon inValue) 138 { 139 return (NucleicAcid) super.setNCBITaxon(inValue); 140 } 141 142 //-------------------------------------------------------------------------- 143 @Override 144 public NucleicAcid setSequence(CharSequence inValue) 145 { 146 return (NucleicAcid) super.setSequence(inValue); 147 } 148 149 //-------------------------------------------------------------------------- 150 public String getComplementSequence() 151 { 152 String outRevCompSeq = null; 153 154 try 155 { 156 outRevCompSeq = StreamUtil.inputStreamToString(getComplementSequenceStream()); 157 } 158 catch (IOException e) 159 { 160 throw new RuntimeException(e); 161 } 162 163 return outRevCompSeq; 164 } 165 166 //-------------------------------------------------------------------------- 167 public String getReverseSequence() 168 { 169 String outRevCompSeq = null; 170 171 try 172 { 173 outRevCompSeq = StreamUtil.inputStreamToString(getReverseSequenceStream()); 174 } 175 catch (IOException e) 176 { 177 throw new RuntimeException(e); 178 } 179 180 return outRevCompSeq; 181 } 182 183 //-------------------------------------------------------------------------- 184 public String getReverseComplementSequence() 185 { 186 String outRevCompSeq = null; 187 188 try 189 { 190 outRevCompSeq = StreamUtil.inputStreamToString(getReverseComplementSequenceStream()); 191 } 192 catch (IOException e) 193 { 194 throw new RuntimeException(e); 195 } 196 197 return outRevCompSeq; 198 } 199 200 //-------------------------------------------------------------------------- 201 public Reader getComplementSequenceReader() 202 { 203 InputStream seqStream = getComplementSequenceStream(); 204 return (null == seqStream ? null : new InputStreamReader(seqStream)); 205 } 206 207 //-------------------------------------------------------------------------- 208 protected Reader getReverseSequenceReader() 209 { 210 InputStream seqStream = getReverseSequenceStream(); 211 return (null == seqStream ? null : new InputStreamReader(seqStream)); 212 } 213 214 //-------------------------------------------------------------------------- 215 public Reader getReverseComplementSequenceReader() 216 { 217 InputStream seqStream = getReverseComplementSequenceStream(); 218 return (null == seqStream ? null : new InputStreamReader(seqStream)); 219 } 220 221 //-------------------------------------------------------------------------- 222 public InputStream getComplementSequenceStream() 223 { 224 InputStream seqStream = getSequenceStream(); 225 return (null == seqStream ? null : new NucleicAcidComplementFilterInputStream(seqStream)); 226 } 227 228 //-------------------------------------------------------------------------- 229 @Override 230 public InputStream getReverseSequenceStream() 231 { 232 return super.getReverseSequenceStream(); 233 } 234 235 //-------------------------------------------------------------------------- 236 public InputStream getReverseComplementSequenceStream() 237 { 238 InputStream seqStream = getReverseSequenceStream(); 239 return (null == seqStream ? null : new NucleicAcidComplementFilterInputStream(seqStream)); 240 } 241 242 //-------------------------------------------------------------------------- 243 public NucleicAcid setSeqQualityScores(SeqQualityScores inValue) 244 { 245 mQualityScores = inValue; 246 return this; 247 } 248 249 //-------------------------------------------------------------------------- 250 public SeqQualityScores getSeqQualityScores() 251 { 252 return mQualityScores; 253 } 254 255}