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 java.awt.Dimension;
024import java.util.ArrayList;
025import java.util.List;
026
027import javax.swing.Box;
028import javax.swing.JFrame;
029import javax.swing.JMenuBar;
030import javax.swing.JScrollPane;
031
032import org.biojava.nbio.structure.Atom;
033import org.biojava.nbio.structure.StructureException;
034import org.biojava.nbio.structure.align.gui.aligpanel.MultipleAligPanel;
035import org.biojava.nbio.structure.align.gui.aligpanel.MultipleStatusDisplay;
036import org.biojava.nbio.structure.align.gui.jmol.AbstractAlignmentJmol;
037import org.biojava.nbio.structure.align.gui.jmol.JmolTools;
038import org.biojava.nbio.structure.align.gui.jmol.MultipleAlignmentJmol;
039import org.biojava.nbio.structure.align.multiple.Block;
040import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
041import org.biojava.nbio.structure.align.multiple.util.MultipleAlignmentDisplay;
042//import org.slf4j.Logger;
043//import org.slf4j.LoggerFactory;
044
045/**
046 * Utility Class that provides helper methods for the visualization of
047 * {@link MultipleAlignment}s.
048 * <p>
049 * Currently supported: Alignment Panel Display, select aligned
050 * residues in Jmol by their PDB name, show a text Frame for any sequence
051 * alignment format, basic Jmol display from a MultipleAlignment, generate
052 * an artificial PDB structure with a new model for every aligned structure.
053 *
054 * @author Aleix Lafita
055 * @since 4.2.0
056 *
057 */
058public class MultipleAlignmentJmolDisplay  {
059
060        //private static final Logger logger =
061        //              LoggerFactory.getLogger(MultipleAlignmentJmolDisplay.class);
062
063        /**
064         * Utility method used in the {@link MultipleAlignmentJmol} Frame,
065         * when the aligned residues of a structure in the alignment have
066         * to be selected for formatting them (coloring and style).
067         *
068         * @param structNum the structure index (row) of the alignment
069         * @param multAln the MultipleAlignment that contains the equivalent
070         *                      positions
071         * @param ca the atom array of the structure specified
072         *                      (corresponding to the structure index)
073         * @return List of pdb Strings corresponding to the aligned positions
074         *                      of the structure.
075         */
076        public static List<String> getPDBresnum(int structNum,
077                        MultipleAlignment multAln, Atom[] ca){
078
079                List<String> lst = new ArrayList<String>();
080
081                for(Block block : multAln.getBlocks() ) {
082
083                        for (int i=0; i<block.length(); i++){
084                                Integer pos = block.getAlignRes().get(structNum).get(i);
085                                if (pos==null) continue; //gap
086                                else if (pos < ca.length) {
087                                        String pdbInfo = JmolTools.getPdbInfo(ca[pos]);
088                                        lst.add(pdbInfo);
089                                }
090                        }
091                }
092                return lst;
093        }
094
095        /**
096         * Creates a new Frame with the MultipleAlignment Sequence Panel.
097         * The panel can communicate with the Jmol 3D visualization by
098         * selecting the aligned residues of every structure.
099         *
100         * @param multAln
101         * @param jmol
102
103         * @throws StructureException
104         */
105        public static void showMultipleAligmentPanel(MultipleAlignment multAln,
106                        AbstractAlignmentJmol jmol) throws StructureException {
107
108                MultipleAligPanel me = new MultipleAligPanel(multAln, jmol);
109                JFrame frame = new JFrame();
110
111                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
112                frame.setTitle(jmol.getTitle());
113                me.setPreferredSize(new Dimension(
114                                me.getCoordManager().getPreferredWidth() ,
115                                me.getCoordManager().getPreferredHeight()));
116
117                JMenuBar menu = MenuCreator.getAlignmentPanelMenu(
118                                frame,me,null, multAln);
119                frame.setJMenuBar(menu);
120
121                JScrollPane scroll = new JScrollPane(me);
122                scroll.setAutoscrolls(true);
123
124                MultipleStatusDisplay status = new MultipleStatusDisplay(me);
125                me.addAlignmentPositionListener(status);
126
127                Box vBox = Box.createVerticalBox();
128                vBox.add(scroll);
129                vBox.add(status);
130                frame.getContentPane().add(vBox);
131
132                frame.pack();
133                frame.setVisible(true);
134
135                frame.addWindowListener(me);
136                frame.addWindowListener(status);
137        }
138
139        /**
140         * Creates a new Frame with the String output representation of the
141         * {@link MultipleAlignment}.
142         *
143         * @param multAln
144         * @param result String output
145         */
146        public static void showAlignmentImage(MultipleAlignment multAln,
147                        String result) {
148
149                JFrame frame = new JFrame();
150
151                String title = multAln.getEnsemble().getAlgorithmName() +
152                                " V."+multAln.getEnsemble().getVersion();
153                frame.setTitle(title);
154                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
155
156                AlignmentTextPanel txtPanel = new AlignmentTextPanel();
157                txtPanel.setText(result);
158
159                JMenuBar menu = MenuCreator.getAlignmentTextMenu(
160                                frame,txtPanel,null,multAln);
161
162                frame.setJMenuBar(menu);
163                JScrollPane js = new JScrollPane();
164                js.getViewport().add(txtPanel);
165                js.getViewport().setBorder(null);
166
167                frame.getContentPane().add(js);
168                frame.pack();
169                frame.setVisible(true);
170        }
171
172        /**
173         * Display a MultipleAlignment with a JmolPanel.
174         * New structures are downloaded if they were
175         * not cached in the alignment and they are entirely
176         * transformed here with the superposition information
177         * in the Multiple Alignment.
178         *
179         * @param multAln
180         * @return MultipleAlignmentJmol instance
181         * @throws StructureException
182         */
183        public static MultipleAlignmentJmol display(MultipleAlignment multAln)
184                        throws StructureException {
185
186                List<Atom[]> rotatedAtoms = MultipleAlignmentDisplay.getRotatedAtoms(multAln);
187
188                MultipleAlignmentJmol jmol =
189                                new MultipleAlignmentJmol(multAln, rotatedAtoms);
190
191                jmol.setTitle(jmol.getStructure().getPDBHeader().getTitle());
192                return jmol;
193        }
194
195}