001package com.hfg.bio.seq.alignment.blast;
002
003
004import java.io.File;
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.List;
008
009import com.hfg.setting.*;
010import com.hfg.util.StringUtil;
011import com.hfg.util.collection.CollectionUtil;
012import com.hfg.xml.XMLTag;
013
014//==============================================================================
015/**
016 Settings used for BLAST search execution.
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// http://www.ncbi.nlm.nih.gov/books/NBK279675/
041
042public class BLAST_Settings extends Settings implements Cloneable
043{
044   public static final String EXECUTABLE_DIR      = "executable_dir";
045   public static final String BLAST_PROGRAM       = "blast_program";
046   public static final String BLAST_DATABASE      = "blast_db";
047   public static final String OUTPUT_FILENAME     = "output_filename";
048   public static final String NUM_DESCRIPTIONS    = "num_descriptions";
049   public static final String NUM_ALIGNMENTS      = "num_alignments";
050   public static final String NUM_THREADS         = "num_threads";
051   public static final String EVALUE              = "evalue";
052   public static final String HTML                = "html";
053   public static final String WORD_SIZE           = "word_size";
054   public static final String SOFT_MASKING        = "soft_masking";
055   public static final String COMMAND_LINE_PARAMS = "command_line_params";
056
057   //###########################################################################
058   // CONSTRUCTORS
059   //###########################################################################
060
061   //---------------------------------------------------------------------------
062   public BLAST_Settings()
063   {
064      super();
065   }
066
067   public BLAST_Settings(XMLTag inXMLTag)
068   {
069      super(inXMLTag);
070   }
071
072   //---------------------------------------------------------------------------
073   @Override
074   protected void init()
075   {
076      add(new StringSetting(EXECUTABLE_DIR));
077      add(new StringSetting(BLAST_PROGRAM));
078      add(new StringSetting(OUTPUT_FILENAME));
079      add(new StringListSetting(BLAST_DATABASE));
080      add(new IntSetting(NUM_DESCRIPTIONS));
081      add(new IntSetting(NUM_ALIGNMENTS));
082      add(new IntSetting(NUM_THREADS));
083      add(new IntSetting(WORD_SIZE));
084      add(new FloatSetting(EVALUE));
085      add(new BooleanSetting(HTML));
086      add(new BooleanSetting(SOFT_MASKING));
087      add(new StringSetting(COMMAND_LINE_PARAMS));
088   }
089
090   //###########################################################################
091   // PUBLIC METHODS
092   //###########################################################################
093
094   //---------------------------------------------------------------------------
095   public File getExecutableDir()
096   {
097      File executableDir = null;
098
099      String stringValue = get(EXECUTABLE_DIR).getStringValue();
100      if (StringUtil.isSet(stringValue))
101      {
102         executableDir = new File(stringValue);
103      }
104
105      return executableDir;
106   }
107
108   //---------------------------------------------------------------------------
109   @SuppressWarnings("unchecked")
110   public BLAST_Settings setExecutableDir(File inValue)
111   {
112      get(EXECUTABLE_DIR).setValue(inValue != null ? inValue.getAbsolutePath() : null);
113      return this;
114   }
115
116   //---------------------------------------------------------------------------
117   public BLAST_Program getBLAST_Program()
118   {
119      BLAST_Program program = null;
120
121      String stringValue = get(BLAST_PROGRAM).getStringValue();
122      if (StringUtil.isSet(stringValue))
123      {
124         program = BLAST_Program.valueOf(stringValue);
125      }
126
127      return program;
128   }
129
130   //---------------------------------------------------------------------------
131   @SuppressWarnings("unchecked")
132   public BLAST_Settings setBLAST_Program(BLAST_Program inValue)
133   {
134      get(BLAST_PROGRAM).setValue(inValue != null ? inValue.name() : null);
135      return this;
136   }
137
138
139
140   //---------------------------------------------------------------------------
141   public List<BLAST_Database> getBLAST_Databases()
142   {
143      List<BLAST_Database> dbs = null;
144
145      List<String> stringList = (List<String>) get(BLAST_DATABASE).getValue();
146      if (CollectionUtil.hasValues(stringList))
147      {
148         dbs = new ArrayList<>(stringList.size());
149         for (String dbString : stringList)
150         {
151            dbs.add(new BLAST_Database(dbString));
152         }
153      }
154
155      return dbs;
156   }
157
158   //---------------------------------------------------------------------------
159   @SuppressWarnings("unchecked")
160   public BLAST_Settings addBLAST_Database(BLAST_Database inValue)
161   {
162      if (inValue != null)
163      {
164         List<BLAST_Database> dbs = getBLAST_Databases();
165         if (null == dbs)
166         {
167            dbs = new ArrayList<>(1);
168         }
169
170
171         if (! dbs.contains(inValue))
172         {
173            dbs.add(inValue);
174         }
175
176         setBLAST_Databases(dbs);
177      }
178
179      return this;
180   }
181
182   //---------------------------------------------------------------------------
183   @SuppressWarnings("unchecked")
184   public BLAST_Settings setBLAST_Databases(Collection<BLAST_Database> inValues)
185   {
186      List<String> stringList = null;
187      if (CollectionUtil.hasValues(inValues))
188      {
189         stringList = new ArrayList<>(inValues.size());
190         for (BLAST_Database db : inValues)
191         {
192            stringList.add(db.getAbsolutePath());
193         }
194      }
195
196      get(BLAST_DATABASE).setValue(stringList);
197      return this;
198   }
199
200
201   //---------------------------------------------------------------------------
202   public File getOutputFile()
203   {
204      File file = null;
205
206      String stringValue = get(OUTPUT_FILENAME).getStringValue();
207      if (StringUtil.isSet(stringValue))
208      {
209         file = new File(stringValue);
210      }
211
212      return file;
213   }
214
215   //---------------------------------------------------------------------------
216   @SuppressWarnings("unchecked")
217   public BLAST_Settings setOutputFile(File inValue)
218   {
219      get(OUTPUT_FILENAME).setValue(inValue != null ? inValue.getAbsolutePath() : null);
220      return this;
221   }
222
223
224   //---------------------------------------------------------------------------
225   public Integer getNumDescriptions()
226   {
227      return (Integer) get(NUM_DESCRIPTIONS).getValue();
228   }
229
230   //---------------------------------------------------------------------------
231   /**
232    Show one-line descriptions for this number of database sequences.
233    @param inValue the number of description lines
234    @return this Settings object to enable chaining
235    */
236   @SuppressWarnings("unchecked")
237   public BLAST_Settings setNumDescriptions(Integer inValue)
238   {
239      get(NUM_DESCRIPTIONS).setValue(inValue);
240      return this;
241   }
242
243
244   //---------------------------------------------------------------------------
245   public Integer getNumAlignments()
246   {
247      return (Integer) get(NUM_ALIGNMENTS).getValue();
248   }
249
250   //---------------------------------------------------------------------------
251   /**
252    Show alignments for this number of database sequences.
253    @param inValue the number of alignments to show
254    @return this Settings object to enable chaining
255    */
256   @SuppressWarnings("unchecked")
257   public BLAST_Settings setNumAlignments(Integer inValue)
258   {
259      get(NUM_ALIGNMENTS).setValue(inValue);
260      return this;
261   }
262
263
264   //---------------------------------------------------------------------------
265   public Integer getWordSize()
266   {
267      return (Integer) get(WORD_SIZE).getValue();
268   }
269
270   //---------------------------------------------------------------------------
271   /**
272    From the BLAST documentation:<br />
273    "BLAST is a heuristic that works by finding word-matches between the query and database sequences.
274    One may think of this process as finding "hot-spots" that BLAST can then use to initiate extensions
275    that might eventually lead to full-blown alignments. For nucleotide-nucleotide searches (i.e., "blastn")
276    an exact match of the entire word is required before an extension is initiated, so that one normally
277    regulates the sensitivity and speed of the search by increasing or decreasing the word-size.
278    For other BLAST searches non-exact word matches are taken into account based upon the similarity between words."
279    @param inValue the word size to use
280    @return this Settings object to enable chaining
281    */
282   @SuppressWarnings("unchecked")
283   public BLAST_Settings setWordSize(Integer inValue)
284   {
285      get(WORD_SIZE).setValue(inValue);
286      return this;
287   }
288
289
290   //---------------------------------------------------------------------------
291   public Integer getNumThreads()
292   {
293      return (Integer) get(NUM_THREADS).getValue();
294   }
295
296   //---------------------------------------------------------------------------
297   /**
298    Number of threads (CPUs) to use in the BLAST search.
299    @param inValue the number of threads
300    @return this Settings object to enable chaining
301    */
302   @SuppressWarnings("unchecked")
303   public BLAST_Settings setNumThreads(Integer inValue)
304   {
305      get(NUM_THREADS).setValue(inValue);
306      return this;
307   }
308
309
310   //---------------------------------------------------------------------------
311   public Float getEValue()
312   {
313      return (Float) get(EVALUE).getValue();
314   }
315
316   //---------------------------------------------------------------------------
317   /**
318    Expect value (E) for saving hits.
319    @param inValue the e-value threshold
320    @return this Settings object to enable chaining
321    */
322   @SuppressWarnings("unchecked")
323   public BLAST_Settings setEValue(Float inValue)
324   {
325      get(EVALUE).setValue(inValue);
326      return this;
327   }
328
329
330   //---------------------------------------------------------------------------
331   public Boolean htmlOutput()
332   {
333      return (Boolean) get(HTML).getValue();
334   }
335
336   //---------------------------------------------------------------------------
337   /**
338    Produce HTML output.
339    @param inValue whether to produce HTML output or not
340    @return this Settings object to enable chaining
341    */
342   @SuppressWarnings("unchecked")
343   public BLAST_Settings setHTMLOutput(Boolean inValue)
344   {
345      get(HTML).setValue(inValue);
346      return this;
347   }
348
349
350   //---------------------------------------------------------------------------
351   public Boolean getSoftMasking()
352   {
353      return (Boolean) get(SOFT_MASKING).getValue();
354   }
355
356   //---------------------------------------------------------------------------
357   /**
358    Apply filtering locations as soft masks.
359    @param inValue whether to apply filtering locations as soft masks or not
360    @return this Settings object to enable chaining
361    */
362   @SuppressWarnings("unchecked")
363   public BLAST_Settings setSoftMasking(Boolean inValue)
364   {
365      get(SOFT_MASKING).setValue(inValue);
366      return this;
367   }
368
369
370   //---------------------------------------------------------------------------
371   public String getCommandLineParams()
372   {
373      return (String) get(COMMAND_LINE_PARAMS).getValue();
374   }
375
376   //---------------------------------------------------------------------------
377   /**
378    Additional (optional) command line parameters for the end of the BLAST command
379    @param inValue the additional command line parameters to be appended to the end of the command
380    @return this Settings object to enable chaining
381    */
382   @SuppressWarnings("unchecked")
383   public BLAST_Settings setCommandLineParams(String inValue)
384   {
385      get(COMMAND_LINE_PARAMS).setValue(inValue);
386      return this;
387   }
388
389}