<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.jstacs.de/index.php?action=history&amp;feed=atom&amp;title=Dessert%3A_Alignments%2C_Utils%2C_and_goodies</id>
	<title>Dessert: Alignments, Utils, and goodies - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.jstacs.de/index.php?action=history&amp;feed=atom&amp;title=Dessert%3A_Alignments%2C_Utils%2C_and_goodies"/>
	<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;action=history"/>
	<updated>2026-04-04T14:04:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;diff=525&amp;oldid=prev</id>
		<title>Grau at 14:26, 2 February 2012</title>
		<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;diff=525&amp;oldid=prev"/>
		<updated>2012-02-02T14:26:59Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;amp;diff=525&amp;amp;oldid=494&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Grau</name></author>
	</entry>
	<entry>
		<id>https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;diff=494&amp;oldid=prev</id>
		<title>Grau: Created page with &quot;   In this section, we present a motley composition of interesting classes of Jstacs.  == Alignments ==  In this subsection, we present how to compute Alignments using Jstacs.  I...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.jstacs.de/index.php?title=Dessert:_Alignments,_Utils,_and_goodies&amp;diff=494&amp;oldid=prev"/>
		<updated>2012-02-01T22:57:17Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;   In this section, we present a motley composition of interesting classes of Jstacs.  == Alignments ==  In this subsection, we present how to compute Alignments using Jstacs.  I...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this section, we present a motley composition of interesting classes of Jstacs.&lt;br /&gt;
&lt;br /&gt;
== Alignments ==&lt;br /&gt;
&lt;br /&gt;
In this subsection, we present how to compute Alignments using Jstacs.&lt;br /&gt;
&lt;br /&gt;
If we like to compute an alignment, we first have to define the costs for match, mismatch, and gaps. In Jstacs, we provide the interface [http://www.jstacs.de/api-2.0//de/jstacs/algorithms/alignment/cost/Costs.html Costs] that declares all necessary method used during the alignment. In this example, we restrict to simple costs that are 0 for match, 1 for match, 0.5 for a gap.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		Costs costs = new SimpleCosts( 0, 1, 0.5 );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Second, we have to provide an instance of [http://www.jstacs.de/api-2.0//de/jstacs/algorithms/alignment/Alignment.html Alignment]. This instance contains all information needed for an alignment and stores for instance matrices used for dynamic programming. When creating an instance, we have to specify which kind of alignment we like to have. Jstacs supports local, global and semi-global alignments (cf. [http://www.jstacs.de/api-2.0//de/jstacs/algorithms/alignment/Alignment.AlignmentType.html Alignment.AlignmentType]).  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		Alignment align = new Alignment( AlignmentType.GLOBAL, costs );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In second constructor it is also possible to specify the number of off-diagonals to be used in the alignment leading to a potential speedup. &lt;br /&gt;
&lt;br /&gt;
Finally, we can compute the optimal alignment between two [http://www.jstacs.de/api-2.0//de/jstacs/data/sequences/Sequence.html Sequence] s and write the result to the standard output.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		System.out.println( align.getAlignment( seq1, seq2 ) );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The alignment instance can be reused for aligning further sequences.&lt;br /&gt;
&lt;br /&gt;
In Jstacs, we also provide the possibility of computing optimal alignments with affine gap costs. For this reason, we implement the class [http://www.jstacs.de/api-2.0//de/jstacs/algorithms/alignment/cost/AffineCosts.html AffineCosts] that is used to specify the cost for a gap opening. The costs for gap elongation are given by the gap costs of the internally used instance of [http://www.jstacs.de/api-2.0//de/jstacs/algorithms/alignment/cost/Costs.html Costs].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
		costs = new AffineCosts( 1, costs );&lt;br /&gt;
		align = new Alignment( AlignmentType.GLOBAL, costs );&lt;br /&gt;
		System.out.println( align.getAlignment( seq1, seq2 ) );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== REnvironment: Connection to R ==&lt;br /&gt;
&lt;br /&gt;
In this subsection, we show how to access R (cf. [http://www.r-project.org/ http://www.r-project.org/]) from Jstacs. R is a project for statistical computing that allows for performing complex computations and creating nice plots.&lt;br /&gt;
&lt;br /&gt;
In some cases, it is reasonable to use R from within Jstacs. To do so, we have to create a connection to R. We utilize the package &amp;lt;code&amp;gt;Rserve&amp;lt;/code&amp;gt; (cf. [http://www.rforge.net/Rserve/ http://www.rforge.net/Rserve/]) of R that allows to communicate between Java and R. Having a running instance of &amp;lt;code&amp;gt;Rserve&amp;lt;/code&amp;gt;, we can create a connection via  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		REnvironment re = new REnvironment();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However, in some cases we have to specify the login name, a password, and the port for the communication which is possible via alternative constructors.&lt;br /&gt;
&lt;br /&gt;
Now, we are able to do diverse things in R. Here, we only present three methods, but [http://www.jstacs.de/api-2.0//de/jstacs/utils/REnvironment.html REnvironment] provides more functionality. First, we copy an array of &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;s from Java to R &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		re.createVector( &amp;quot;values&amp;quot;, values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and second, we modify it &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		re.voidEval( &amp;quot;values=rnorm(length(values));&amp;quot; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, the [http://www.jstacs.de/api-2.0//de/jstacs/utils/REnvironment.html REnvironment] allows to create plots as PDF, TeX, or &amp;lt;code&amp;gt;BufferedImage&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		re.plotToPDF( &amp;quot;plot(values,t=\&amp;quot;l\&amp;quot;);&amp;quot;, &amp;quot;values.pdf&amp;quot;, true );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ArrayHandler: Handling arrays ==&lt;br /&gt;
&lt;br /&gt;
In this subsection, we present a way to easily handle arrays in Java, i.e., to cast, clone, and create arrays with elements of generic type. To this end, we implement the class [http://www.jstacs.de/api-2.0//de/jstacs/io/ArrayHandler.html ArrayHandler] in Jstacs. &lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s assume we have a two dimensional array of either primitives of some Java class and we like to create a deep clone as it is necessary for member fields in clone methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		double[][] twodim = new double[5][5];&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Traditionally, we would have to implement &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loops to do so. However, the [http://www.jstacs.de/api-2.0//de/jstacs/io/ArrayHandler.html ArrayHandler] implements this functionality in a generic manner providing one method for this purpose.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		double[][] clone = ArrayHandler.clone( twodim );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A second use case, is the creation of arrays, where each and every entry is a clone of some instance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		TrainableStatisticalModel pwm = TrainableStatisticalModelFactory.createPWM( DNAAlphabetContainer.SINGLETON, 10, 4.0 );&lt;br /&gt;
		TrainableStatisticalModel[] models = ArrayHandler.createArrayOf( pwm, 10 );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The third use case is to cast an array. Even if all elements of the array are from the same class, the component type of the array might be different (some super class). A simple cast will fail in those cases. However, the [http://www.jstacs.de/api-2.0//de/jstacs/io/ArrayHandler.html ArrayHandler] provides two methods for casting arrays. Here, we present the more important method, which allows to specify the array component type and performs the cast operation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		Object[] m = new Object[]{&lt;br /&gt;
		    TrainableStatisticalModelFactory.createPWM( DNAAlphabetContainer.SINGLETON, 10, 4.0 ),&lt;br /&gt;
		    TrainableStatisticalModelFactory.createHomogeneousMarkovModel( DNAAlphabetContainer.SINGLETON, 40.0, (byte)0 )&lt;br /&gt;
		};&lt;br /&gt;
		TrainableStatisticalModel[] sms = ArrayHandler.cast( TrainableStatisticalModel.class, models );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ToolBox ==&lt;br /&gt;
&lt;br /&gt;
The class [http://www.jstacs.de/api-2.0//de/jstacs/utils/ToolBox.html ToolBox] contains several static methods for recurring tasks. &lt;br /&gt;
For example, you can compute the maximum of an array of &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		double max = ToolBox.max( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or the sum of the values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		double sum = ToolBox.sum( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or you can obtain the index of the first maximum value in the provided array&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		int maxIndex = ToolBox.getMaxIndex( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Normalisation ==&lt;br /&gt;
&lt;br /&gt;
Another frequently needed functionality is the handling of log-values. Assume that we have an array &amp;lt;code&amp;gt;values&amp;lt;/code&amp;gt; containing a number of log-probabilities &amp;lt;math&amp;gt;l_i&amp;lt;/math&amp;gt;. What we want to compute is the logarithm of the sum of the original probabilities, i.e., &amp;lt;math&amp;gt;\log(\sum_i \exp(l_i))&amp;lt;/math&amp;gt;. The naive computation of this sum often results in numerical problems, especially, if the original probabilities are very different.&lt;br /&gt;
A more exact solution is provided by the static method &amp;lt;code&amp;gt;getLogSum&amp;lt;/code&amp;gt; of the class [http://www.jstacs.de/api-2.0//de/jstacs/utils/Normalisation.html Normalisation], which can be accessed by calling&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		double logSum = Normalisation.getLogSum( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, this method does not only work for probabilities, but for general log-values.&lt;br /&gt;
&lt;br /&gt;
Sometimes, we also want to normalize the given probabilities. That means, given the log-probabilities &amp;lt;math&amp;gt;l_i&amp;lt;/math&amp;gt;, we want to obtain normalized probabilities &amp;lt;math&amp;gt;p_i = \frac{\exp(l_i)}{\sum_j \exp(l_j)}&amp;lt;/math&amp;gt;. This normalization is performed by calling&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		Normalisation.logSumNormalisation( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and after the call, the array &amp;lt;code&amp;gt;values&amp;lt;/code&amp;gt; contains the normalized probabilities (not log-probabilities!).&lt;br /&gt;
&lt;br /&gt;
Finally, we might want to do the same for probabilities, i.e. given probabilities &amp;lt;math&amp;gt;q_i&amp;lt;/math&amp;gt; in an array &amp;lt;code&amp;gt;values&amp;lt;/code&amp;gt;, we want to compute &amp;lt;math&amp;gt;p_i = \frac{q_i}{\sum_j q_j}&amp;lt;/math&amp;gt; using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		Normalisation.sumNormalisation( values );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A typical application for the last two methods are (log) joint probabilities that we want use to compute conditional probabilities by dividing by a marginal probability.&lt;br /&gt;
&lt;br /&gt;
== Goodies ==&lt;br /&gt;
The class [http://www.jstacs.de/api-2.0//de/jstacs/utils/SafeOutputStream.html SafeOutputStream] is a simple way to switch between writing outputs of a program to standard out, to a file, or to completely suppress output. This class is basically a wrapper for other output streams that can handle &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; values. You can create a [http://www.jstacs.de/api-2.0//de/jstacs/utils/SafeOutputStream.html SafeOutputStream] writing to standard out with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		OutputStream stream = SafeOutputStream.getSafeOutputStream( System.out );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you provided &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; to the factory method instead, output would be suppressed, while no modifications in code using this [http://www.jstacs.de/api-2.0//de/jstacs/utils/SafeOutputStream.html SafeOutputStream] †would be necessary.&lt;br /&gt;
&lt;br /&gt;
Finally, the class [http://www.jstacs.de/api-2.0//de/jstacs/utils/SubclassFinder.html SubclassFinder] can be used to search for all subclasses of a given class in a specified package and its sub-packages. For example, if we want to find all concrete sub-classes of [http://www.jstacs.de/api-2.0//de/jstacs/sequenceScores/statisticalModels/trainable/TrainableStatisticalModel.html TrainableStatisticalModel], i.e., classes that are not abstract and can be instantiated, in all sub-packages of &amp;lt;code&amp;gt;de.jstacs&amp;lt;/code&amp;gt;, we call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java5&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
		LinkedList&amp;lt;Class&amp;lt;? extends TrainableStatisticalModel&amp;gt;&amp;gt; list = SubclassFinder.findInstantiableSubclasses( TrainableStatisticalModel.class, &amp;quot;de.jstacs&amp;quot; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and obtain a linked list containing all such classes. Other methods in [http://www.jstacs.de/api-2.0//de/jstacs/utils/SubclassFinder.html SubclassFinder] allow for searching for general sub-types including interfaces and abstract classes, or for filtering the results by further required interfaces.&lt;/div&gt;</summary>
		<author><name>Grau</name></author>
	</entry>
</feed>