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 * Created on Jul 2, 2012
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package demo;
026
027import org.biojava.nbio.alignment.Alignments;
028import org.biojava.nbio.core.alignment.template.Profile;
029import org.biojava.nbio.core.sequence.ProteinSequence;
030import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
031import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
032import org.biojava.nbio.core.util.ConcurrencyTools;
033
034import java.net.URL;
035import java.util.ArrayList;
036import java.util.List;
037
038public class CookbookMSA {
039
040        public static void main(String[] args) throws Exception {
041                String[] ids = new String[] {"Q21691", "A8WS47", "O48771"};
042                multipleSequenceAlignment(ids);
043        }
044
045        private static void multipleSequenceAlignment(String[] ids) throws Exception {
046                List<ProteinSequence> lst = new ArrayList<ProteinSequence>();
047                for (String id : ids) {
048                        lst.add(getSequenceForId(id));
049                }
050                Profile<ProteinSequence, AminoAcidCompound> profile = Alignments.getMultipleSequenceAlignment(lst);
051                System.out.printf("Clustalw:%s%s", System.getProperty("line.separator"), profile);
052                System.out.println();
053
054                ConcurrencyTools.shutdown();
055        }
056
057        private static ProteinSequence getSequenceForId(String uniProtId) throws Exception {
058                URL uniprotFasta = new URL(String.format("http://www.uniprot.org/uniprot/%s.fasta", uniProtId));
059                System.out.println("Getting Sequence from URL: "+ uniprotFasta);
060
061                ProteinSequence seq = FastaReaderHelper.readFastaProteinSequence(uniprotFasta.openStream()).get(uniProtId);
062                System.out.printf("id : %s %s%s%s", uniProtId, seq, System.getProperty("line.separator"), seq.getOriginalHeader());
063                System.out.println();
064
065                return seq;
066        }
067
068}