001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package demo;
022
023import org.biojava.nbio.alignment.Alignments;
024import org.biojava.nbio.alignment.Alignments.PairwiseSequenceAlignerType;
025import org.biojava.nbio.alignment.SimpleGapPenalty;
026import org.biojava.nbio.core.alignment.matrices.SubstitutionMatrixHelper;
027import org.biojava.nbio.alignment.template.GapPenalty;
028import org.biojava.nbio.alignment.template.PairwiseSequenceAligner;
029import org.biojava.nbio.core.alignment.template.SequencePair;
030import org.biojava.nbio.core.alignment.template.SubstitutionMatrix;
031import org.biojava.nbio.core.sequence.ProteinSequence;
032import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
033import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
034
035import java.net.URL;
036
037public class DemoAlignProteins {
038
039        public static void main(String[] args) throws Exception {
040
041                String uniprotID1 = "P69905";
042                String uniprotID2 = "P68871";
043
044                ProteinSequence s1 = getSequenceForId(uniprotID1);
045                ProteinSequence s2 = getSequenceForId(uniprotID2);
046
047                SubstitutionMatrix<AminoAcidCompound> matrix = SubstitutionMatrixHelper.getBlosum65();
048
049                GapPenalty penalty = new SimpleGapPenalty();
050
051                int gop = 8;
052                int extend = 1;
053                penalty.setOpenPenalty(gop);
054                penalty.setExtensionPenalty(extend);
055
056
057                PairwiseSequenceAligner<ProteinSequence, AminoAcidCompound> smithWaterman =
058                                Alignments.getPairwiseAligner(s1, s2, PairwiseSequenceAlignerType.LOCAL, penalty, matrix);
059
060                SequencePair<ProteinSequence, AminoAcidCompound> pair = smithWaterman.getPair();
061
062
063                System.out.println(pair.toString(60));
064
065
066        }
067
068        private static ProteinSequence getSequenceForId(String uniProtId) throws Exception {
069                URL uniprotFasta = new URL(String.format("http://www.uniprot.org/uniprot/%s.fasta", uniProtId));
070                ProteinSequence seq = FastaReaderHelper.readFastaProteinSequence(uniprotFasta.openStream()).get(uniProtId);
071                System.out.printf("id : %s %s%s%s", uniProtId, seq, System.getProperty("line.separator"), seq.getOriginalHeader());
072                System.out.println();
073
074                return seq;
075        }
076}