com.hfg.bio.phylogeny.UncorrectedModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.bio.phylogeny;
import com.hfg.bio.seq.BioSequence;
//------------------------------------------------------------------------------
/**
Distance matrix calculation method that is simply the fraction of mismatches.
distance = mismatches / aligned_length
If both sequences contain a gap at a given position, it is not counted towards
the alignment length.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class UncorrectedModel implements DistanceMatrixModel
{
//---------------------------------------------------------------------------
public UncorrectedModel()
{
}
//---------------------------------------------------------------------------
/**
Returns the model name as required by the DistanceMatrixModel interface.
*/
public String name()
{
return "Uncorrected";
}
//---------------------------------------------------------------------------
/**
Calculates the distance score for a pair of sequences.
The sequences must be aligned to be the same length.
If both sequences contain a gap at a given position, it is not counted towards
the alignment length.
*/
public float calculateDistance(BioSequence inSeq1, BioSequence inSeq2)
{
if (inSeq1.length() != inSeq2.length())
{
throw new RuntimeException("The length of seq1 [" + inSeq1.length() + "] and seq2 [" + inSeq2.length() + "] don't match!");
}
int mismatches = 0;
int length = 0;
String seq1 = inSeq1.getSequence().toUpperCase();
String seq2 = inSeq2.getSequence().toUpperCase();
// This way will avoid a NaN result from comparing two all-gapped sequences.
float distance = 0.0f;
if (! seq1.equals(seq2))
{
for (int i = 0; i < seq1.length(); i++)
{
char char1 = seq1.charAt(i);
char char2 = seq2.charAt(i);
if (char1 != char2) mismatches++;
// If both seqs are gapped at this position, don't count it against length.
if (!(char1 == '-' && char2 == '-')) length++;
}
distance = mismatches / (float) length;
}
return distance;
}
}