All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.bio.seq.alignment.AlignedSeq Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.bio.seq.alignment;

import com.hfg.bio.HfgBioXML;
import com.hfg.bio.seq.BioSequence;
import com.hfg.bio.seq.SeqLocation;
import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 Base class for the aligned sequences that compose a pairwise sequence alignment.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding 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 abstract class AlignedSeq implements Cloneable { private BioSequence mSeq; private String mAlignedSeq; private SeqLocation mLocation; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public AlignedSeq(BioSequence inSeq, CharSequence inAlignedSeq, int inStart) { init(inSeq, inAlignedSeq, inStart); } //-------------------------------------------------------------------------- public AlignedSeq(XMLNode inXMLNode) { int start = Integer.parseInt(inXMLNode.getAttributeValue(HfgBioXML.START_ATT)); XMLNode seqTag = inXMLNode.getOptionalSubtagByName(HfgBioXML.QUERY); if (null == seqTag) { seqTag = inXMLNode.getOptionalSubtagByName(HfgBioXML.SUBJECT); } BioSequence seq = (seqTag != null && CollectionUtil.hasValues(seqTag.getSubtags()) ? BioSequence.instantiate((XMLNode) seqTag.getSubtags().get(0)) : null); init(seq, inXMLNode.getUnescapedContent(), start); } //--------------------------------------------------------------------------- private void init(BioSequence inSeq, CharSequence inAlignedSeq, int inStart) { mSeq = inSeq; mAlignedSeq = inAlignedSeq.toString(); mLocation = new SeqLocation(inStart, mAlignedSeq.length() - StringUtil.getCharCount(mAlignedSeq, '-') + inStart - 1); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public String toString() { StringBuilder buffer = new StringBuilder(); if (mLocation != null) { buffer.append(mLocation); } return buffer.toString(); } //-------------------------------------------------------------------------- public XMLNode toXMLNode() { XMLTag tag = new XMLTag(this instanceof AlignedQuery ? HfgBioXML.ALIGNED_QUERY : HfgBioXML.ALIGNED_SUBJECT); tag.setAttribute(HfgBioXML.START_ATT, mLocation.getStart()); tag.setContent(mAlignedSeq); if (mSeq != null) { XMLTag seqTag = new XMLTag(this instanceof AlignedQuery ? HfgBioXML.QUERY : HfgBioXML.SUBJECT); seqTag.addSubtag(mSeq.toXMLNode()); tag.addSubtag(seqTag); } return tag; } //--------------------------------------------------------------------------- @Override public AlignedSeq clone() { AlignedSeq cloneObj; try { cloneObj = (AlignedSeq) super.clone(); } catch (CloneNotSupportedException e) { throw new ProgrammingException(e); } if (mSeq != null) { cloneObj.mSeq = mSeq.clone(); } if (mLocation != null) { cloneObj.mLocation = mLocation.clone(); } return cloneObj; } //--------------------------------------------------------------------------- public AlignedSeq setSeq(BioSequence inValue) { mSeq = inValue; return this; } //--------------------------------------------------------------------------- public BioSequence getSeq() { return mSeq; } //--------------------------------------------------------------------------- public AlignedSeq setAlignedSeq(CharSequence inValue) { mAlignedSeq = inValue.toString(); return this; } //--------------------------------------------------------------------------- public String getAlignedSeq() { return mAlignedSeq; } //--------------------------------------------------------------------------- public SeqLocation getSeqLocation() { return mLocation; } //--------------------------------------------------------------------------- public int getIndexedPosition(int inLinearPosition) { int indexedPosition = 0; int linearPosition = 0; int residueCount = 0; int index = 0; for (char residue : mAlignedSeq.toCharArray()) { if (residue != '-') { residueCount++; } linearPosition = mLocation.getStart() + residueCount - 1 + (residue != '-' ? 0 : 1); if (linearPosition == inLinearPosition) { indexedPosition = index; break; } index++; } return indexedPosition; } //--------------------------------------------------------------------------- public int getLinearPosition(int inIndexedPosition) { int linearPosition = 0; int residueCount = 0; int index = 0; for (char residue : mAlignedSeq.toCharArray()) { if (residue != '-') { residueCount++; } if (index == inIndexedPosition) { linearPosition = mLocation.getStart() + residueCount - 1 + (residue != '-' ? 0 : 1); break; } index++; } return linearPosition; } //--------------------------------------------------------------------------- public int getNTerminalGapLength() { int gapLength = 0; for (int i = 0; i < mAlignedSeq.length(); i++) { if ('-' == mAlignedSeq.charAt(i)) { gapLength++; } else { break; } } return gapLength; } //--------------------------------------------------------------------------- public int getCTerminalGapLength() { int gapLength = 0; for (int i = mAlignedSeq.length() - 1; i > 0; i--) { if ('-' == mAlignedSeq.charAt(i)) { gapLength++; } else { break; } } return gapLength; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy