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<>();
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         * @deprecated Replace with {@see showMultipleAlignmentPanel}
097         */
098        @Deprecated
099        public static void showMultipleAligmentPanel(MultipleAlignment multAln,
100                        AbstractAlignmentJmol jmol) throws StructureException {
101                showMultipleAlignmentPanel(multAln, jmol);
102        }
103
104        /**
105         * Creates a new Frame with the MultipleAlignment Sequence Panel.
106         * The panel can communicate with the Jmol 3D visualization by
107         * selecting the aligned residues of every structure.
108         *
109         * @param multAln
110         * @param jmol
111
112         * @throws StructureException
113         */
114        public static void showMultipleAlignmentPanel(MultipleAlignment multAln,
115                        AbstractAlignmentJmol jmol) throws StructureException {
116
117                MultipleAligPanel me = new MultipleAligPanel(multAln, jmol);
118                JFrame frame = new JFrame();
119
120                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
121                frame.setTitle(jmol.getTitle());
122                me.setPreferredSize(new Dimension(
123                                me.getCoordManager().getPreferredWidth() ,
124                                me.getCoordManager().getPreferredHeight()));
125
126                JMenuBar menu = MenuCreator.getAlignmentPanelMenu(
127                                frame,me,null, multAln);
128                frame.setJMenuBar(menu);
129
130                JScrollPane scroll = new JScrollPane(me);
131                scroll.setAutoscrolls(true);
132
133                MultipleStatusDisplay status = new MultipleStatusDisplay(me);
134                me.addAlignmentPositionListener(status);
135
136                Box vBox = Box.createVerticalBox();
137                vBox.add(scroll);
138                vBox.add(status);
139                frame.getContentPane().add(vBox);
140
141                frame.pack();
142                frame.setVisible(true);
143
144                frame.addWindowListener(me);
145                frame.addWindowListener(status);
146        }
147
148        /**
149         * Creates a new Frame with the String output representation of the
150         * {@link MultipleAlignment}.
151         *
152         * @param multAln
153         * @param result String output
154         */
155        public static void showAlignmentImage(MultipleAlignment multAln,
156                        String result) {
157
158                JFrame frame = new JFrame();
159
160                String title = multAln.getEnsemble().getAlgorithmName() +
161                                " V."+multAln.getEnsemble().getVersion();
162                frame.setTitle(title);
163                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
164
165                AlignmentTextPanel txtPanel = new AlignmentTextPanel();
166                txtPanel.setText(result);
167
168                JMenuBar menu = MenuCreator.getAlignmentTextMenu(
169                                frame,txtPanel,null,multAln);
170
171                frame.setJMenuBar(menu);
172                JScrollPane js = new JScrollPane();
173                js.getViewport().add(txtPanel);
174                js.getViewport().setBorder(null);
175
176                frame.getContentPane().add(js);
177                frame.pack();
178                frame.setVisible(true);
179        }
180
181        /**
182         * Display a MultipleAlignment with a JmolPanel.
183         * New structures are downloaded if they were
184         * not cached in the alignment and they are entirely
185         * transformed here with the superposition information
186         * in the Multiple Alignment.
187         *
188         * @param multAln
189         * @return MultipleAlignmentJmol instance
190         * @throws StructureException
191         */
192        public static MultipleAlignmentJmol display(MultipleAlignment multAln)
193                        throws StructureException {
194
195                List<Atom[]> rotatedAtoms = MultipleAlignmentDisplay.getRotatedAtoms(multAln);
196
197                MultipleAlignmentJmol jmol =
198                                new MultipleAlignmentJmol(multAln, rotatedAtoms);
199
200                jmol.setTitle(jmol.getStructure().getPDBHeader().getTitle());
201                return jmol;
202        }
203
204}