Skip to content

Commit 6f27d0f

Browse files
committed
Save and Load MultipleAlignment through the GUI
1 parent 9d610a9 commit 6f27d0f

File tree

8 files changed

+615
-529
lines changed

8 files changed

+615
-529
lines changed

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/DisplayAFP.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ public static void showAlignmentPanel(AFPChain afpChain, Atom[] ca1, Atom[] ca2,
588588
frame.setTitle(afpChain.getName1() + " vs. " + afpChain.getName2() + " | " + afpChain.getAlgorithmName() + " V. " + afpChain.getVersion());
589589
me.setPreferredSize(new Dimension(me.getCoordManager().getPreferredWidth() , me.getCoordManager().getPreferredHeight()));
590590

591-
JMenuBar menu = MenuCreator.getAlignmentPanelMenu(frame,me,afpChain);
591+
JMenuBar menu = MenuCreator.getAlignmentPanelMenu(frame,me,afpChain,null);
592592
frame.setJMenuBar(menu);
593593

594594
JScrollPane scroll = new JScrollPane(me);
@@ -624,7 +624,7 @@ public static void showAlignmentImage(AFPChain afpChain, String result) {
624624
AlignmentTextPanel txtPanel = new AlignmentTextPanel();
625625
txtPanel.setText(result);
626626

627-
JMenuBar menu = MenuCreator.getAlignmentTextMenu(frame,txtPanel,afpChain);
627+
JMenuBar menu = MenuCreator.getAlignmentTextMenu(frame,txtPanel,afpChain,null);
628628

629629
frame.setJMenuBar(menu);
630630
JScrollPane js = new JScrollPane();

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/MenuCreator.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.biojava.nbio.structure.align.gui.jmol.AbstractAlignmentJmol;
2626
import org.biojava.nbio.structure.align.model.AFPChain;
27+
import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
2728
import org.biojava.nbio.structure.align.util.UserConfiguration;
2829
import org.biojava.nbio.structure.align.webstart.WebStartMain;
2930

@@ -37,10 +38,11 @@
3738
/**
3839
* Create the menus for structure alignment GUI windows (JFrames).
3940
* <p>
40-
* Examples: Text Frames, Alignment Panels, Jmol Panels, etc.
41+
* Examples: Text Frames, Alignment Panels, Jmol Panels.
4142
*
4243
* @author Andreas Prlic
4344
* @author Aleix Lafita
45+
* @author Spencer Bliven
4446
* @since 1.7
4547
*
4648
*/
@@ -63,20 +65,26 @@ public class MenuCreator {
6365
public static final String DOT_PLOT = "Show Dot Plot";
6466
public static final String PAIRWISE_ALIGN = "New Pairwise Alignment";
6567
public static final String MULTIPLE_ALIGN = "New Multiple Alignment";
66-
protected static final int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
68+
protected static final int keyMask =
69+
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
6770

6871
/**
69-
* Provide a JMenuBar that can be added to a JFrame containing a JmolPanel.<p>
72+
* Provide a JMenuBar that can be added to a JFrame containing
73+
* a JmolPanel. The alignment has to be either an AFPChain or a
74+
* MultipleAlignment: set the other parameter to null.<p>
7075
* Menus included:
7176
* <ul><li>File: open, save, export, import, exit.
7277
* <li>Align: new pairwise alignment, new multiple alignment.
73-
* <li>View: aligment panel, aligned pairs, text format, FatCat format, distance matrices, dot plot.
78+
* <li>View: aligment panel, aligned pairs, text format,
79+
* FatCat format, distance matrices, dot plot.
7480
* <li>Help
7581
* </ul>
7682
*
7783
* @return a JMenuBar
7884
*/
79-
public static JMenuBar initJmolMenu(JFrame frame, AbstractAlignmentJmol parent, AFPChain afpChain){
85+
public static JMenuBar initJmolMenu(JFrame frame,
86+
AbstractAlignmentJmol parent, AFPChain afpChain,
87+
MultipleAlignment msa) {
8088

8189
JMenuBar menu = new JMenuBar();
8290

@@ -87,11 +95,11 @@ public static JMenuBar initJmolMenu(JFrame frame, AbstractAlignmentJmol parent,
8795
//Load
8896
if (parent != null){
8997
JMenuItem loadF = getLoadMenuItem();
90-
loadF.addActionListener(new MyAlignmentLoadListener(parent));
98+
loadF.addActionListener(new MyAlignmentLoadListener());
9199
file.add(loadF);
92100
}
93101
//Save
94-
JMenuItem saveF = getSaveAlignmentMenuItem(afpChain); //TODO generalize saving afpChain and MultipleAlignment
102+
JMenuItem saveF = getSaveAlignmentMenuItem(afpChain, msa);
95103
file.add(saveF);
96104
//Open PDB
97105
JMenuItem openPDB = getShowPDBMenuItem();
@@ -143,10 +151,10 @@ public static JMenuBar initJmolMenu(JFrame frame, AbstractAlignmentJmol parent,
143151

144152
if ( parent != null){
145153
//Alignment Panel
146-
JMenuItem aligpI = MenuCreator.getIcon(parent,ALIGNMENT_PANEL);
147-
aligpI.setMnemonic(KeyEvent.VK_M);
148-
aligpI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, keyMask));
149-
view.add(aligpI);
154+
JMenuItem apI = MenuCreator.getIcon(parent,ALIGNMENT_PANEL);
155+
apI.setMnemonic(KeyEvent.VK_M);
156+
apI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, keyMask));
157+
view.add(apI);
150158
//Text Format
151159
JMenuItem textI = MenuCreator.getIcon(parent,TEXT_ONLY);
152160
textI.setMnemonic(KeyEvent.VK_T);
@@ -268,15 +276,20 @@ public static JMenuItem getLoadMenuItem() {
268276

269277

270278
/**
271-
* Create the menu for the Alignment Panel representation of Structural Alignments.
279+
* Create the menu for the Alignment Panel representation of
280+
* Structural Alignments. The alignment can be in AFPChain format
281+
* or in the MultipleAlignment format.
282+
*
272283
* @param frame
273284
* @param actionListener
274285
* @param afpChain
286+
* @param MultipleAlignment
275287
* @return a JMenuBar
276288
*/
277-
public static JMenuBar getAlignmentPanelMenu(JFrame frame, ActionListener actionListener,AFPChain afpChain){
278-
279-
289+
public static JMenuBar getAlignmentPanelMenu(JFrame frame,
290+
ActionListener actionListener, AFPChain afpChain,
291+
MultipleAlignment msa){
292+
280293
JMenuBar menu = new JMenuBar();
281294

282295
JMenu file= new JMenu("File");
@@ -287,14 +300,14 @@ public static JMenuBar getAlignmentPanelMenu(JFrame frame, ActionListener action
287300

288301
JMenuItem saveF = null;
289302

290-
if (saveicon != null )
303+
if (saveicon != null)
291304
saveF = new JMenuItem("Save text display", saveicon);
292305
else
293306
saveF = new JMenuItem("Save text display");
294307

295308
saveF.setMnemonic(KeyEvent.VK_S);
296-
MySaveFileListener listener = new MySaveFileListener(afpChain);
297-
listener.setFatCatOutput(true);
309+
MySaveFileListener listener = new MySaveFileListener(afpChain, msa);
310+
listener.setTextOutput(true);
298311
saveF.addActionListener(listener);
299312
file.add(saveF);
300313

@@ -365,9 +378,12 @@ public static JMenuBar getAlignmentPanelMenu(JFrame frame, ActionListener action
365378
* @param frame
366379
* @param actionListener
367380
* @param afpChain
381+
* @param msa
368382
* @return a JMenuBar
369383
*/
370-
public static JMenuBar getAlignmentTextMenu(JFrame frame, ActionListener actionListener, AFPChain afpChain){
384+
public static JMenuBar getAlignmentTextMenu(JFrame frame,
385+
ActionListener actionListener, AFPChain afpChain,
386+
MultipleAlignment msa){
371387

372388
JMenuBar menu = new JMenuBar();
373389

@@ -385,8 +401,9 @@ public static JMenuBar getAlignmentTextMenu(JFrame frame, ActionListener actionL
385401
saveF = new JMenuItem("Save text display");
386402

387403
saveF.setMnemonic(KeyEvent.VK_S);
388-
MySaveFileListener listener = new MySaveFileListener(afpChain); //TODO save MultipleAlignment not implemented
389-
listener.setFatCatOutput(true);
404+
MySaveFileListener listener =
405+
new MySaveFileListener(afpChain, msa);
406+
listener.setTextOutput(true);
390407
saveF.addActionListener(listener);
391408
file.add(saveF);
392409
file.addSeparator();
@@ -476,7 +493,9 @@ public static JMenuItem getExportPDBMenuItem(AbstractAlignmentJmol parent) {
476493
return exportI;
477494
}
478495

479-
public static JMenuItem getSaveAlignmentMenuItem(AFPChain afpChain) {
496+
public static JMenuItem getSaveAlignmentMenuItem(AFPChain afpChain,
497+
MultipleAlignment msa){
498+
480499
ImageIcon saveicon = createImageIcon("/icons/filesave.png");
481500
JMenuItem saveF = null;
482501

@@ -487,7 +506,7 @@ public static JMenuItem getSaveAlignmentMenuItem(AFPChain afpChain) {
487506

488507
saveF.setMnemonic(KeyEvent.VK_S);
489508
saveF.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, keyMask));
490-
saveF.addActionListener(new MySaveFileListener(afpChain));
509+
saveF.addActionListener(new MySaveFileListener(afpChain, msa));
491510

492511
return saveF;
493512
}
@@ -725,9 +744,7 @@ public void actionPerformed(ActionEvent e) {
725744

726745
/**
727746
* Creates a frame to display a DotPlotPanel.
728-
*
729747
* Used by the 'View>Show Dot Plot' menu item
730-
* @author Spencer Bliven
731748
*
732749
*/
733750
public static class DotPlotListener implements ActionListener {
@@ -737,7 +754,8 @@ public DotPlotListener(AFPChain afpChain) {
737754
}
738755
@Override
739756
public void actionPerformed(ActionEvent e) {
740-
String title = String.format("%s vs. %s", afpChain.getName1(),afpChain.getName2());
757+
String title = String.format("%s vs. %s",
758+
afpChain.getName1(),afpChain.getName2());
741759

742760
// Create window
743761
JFrame frame = new JFrame(title);
@@ -760,12 +778,11 @@ public void windowClosing(WindowEvent e){
760778

761779
public static JMenuBar initAlignmentGUIMenu(JFrame frame) {
762780

763-
764781
JMenu file= new JMenu("File");
765782
file.getAccessibleContext().setAccessibleDescription("File Menu");
766783

767784
JMenuItem loadF = MenuCreator.getLoadMenuItem();
768-
loadF.addActionListener(new MyAlignmentLoadListener(null));
785+
loadF.addActionListener(new MyAlignmentLoadListener());
769786
file.add(loadF);
770787

771788
JMenuItem openPDB = MenuCreator.getShowPDBMenuItem();

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/MultipleAlignmentDisplay.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static void showMultipleAligmentPanel(MultipleAlignment multAln, Abstract
8787
frame.setTitle(jmol.getTitle());
8888
me.setPreferredSize(new Dimension(me.getCoordManager().getPreferredWidth() , me.getCoordManager().getPreferredHeight()));
8989

90-
JMenuBar menu = MenuCreator.getAlignmentPanelMenu(frame,me,null);
90+
JMenuBar menu = MenuCreator.getAlignmentPanelMenu(frame,me,null, multAln);
9191
frame.setJMenuBar(menu);
9292

9393
JScrollPane scroll = new JScrollPane(me);
@@ -125,7 +125,7 @@ public static void showAlignmentImage(MultipleAlignment multAln, String result)
125125
AlignmentTextPanel txtPanel = new AlignmentTextPanel();
126126
txtPanel.setText(result);
127127

128-
JMenuBar menu = MenuCreator.getAlignmentTextMenu(frame,txtPanel,null);
128+
JMenuBar menu = MenuCreator.getAlignmentTextMenu(frame,txtPanel,null,multAln);
129129

130130
frame.setJMenuBar(menu);
131131
JScrollPane js = new JScrollPane();

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/MyAlignmentLoadListener.java

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
package org.biojava.nbio.structure.align.gui;
2222

2323
import org.biojava.nbio.structure.Atom;
24-
import org.biojava.nbio.structure.align.gui.jmol.AbstractAlignmentJmol;
2524
import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol;
2625
import org.biojava.nbio.structure.align.model.AFPChain;
26+
import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
27+
import org.biojava.nbio.structure.align.multiple.MultipleAlignmentEnsemble;
2728
import org.biojava.nbio.structure.align.util.AtomCache;
2829
import org.biojava.nbio.structure.align.util.UserConfiguration;
2930
import org.biojava.nbio.structure.align.webstart.WebStartMain;
3031
import org.biojava.nbio.structure.align.xml.AFPChainXMLParser;
32+
import org.biojava.nbio.structure.align.xml.MultipleAlignmentXMLParser;
3133
import org.biojava.nbio.core.util.InputStreamProvider;
3234

3335
import javax.swing.*;
@@ -38,71 +40,91 @@
3840
import java.io.File;
3941
import java.io.InputStream;
4042
import java.io.InputStreamReader;
41-
42-
43+
import java.util.List;
44+
45+
/**
46+
* Loads an alignment in an XML format and displays its content in a
47+
* new Jmol panel. Can handle both alignment formats: AFPChain and
48+
* MultipleAlignment.
49+
* <p>
50+
* All the alignments stored in the File are displayed, not only the first
51+
* one. However, all the alignments in the same file have to be in the same
52+
* format (either AFPChain or MultipleAlignment).
53+
* Multiple Jmol panels can be created for that purpose.
54+
*
55+
* @author Aleix Lafita
56+
* @since 4.1.1
57+
*
58+
*/
4359
public class MyAlignmentLoadListener implements ActionListener {
4460

45-
AbstractAlignmentJmol jmol;
46-
public MyAlignmentLoadListener(AbstractAlignmentJmol jmol){
47-
this.jmol = jmol;
48-
}
49-
@Override
50-
public void actionPerformed(ActionEvent evt) {
51-
52-
final JFileChooser fc = new JFileChooser();
53-
54-
// In response to a button click:
55-
int returnVal = fc.showOpenDialog(null);
56-
57-
if ( returnVal == JFileChooser.APPROVE_OPTION) {
58-
59-
File file = fc.getSelectedFile();
60-
61-
try {
62-
InputStreamProvider ip = new InputStreamProvider();
63-
InputStream stream = ip.getInputStream(file);
64-
BufferedReader in = new BufferedReader( new InputStreamReader(stream));
61+
@Override
62+
public void actionPerformed(ActionEvent evt) {
6563

64+
final JFileChooser fc = new JFileChooser();
6665

67-
StringBuffer xml = new StringBuffer();
68-
String str;
69-
while ((str = in.readLine()) != null) {
70-
xml.append(str);
71-
}
72-
in.close();
66+
//in response to a button click
67+
int returnVal = fc.showOpenDialog(null);
7368

74-
AFPChain[] afps = AFPChainXMLParser.parseMultiXML(xml.toString());
75-
AFPChain afpChain = afps[0];
69+
if (returnVal == JFileChooser.APPROVE_OPTION) {
7670

77-
UserConfiguration config = WebStartMain.getWebStartConfig();
78-
AtomCache cache = new AtomCache(config.getPdbFilePath(),config.getCacheFilePath());
71+
File file = fc.getSelectedFile();
72+
try {
7973

80-
Atom[] ca1 = cache.getAtoms(afpChain.getName1());
81-
Atom[] ca2 = cache.getAtoms(afpChain.getName2());
74+
InputStreamProvider ip = new InputStreamProvider();
75+
InputStream stream = ip.getInputStream(file);
76+
BufferedReader in = new BufferedReader(
77+
new InputStreamReader(stream));
8278

83-
AFPChainXMLParser.rebuildAFPChain(afpChain, ca1, ca2);
79+
StringBuffer input = new StringBuffer();
80+
String str;
81+
while ((str = in.readLine()) != null) {
82+
input.append(str);
83+
}
84+
in.close();
8485

85-
//Chain c1 = ca1[0].getParent().getParent();
86-
//Chain c2 = ca2[0].getParent().getParent();
86+
String xml = input.toString();
8787

88+
//Determine the format of the file
89+
if (xml.contains("MultipleAlignmentEnsemble")){
8890

89-
//StructureAlignment algorithm = StructureAlignmentFactory.getAlgorithm(afpChain.getAlgorithmName());
90-
StructureAlignmentJmol jmol = StructureAlignmentDisplay.display(afpChain, ca1, ca2);
91+
List<MultipleAlignmentEnsemble> ensembles =
92+
MultipleAlignmentXMLParser.parseXMLfile(xml);
9193

92-
//String result = afpChain.toFatcat(ca1, ca2);
94+
//Display all ensembles, and all its alignments
95+
for (MultipleAlignmentEnsemble e:ensembles){
96+
for (MultipleAlignment msa:e.getMultipleAlignments()){
97+
MultipleAlignmentDisplay.display(msa);
98+
}
99+
}
93100

94-
//String rot = afpChain.toRotMat();
95-
DisplayAFP.showAlignmentPanel(afpChain, ca1,ca2,jmol);
101+
}
102+
else {
96103

104+
AFPChain[] afps = AFPChainXMLParser.parseMultiXML(xml);
97105

106+
UserConfiguration conf = WebStartMain.getWebStartConfig();
107+
AtomCache cache = new AtomCache(
108+
conf.getPdbFilePath(),conf.getCacheFilePath());
98109

99-
} catch (Exception e){
100-
e.printStackTrace();
101-
JOptionPane.showMessageDialog(null,"Could not load alignment file. Exception: " + e.getMessage());
102-
}
110+
for (AFPChain afpChain:afps){
111+
Atom[] ca1 = cache.getAtoms(afpChain.getName1());
112+
Atom[] ca2 = cache.getAtoms(afpChain.getName2());
103113

104-
}
114+
AFPChainXMLParser.rebuildAFPChain(afpChain, ca1, ca2);
115+
StructureAlignmentJmol jmol =
116+
StructureAlignmentDisplay.display(
117+
afpChain, ca1, ca2);
105118

106-
}
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+
}
107129

108130
}

0 commit comments

Comments
 (0)