Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,33 @@

package org.biojava.nbio.structure.align.gui.jmol;

import org.biojava.nbio.structure.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.JComboBox;

import org.biojava.nbio.structure.Atom;
import org.biojava.nbio.structure.Calc;
import org.biojava.nbio.structure.Group;
import org.biojava.nbio.structure.Structure;
import org.biojava.nbio.structure.StructureTools;
import org.biojava.nbio.structure.align.gui.JPrintPanel;
import org.biojava.nbio.structure.domain.LocalProteinDomainParser;
import org.biojava.nbio.structure.domain.pdp.Domain;
import org.biojava.nbio.structure.domain.pdp.Segment;
import org.biojava.nbio.structure.gui.util.color.ColorUtils;
import org.biojava.nbio.structure.io.mmtf.MmtfActions;
import org.biojava.nbio.structure.jama.Matrix;
import org.biojava.nbio.structure.scop.ScopDatabase;
import org.biojava.nbio.structure.scop.ScopDomain;
Expand All @@ -42,14 +63,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.List;


public class JmolPanel
extends JPrintPanel
Expand Down Expand Up @@ -114,11 +127,28 @@ public void executeCmd(String rasmolScript) {
viewer.evalString(rasmolScript);
}

public void setStructure(Structure s)
public void setStructure(final Structure s)
{
this.structure = s;
String serialized = s.toMMCIF();
viewer.openStringInline(serialized);
try (
PipedOutputStream out = new PipedOutputStream();
// Viewer requires a BufferedInputStream for reflection
InputStream in = new BufferedInputStream(new PipedInputStream(out));
) {
new Thread((Runnable)() -> {
try {
MmtfActions.writeToOutputStream(s,out);
} catch (Exception e) {
logger.error("Error generating MMTF output for {}",
s.getStructureIdentifier()==null ? s.getStructureIdentifier().getIdentifier() : s.getName(), e);
}
}).start();
viewer.openReader(null, in);
} catch (IOException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about throwing the exception? In the end of the day this will only produce a blank jmol screen (and a message in the logs), but a user will not be able to know if there was a problem in the loading. If we throw it then a user can catch it down the line and act upon it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the interesting exception is the one from writeToOutputStream, and that has to run in its own thread if I want to use pipes like this, so it can't throw an exception.

The other IOExceptions would indicate a broken pipe to Jmol, which would very likely be from program termination anyways. Trying to throw an IOException here requires adding the exception to maybe a dozen other functions that call this, some of which don't make much sense.

I guess I could write the MMTF to a byte[] and then pass it to jmol (avoiding creating a new thread).

I guess ultimately I caught the exceptions here because they seemed very unlikely to result from user input (assuming the MMTF encoder doesn't have bugs).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, that makes sense now.

I guess ultimately I caught the exceptions here because they seemed very unlikely to result from user input (assuming the MMTF encoder doesn't have bugs).

Well in theory if the structure is "malformed" in some way the encoder should throw an exception saying it doesn't like it. In real life I'm not sure if such a "malformation" is a possibility.

logger.error("Error transfering {} to Jmol",
s.getStructureIdentifier()==null ? s.getStructureIdentifier().getIdentifier() : s.getName(), e);
}

evalString("save STATE state_1");
}

Expand Down