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 org.biojava.nbio.structure.align.gui;
022
023import org.biojava.nbio.structure.Atom;
024import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol;
025import org.biojava.nbio.structure.align.model.AFPChain;
026import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
027import org.biojava.nbio.structure.align.multiple.MultipleAlignmentEnsemble;
028import org.biojava.nbio.structure.align.util.AtomCache;
029import org.biojava.nbio.structure.align.util.UserConfiguration;
030import org.biojava.nbio.structure.align.webstart.WebStartMain;
031import org.biojava.nbio.structure.align.xml.AFPChainXMLParser;
032import org.biojava.nbio.structure.align.xml.MultipleAlignmentXMLParser;
033import org.biojava.nbio.core.util.InputStreamProvider;
034
035import javax.swing.*;
036
037import java.awt.event.ActionEvent;
038import java.awt.event.ActionListener;
039import java.io.BufferedReader;
040import java.io.File;
041import java.io.InputStream;
042import java.io.InputStreamReader;
043import java.util.List;
044
045/**
046 * Loads an alignment in an XML format and displays its content in a
047 * new Jmol panel. Can handle both alignment formats: AFPChain and
048 * MultipleAlignment.
049 * <p>
050 * All the alignments stored in the File are displayed, not only the first
051 * one. However, all the alignments in the same file have to be in the same
052 * format (either AFPChain or MultipleAlignment).
053 * Multiple Jmol panels can be created for that purpose.
054 *
055 * @author Aleix Lafita
056 * @version 2.0 - adapted for MultipleAlignments
057 *
058 */
059public class MyAlignmentLoadListener implements ActionListener {
060
061        @Override
062        public void actionPerformed(ActionEvent evt) {
063
064                final JFileChooser fc = new JFileChooser();
065
066                //in response to a button click
067                int returnVal = fc.showOpenDialog(null);
068
069                if (returnVal == JFileChooser.APPROVE_OPTION) {
070
071                        File file = fc.getSelectedFile();
072                        try {
073
074                                InputStreamProvider ip = new InputStreamProvider();
075                                InputStream stream = ip.getInputStream(file);
076                                BufferedReader in = new BufferedReader(
077                                                new InputStreamReader(stream));
078
079                                StringBuffer input = new StringBuffer();
080                                String str;
081                                while ((str = in.readLine()) != null) {
082                                        input.append(str);
083                                }
084                                in.close();
085
086                                String xml = input.toString();
087
088                                //Determine the format of the file
089                                if (xml.contains("MultipleAlignmentEnsemble")){
090
091                                        List<MultipleAlignmentEnsemble> ensembles =
092                                                        MultipleAlignmentXMLParser.parseXMLfile(xml);
093
094                                        //Display all ensembles, and all its alignments
095                                        for (MultipleAlignmentEnsemble e:ensembles){
096                                                for (MultipleAlignment msa:e.getMultipleAlignments()){
097                                                        MultipleAlignmentJmolDisplay.display(msa);
098                                                }
099                                        }
100
101                                }
102                                else {
103
104                                        AFPChain[] afps = AFPChainXMLParser.parseMultiXML(xml);
105
106                                        UserConfiguration conf = WebStartMain.getWebStartConfig();
107                                        AtomCache cache = new AtomCache(
108                                                        conf.getPdbFilePath(),conf.getCacheFilePath());
109
110                                        for (AFPChain afpChain:afps){
111                                                Atom[] ca1 = cache.getAtoms(afpChain.getName1());
112                                                Atom[] ca2 = cache.getAtoms(afpChain.getName2());
113
114                                                AFPChainXMLParser.rebuildAFPChain(afpChain, ca1, ca2);
115                                                StructureAlignmentJmol jmol =
116                                                                StructureAlignmentDisplay.display(
117                                                                                afpChain, ca1, ca2);
118
119                                                DisplayAFP.showAlignmentPanel(afpChain, ca1,ca2,jmol);
120                                        }
121                                }
122                        } catch (Exception e){
123                                e.printStackTrace();
124                                JOptionPane.showMessageDialog(null,"Could not load alignment "
125                                                + "file. Exception: " + e.getMessage());
126                        }
127                }
128        }
129
130}