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 pairwise 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 PairwiseSettings implements Cloneable
042{
043   private SubstitutionMatrix mMatrix;
044   private Integer            mMaxLocalAlignmentResults = sDefaultMaxLocalAlignmentResults;
045   private Integer            mMinLocalAlignmentLength  = sDefaultMinLocalAlignmentLength;
046   private Integer            mMinQueryMatchLength  = sDefaultMinQueryMatchLength;
047   private Map<PairwiseSeqType, GapPenalties> mGapPenaltyMap = new HashMap<>(2);
048   private SparseMatrix<PairwiseSeqType, PairwiseSeqTerminus, Boolean> mTerminalPenaltyMatrix = new SparseMatrix<>();
049   private PairwiseAlignmentType mQueryAlignmentType   = sDefaultAlignmentType;
050   private PairwiseAlignmentType mSubjectAlignmentType = sDefaultAlignmentType;
051   private int                   mMinNumOfBackgroundCycles = sDefaultMinNumOfBackgroundCycles;
052   private Integer               mMinRawScore = sDefaultMinRawScore;
053   private boolean               mCaseInsensitive = false;
054   private boolean               mSubjectTemplateMode = false;
055   private boolean               mCalculateStatisticalScores = false;
056   private boolean               mDumpMatrices = false;
057
058
059   private static Integer            sDefaultMaxLocalAlignmentResults = 10;
060   private static Integer            sDefaultMinLocalAlignmentLength  = 4;
061   private static Integer            sDefaultMinQueryMatchLength  = 4;
062   private static Integer            sDefaultMinRawScore  = 1;
063   private static int                sDefaultMinNumOfBackgroundCycles = 50;
064   private static GapPenalties       sDefaultGapPenalties = new GapPenalties().setOpenPenalty(-5f).setExtensionPenalty(-2f);
065   private static Map<BioSequenceType, SubstitutionMatrix> sDefaultMatrixMap = new HashMap<>(2);
066   private static PairwiseAlignmentType sDefaultAlignmentType = PairwiseAlignmentType.LOCAL;
067
068   static
069   {
070      sDefaultMatrixMap.put(BioSequenceType.PROTEIN, SubstitutionMatrix.BLOSUM62);
071      sDefaultMatrixMap.put(BioSequenceType.NUCLEIC_ACID, SubstitutionMatrix.NUCLEOTIDE);
072   }
073
074
075   //###########################################################################
076   // CONSTRUCTORS
077   //###########################################################################
078
079   //---------------------------------------------------------------------------
080   public PairwiseSettings()
081   {
082      init();
083   }
084
085   //---------------------------------------------------------------------------
086   private void init()
087   {
088      setGapPenalties(sDefaultGapPenalties);
089      setPenalizeTerminalGaps(true);
090   }
091
092   //###########################################################################
093   // PUBLIC METHODS
094   //###########################################################################
095
096   //---------------------------------------------------------------------------
097   public PairwiseSettings setSubstitutionMatrix(SubstitutionMatrix inValue)
098   {
099      mMatrix = inValue;
100      return this;
101   }
102
103   //---------------------------------------------------------------------------
104   public SubstitutionMatrix getSubstitutionMatrix(BioSequenceType inSeqType)
105   {
106      if (null == mMatrix)
107      {
108         mMatrix = sDefaultMatrixMap.get(inSeqType != null ? inSeqType : BioSequenceType.NUCLEIC_ACID);
109      }
110
111      return mMatrix;
112   }
113
114   //---------------------------------------------------------------------------
115   public PairwiseSettings setMaxLocalAlignmentResults(Integer inValue)
116   {
117      mMaxLocalAlignmentResults = inValue;
118      return this;
119   }
120
121   //---------------------------------------------------------------------------
122   public Integer getMaxLocalAlignmentResults()
123   {
124      return mMaxLocalAlignmentResults;
125   }
126
127
128   //---------------------------------------------------------------------------
129   public PairwiseSettings setMinLocalAlignmentLength(Integer inValue)
130   {
131      mMinLocalAlignmentLength = inValue;
132      return this;
133   }
134
135   //---------------------------------------------------------------------------
136   public Integer getMinLocalAlignmentLength()
137   {
138      return mMinLocalAlignmentLength;
139   }
140
141
142   //---------------------------------------------------------------------------
143   /**
144    Specifies the minimum number of non-gap characters for the query in the alignment.
145    @param inValue the minimum number of non-gap characters for the query in the alignment
146    @return this settings object to facilitate method chaining
147    */
148   public PairwiseSettings setMinQueryMatchLength(Integer inValue)
149   {
150      mMinQueryMatchLength = inValue;
151      return this;
152   }
153
154   //---------------------------------------------------------------------------
155   public Integer getMinQueryMatchLength()
156   {
157      return mMinQueryMatchLength;
158   }
159
160
161   //---------------------------------------------------------------------------
162   public PairwiseSettings setMinRawScore(Integer inValue)
163   {
164      mMinRawScore = inValue;
165      return this;
166   }
167
168   //---------------------------------------------------------------------------
169   public Integer getMinRawScore()
170   {
171      return mMinRawScore;
172   }
173
174
175   //---------------------------------------------------------------------------
176   /**
177    If specified as true, query gaps are not penalized if they exist in the subject.
178    @param inValue whether or not to apply subject template mode scoring
179    @return this settings object (for possible method chaining)
180    */
181   public PairwiseSettings setSubjectTemplateMode(boolean inValue)
182   {
183      mSubjectTemplateMode = inValue;
184      return this;
185   }
186
187   //---------------------------------------------------------------------------
188   public boolean getSubjectTemplateMode()
189   {
190      return mSubjectTemplateMode;
191   }
192
193
194   //---------------------------------------------------------------------------
195   /**
196    If specified as true, the scoring matrix's scoreCaseInsensitive() method is used.
197    @param inValue whether or not scoring is case insensitive
198    @return this settings object (for possible method chaining)
199    */
200   public PairwiseSettings setScoreCaseInsensitive(boolean inValue)
201   {
202      mCaseInsensitive = inValue;
203      return this;
204   }
205
206   //---------------------------------------------------------------------------
207   public boolean getScoreCaseInsensitive()
208   {
209      return mCaseInsensitive;
210   }
211
212   //---------------------------------------------------------------------------
213   /**
214    Sets the specified gap penalties for both the query and subject.
215    @param inValue GapPenalties to apply
216    @return this settings object (for possible method chaining)
217    */
218   public PairwiseSettings setGapPenalties(GapPenalties inValue)
219   {
220      setGapPenalties(PairwiseSeqType.QUERY, inValue.clone());
221      setGapPenalties(PairwiseSeqType.SUBJECT, inValue.clone());
222      return this;
223   }
224
225   //---------------------------------------------------------------------------
226   public PairwiseSettings setGapPenalties(PairwiseSeqType inPairwiseSeqType, GapPenalties inValue)
227   {
228      mGapPenaltyMap.put(inPairwiseSeqType, inValue);
229      return this;
230   }
231
232   //---------------------------------------------------------------------------
233   public GapPenalties getGapPenalties(PairwiseSeqType inPairwiseSeqType)
234   {
235      return mGapPenaltyMap.get(inPairwiseSeqType);
236   }
237
238   //---------------------------------------------------------------------------
239   public PairwiseSettings setPenalizeTerminalGaps(boolean inValue)
240   {
241      setPenalizeTerminalGaps(PairwiseSeqType.QUERY, inValue);
242      setPenalizeTerminalGaps(PairwiseSeqType.SUBJECT, inValue);
243      return this;
244   }
245
246   //---------------------------------------------------------------------------
247   public PairwiseSettings setPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, boolean inValue)
248   {
249      setPenalizeTerminalGaps(inPairwiseSeqType, PairwiseSeqTerminus.LEFT, inValue);
250      setPenalizeTerminalGaps(inPairwiseSeqType, PairwiseSeqTerminus.RIGHT, inValue);
251      return this;
252   }
253
254   //---------------------------------------------------------------------------
255   public PairwiseSettings setPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, PairwiseSeqTerminus inPairwiseSeqTerminus, boolean inValue)
256   {
257      mTerminalPenaltyMatrix.put(inPairwiseSeqType, inPairwiseSeqTerminus, inValue);
258      return this;
259   }
260
261   //---------------------------------------------------------------------------
262   public Boolean getPenalizeTerminalGaps(PairwiseSeqType inPairwiseSeqType, PairwiseSeqTerminus inPairwiseSeqTerminus)
263   {
264      return mTerminalPenaltyMatrix.get(inPairwiseSeqType, inPairwiseSeqTerminus);
265   }
266
267   //---------------------------------------------------------------------------
268   public PairwiseSettings setAlignmentType(PairwiseAlignmentType inValue)
269   {
270      setQueryAlignmentType(inValue);
271      setSubjectAlignmentType(inValue);
272      return this;
273   }
274
275   //---------------------------------------------------------------------------
276   public PairwiseSettings setQueryAlignmentType(PairwiseAlignmentType inValue)
277   {
278      mQueryAlignmentType = inValue;
279      return this;
280   }
281
282   //---------------------------------------------------------------------------
283   public PairwiseAlignmentType getQueryAlignmentType()
284   {
285      return mQueryAlignmentType;
286   }
287
288   //---------------------------------------------------------------------------
289   public PairwiseSettings setSubjectAlignmentType(PairwiseAlignmentType inValue)
290   {
291      mSubjectAlignmentType = inValue;
292      return this;
293   }
294
295   //---------------------------------------------------------------------------
296   public PairwiseAlignmentType getSubjectAlignmentType()
297   {
298      return mSubjectAlignmentType;
299   }
300
301
302   //---------------------------------------------------------------------------
303   public PairwiseSettings setMinNumOfBackgroundCycles(int inValue)
304   {
305      mMinNumOfBackgroundCycles = inValue;
306      return this;
307   }
308
309   //---------------------------------------------------------------------------
310   public int getMinNumOfBackgroundCycles()
311   {
312      return mMinNumOfBackgroundCycles;
313   }
314
315   //---------------------------------------------------------------------------
316   /**
317    Specifies whether or not to calculate an E-value, P-value, and Z score by
318    generating background alignments.
319    @param inValue whether or not to calculate statistical measures for the alignment
320    @return this settings object (for possible method chaining)
321    */
322   public PairwiseSettings setCalculateStatisticalScores(boolean inValue)
323   {
324      mCalculateStatisticalScores = inValue;
325      return this;
326   }
327
328   //---------------------------------------------------------------------------
329   public boolean getCalculateStatisticalScores()
330   {
331      return mCalculateStatisticalScores;
332   }
333
334   //---------------------------------------------------------------------------
335   public PairwiseSettings setDumpMatrices(boolean inValue)
336   {
337      mDumpMatrices = inValue;
338      return this;
339   }
340
341   //---------------------------------------------------------------------------
342   public boolean getDumpMatrices()
343   {
344      return mDumpMatrices;
345   }
346
347   //---------------------------------------------------------------------------
348   @Override
349   public PairwiseSettings clone()
350   {
351      PairwiseSettings cloneObj;
352      try
353      {
354         cloneObj = (PairwiseSettings) super.clone();
355      }
356      catch (CloneNotSupportedException e)
357      {
358         throw new ProgrammingException(e);
359      }
360
361      return cloneObj;
362   }
363}