001package com.hfg.bio.seq.alignment; 002 003import java.util.HashMap; 004import java.util.Map; 005 006import com.hfg.bio.seq.BioSequenceType; 007import com.hfg.bio.seq.alignment.matrix.SubstitutionMatrix; 008import com.hfg.exception.ProgrammingException; 009import com.hfg.util.collection.SparseMatrix; 010 011 012//------------------------------------------------------------------------------ 013/** 014 Container for multiple sequence alignment settings. 015 <div> 016 @author J. Alex Taylor, hairyfatguy.com 017 </div> 018 */ 019//------------------------------------------------------------------------------ 020// com.hfg XML/HTML Coding Library 021// 022// This library is free software; you can redistribute it and/or 023// modify it under the terms of the GNU Lesser General Public 024// License as published by the Free Software Foundation; either 025// version 2.1 of the License, or (at your option) any later version. 026// 027// This library is distributed in the hope that it will be useful, 028// but WITHOUT ANY WARRANTY; without even the implied warranty of 029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030// Lesser General Public License for more details. 031// 032// You should have received a copy of the GNU Lesser General Public 033// License along with this library; if not, write to the Free Software 034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 035// 036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 037// jataylor@hairyfatguy.com 038//------------------------------------------------------------------------------ 039 040 041public class MSA_Settings implements Cloneable 042{ 043 private SubstitutionMatrix mMatrix; 044 private boolean mCaseInsensitive = false; 045 private int mKMerSize = sDefaultKMerSize; 046 047 private Map<PairwiseSeqType, GapPenalties> mGapPenaltyMap = new HashMap<>(2); 048 private SparseMatrix<PairwiseSeqType, PairwiseSeqTerminus, Boolean> mTerminalPenaltyMatrix = new SparseMatrix<>(); 049 050 private static int sDefaultKMerSize = 3; 051 private static GapPenalties sDefaultGapPenalties = new GapPenalties().setOpenPenalty(-5f).setExtensionPenalty(-2f); 052 private static Map<BioSequenceType, SubstitutionMatrix> sDefaultMatrixMap = new HashMap<>(2); 053 054 static 055 { 056 sDefaultMatrixMap.put(BioSequenceType.PROTEIN, SubstitutionMatrix.BLOSUM62); 057 sDefaultMatrixMap.put(BioSequenceType.NUCLEIC_ACID, SubstitutionMatrix.NUCLEOTIDE); 058 } 059 060 061 //########################################################################### 062 // CONSTRUCTORS 063 //########################################################################### 064 065 //--------------------------------------------------------------------------- 066 public MSA_Settings() 067 { 068 init(); 069 } 070 071 //--------------------------------------------------------------------------- 072 private void init() 073 { 074 setGapPenalties(sDefaultGapPenalties); 075 setPenalizeTerminalGaps(true); 076 } 077 078 //########################################################################### 079 // PUBLIC METHODS 080 //########################################################################### 081 082 //--------------------------------------------------------------------------- 083 public MSA_Settings setKMerSize(int inValue) 084 { 085 mKMerSize = inValue; 086 return this; 087 } 088 089 //--------------------------------------------------------------------------- 090 public int getKMerSize() 091 { 092 return mKMerSize; 093 } 094 095 //--------------------------------------------------------------------------- 096 public MSA_Settings setSubstitutionMatrix(SubstitutionMatrix inValue) 097 { 098 mMatrix = inValue; 099 return this; 100 } 101 102 //--------------------------------------------------------------------------- 103 public SubstitutionMatrix getSubstitutionMatrix(BioSequenceType inSeqType) 104 { 105 if (null == mMatrix) 106 { 107 mMatrix = sDefaultMatrixMap.get(inSeqType != null ? inSeqType : BioSequenceType.NUCLEIC_ACID); 108 } 109 110 return mMatrix; 111 } 112 113 114 115 //--------------------------------------------------------------------------- 116 /** 117 If specified as true, the scoring matrix's scoreCaseInsensitive() method is used. 118 @param inValue whether or not scoring is case insensitive 119 @return this settings object (for possible method chaining) 120 */ 121 public MSA_Settings setScoreCaseInsensitive(boolean inValue) 122 { 123 mCaseInsensitive = inValue; 124 return this; 125 } 126 127 //--------------------------------------------------------------------------- 128 public boolean getScoreCaseInsensitive() 129 { 130 return mCaseInsensitive; 131 } 132 133 134 //--------------------------------------------------------------------------- 135 /** 136 Sets the specified gap penalties for both the query and subject. 137 @param inValue GapPenalties to apply 138 @return this settings object (for possible method chaining) 139 */ 140 public MSA_Settings setGapPenalties(GapPenalties inValue) 141 { 142 setGapPenalties(PairwiseSeqType.QUERY, inValue.clone()); 143 setGapPenalties(PairwiseSeqType.SUBJECT, inValue.clone()); 144 return this; 145 } 146 147 //--------------------------------------------------------------------------- 148 public MSA_Settings setGapPenalties(PairwiseSeqType inPairwiseSeqType, GapPenalties inValue) 149 { 150 mGapPenaltyMap.put(inPairwiseSeqType, inValue); 151 return this; 152 } 153 154 //--------------------------------------------------------------------------- 155 public GapPenalties getGapPenalties(PairwiseSeqType inPairwiseSeqType) 156 { 157 return mGapPenaltyMap.get(inPairwiseSeqType); 158 } 159 160 //--------------------------------------------------------------------------- 161 public MSA_Settings setPenalizeTerminalGaps(boolean inValue) 162 { 163 setPenalizeTerminalGaps(PairwiseSeqType.QUERY, inValue); 164 setPenalizeTerminalGaps(PairwiseSeqType.SUBJECT, inValue); 165 return this; 166 } 167 168 //--------------------------------------------------------------------------- 169 public MSA_Settings setPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, boolean inValue) 170 { 171 setPenalizeTerminalGaps(inPairwiseSeqType, PairwiseSeqTerminus.LEFT, inValue); 172 setPenalizeTerminalGaps(inPairwiseSeqType, PairwiseSeqTerminus.RIGHT, inValue); 173 return this; 174 } 175 176 //--------------------------------------------------------------------------- 177 public MSA_Settings setPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, PairwiseSeqTerminus inPairwiseSeqTerminus, boolean inValue) 178 { 179 mTerminalPenaltyMatrix.put(inPairwiseSeqType, inPairwiseSeqTerminus, inValue); 180 return this; 181 } 182 183 //--------------------------------------------------------------------------- 184 public Boolean getPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, PairwiseSeqTerminus inPairwiseSeqTerminus) 185 { 186 return mTerminalPenaltyMatrix.get(inPairwiseSeqType, inPairwiseSeqTerminus); 187 } 188 189 190 //--------------------------------------------------------------------------- 191 @Override 192 public MSA_Settings clone() 193 { 194 MSA_Settings cloneObj; 195 try 196 { 197 cloneObj = (MSA_Settings) super.clone(); 198 } 199 catch (CloneNotSupportedException e) 200 { 201 throw new ProgrammingException(e); 202 } 203 204 return cloneObj; 205 } 206}