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 Feb 9, 2007
021 *
022 */
023package org.biojava.nbio.structure.gui.util;
024
025import org.biojava.nbio.structure.Chain;
026import org.biojava.nbio.structure.PdbId;
027import org.biojava.nbio.structure.Structure;
028import org.biojava.nbio.structure.StructureException;
029import org.biojava.nbio.structure.StructureImpl;
030import org.biojava.nbio.structure.align.util.UserConfiguration;
031import org.biojava.nbio.structure.io.PDBFileReader;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import javax.swing.*;
036import java.awt.*;
037import java.awt.event.ActionEvent;
038import java.io.File;
039import java.io.IOException;
040
041/** A class to define where a structure for the alignment is coming from
042 *
043 * @author Andreas Prlic
044 * @since 1.7
045 * @version %I% %G%
046 */
047public class PDBDirPanel
048extends JPanel
049implements StructurePairSelector{
050
051        /**
052         *
053         */
054        private static final long serialVersionUID = -5682120627824627408L;
055
056        boolean debug = true;
057        JTextField pdbDir;
058        JTextField f1;
059        JTextField f2;
060        JTextField c1;
061        JTextField c2;
062
063        private static final Logger logger = LoggerFactory.getLogger(StructurePairSelector.class);
064
065        /** load the PDB files from a local directory
066         *
067         */
068        public PDBDirPanel() {
069
070                Box vBox = Box.createVerticalBox();
071
072                pdbDir = new JTextField(20);
073
074                String conf = System.getProperty(UserConfiguration.PDB_DIR);
075                if ( conf != null){
076                        pdbDir.setText(conf);
077                }
078                JPanel dir = getPDBDirPanel(pdbDir);
079                vBox.add(dir);
080
081
082                int pdbfSize = 4;
083
084                f1 = new JTextField(pdbfSize);
085                c1 = new JTextField(1);
086                JPanel p1 = getPDBFilePanel(1,f1,c1);
087                vBox.add(p1);
088
089                f2 = new JTextField(pdbfSize);
090                c2 = new JTextField(1);
091                JPanel p2 = getPDBFilePanel(2, f2,c2);
092                vBox.add(p2);
093
094
095                this.add(vBox);
096
097        }
098
099
100        private Structure fromPDB(JTextField f, JTextField c) throws StructureException{
101                String pdbIdString = f.getText();
102
103
104                if ( pdbIdString.length() < 4) {
105                        f.setText("!!!");
106                        return null;
107                }
108
109                String chain = c.getText();
110                if ( debug )
111                        System.out.println("file :" + pdbIdString + " " +  chain);
112                /// prepare structures
113
114                // load them from the file system
115
116
117
118                String dir = pdbDir.getText();
119                PDBFileReader reader = new PDBFileReader(dir);
120
121                if ( debug )
122                        System.out.println("dir: " + dir);
123
124                Structure tmp1 = new StructureImpl();
125
126                try {
127                        Structure structure1 = reader.getStructureById(new PdbId(pdbIdString));
128
129                        // no chain has been specified
130                        // return whole structure
131                        if (( chain == null) || (chain.length()==0)){
132                                return structure1;
133                        }
134                        if ( debug)
135                                System.out.println("using chain " + chain +  " for structure " + structure1.getPdbId().getId());
136                        Chain c1 = structure1.getPolyChainByPDB(chain);
137                        tmp1.setPDBHeader(structure1.getPDBHeader());
138                        tmp1.setPdbId(structure1.getPdbId());
139                        tmp1.addChain(c1);
140                        System.out.println("ok");
141
142                } catch (IOException e){
143                        logger.warn(e.getMessage());
144                        throw new StructureException(e);
145                }
146                return tmp1;
147        }
148
149
150
151        @Override
152        public Structure getStructure1() throws StructureException{
153                return fromPDB(f1,c1);
154        }
155
156        @Override
157        public Structure getStructure2() throws StructureException{
158                return fromPDB(f2,c2);
159        }
160
161
162        private JPanel getPDBDirPanel(JTextField f){
163                JPanel panel = new JPanel();
164                panel.setBorder(BorderFactory.createLineBorder(Color.black));
165
166
167                JLabel l01 = new JLabel("Select PDB directory");
168                panel.add(l01);
169
170                panel.add(f);
171
172                Action action = new ChooseDirAction(pdbDir);
173
174                JButton chooser = new JButton(action);
175                panel.add(chooser);
176                return panel;
177        }
178
179        private JPanel getPDBFilePanel(int pos ,JTextField f, JTextField c){
180
181                JPanel panel = new JPanel();
182                panel.setBorder(BorderFactory.createLineBorder(Color.black));
183
184
185
186                JLabel l01 = new JLabel("PDB code ");
187
188                panel.add(l01);
189                Box hBox11 = Box.createHorizontalBox();
190
191                JLabel l11 = new JLabel(pos + ":");
192
193
194
195                f.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
196
197
198                hBox11.add(l11);
199                hBox11.add(Box.createVerticalGlue());
200                hBox11.add(f, BorderLayout.CENTER);
201                hBox11.add(Box.createVerticalGlue());
202
203                panel.add(hBox11);
204
205                Box hBox21 = Box.createHorizontalBox();
206                JLabel l21 = new JLabel("Chain" + pos + ":");
207
208                c.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
209                hBox21.add(l21);
210                hBox21.add(Box.createGlue());
211                hBox21.add(c, BorderLayout.CENTER);
212                hBox21.add(Box.createGlue());
213
214                panel.add(hBox21);
215
216                return panel;
217        }
218
219}
220
221class ChooseDirAction extends AbstractAction{
222
223        JTextField textField;
224        public ChooseDirAction (JTextField textField){
225                super("Choose");
226                this.textField = textField;
227        }
228        public static final long serialVersionUID = 0l;
229        // This method is called when the button is pressed
230        @Override
231        public void actionPerformed(ActionEvent evt) {
232                // Perform action...
233                JFileChooser chooser = new JFileChooser();
234                String txt = textField.getText();
235                if ( txt != null){
236                        chooser.setCurrentDirectory(new java.io.File(txt));
237                }
238                chooser.setDialogTitle("Choose directory that contains your PDB files");
239                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
240                //
241                // disable the "All files" option.
242                //
243                chooser.setAcceptAllFileFilterUsed(false);
244                //
245
246
247
248//              In response to a button click:
249                int returnVal = chooser.showOpenDialog(null);
250                if ( returnVal == JFileChooser.APPROVE_OPTION) {
251                        File file = chooser.getSelectedFile();
252                        textField.setText(file.getAbsolutePath());
253                        textField.repaint();
254                }
255
256        }
257}
258
259