residues = ResidueProperties.getResidues(isNucleotide(),
false);
for (String resName : residues)
{
char res = resName.length() == 3
? ResidueProperties.getSingleCharacterCode(resName)
: resName.charAt(0);
Color colour = cs.findColour(res, 0, null, null, 0f);
colours.put(resName, colour);
}
/*
* pass to the command constructor, and send the command
*/
String cmd = commandGenerator.colourByResidues(colours);
executeCommand(cmd, false, COLOURING_STRUCTURES);
}
public void setBackgroundColour(Color col)
{
String cmd = commandGenerator.setBackgroundColour(col);
executeCommand(cmd, false, null);
}
/**
* Sends one command to the structure viewer. If {@code getReply} is true, the
* command is sent synchronously, otherwise in a deferred thread.
*
* If a progress message is supplied, this is displayed before command
* execution, and removed afterwards.
*
* @param cmd
* @param getReply
* @param msg
* @return
*/
private List executeCommand(String cmd, boolean getReply,
String msg)
{
if (getReply)
{
return executeSynchronous(cmd, msg, getReply);
}
else
{
executeAsynchronous(cmd, msg);
return null;
}
}
/**
* Sends the command in the current thread. If a message is supplied, this is
* shown before the thread is started, and removed when it completes. May
* return a reply to the command if requested.
*
* @param cmd
* @param msg
* @param getReply
* @return
*/
private List executeSynchronous(String cmd, String msg, boolean getReply)
{
final JalviewStructureDisplayI theViewer = getViewer();
final long handle = msg == null ? 0 : theViewer.startProgressBar(msg);
try
{
return executeCommand(cmd, getReply);
} finally
{
if (msg != null)
{
theViewer.stopProgressBar(null, handle);
}
}
}
/**
* Sends the command in a separate thread. If a message is supplied, this is
* shown before the thread is started, and removed when it completes. No value
* is returned.
*
* @param cmd
* @param msg
*/
private void executeAsynchronous(String cmd, String msg)
{
final JalviewStructureDisplayI theViewer = getViewer();
final long handle = msg == null ? 0 : theViewer.startProgressBar(msg);
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
executeCommand(cmd, false);
} finally
{
if (msg != null)
{
theViewer.stopProgressBar(null, handle);
}
}
}
});
}
protected abstract List executeCommand(String command,
boolean getReply);
protected List executeCommands(boolean getReply,
String... commands)
{
List response = null;
for (String cmd : commands)
{
response = executeCommand(cmd, getReply);
}
return response;
}
/**
* colour any structures associated with sequences in the given alignment
* using the getFeatureRenderer() and getSequenceRenderer() renderers but only
* if colourBySequence is enabled.
*/
public void colourBySequence(AlignmentViewPanel alignmentv)
{
if (!colourBySequence || !isLoadingFinished())
{
return;
}
if (getSsm() == null)
{
return;
}
String[] files = getStructureFiles();
SequenceRenderer sr = getSequenceRenderer(alignmentv);
String[] colourBySequenceCommands = commandGenerator
.colourBySequence(getSsm(), files, getSequence(), sr,
alignmentv);
executeCommands(false, colourBySequenceCommands);
}
/**
* Centre the display in the structure viewer
*/
public void focusView()
{
executeCommand(commandGenerator.focusView(), false);
}
/**
* Generates and executes a command to show only specified chains in the
* structure viewer. The list of chains to show should contain entries
* formatted as "pdbid:chaincode".
*
* @param toShow
*/
public void showChains(List toShow)
{
// todo or reformat toShow list entries as modelNo:pdbId:chainCode ?
/*
* Reformat the pdbid:chainCode values as modelNo:chainCode
* since this is what is needed to construct the viewer command
* todo: find a less messy way to do this
*/
List showThese = new ArrayList<>();
for (String chainId : toShow)
{
String[] tokens = chainId.split("\\:");
if (tokens.length == 2)
{
String pdbFile = getFileForChain(chainId);
int modelNo = getModelNoForFile(pdbFile);
String model = modelNo == -1 ? "" : String.valueOf(modelNo);
showThese.add(model + ":" + tokens[1]);
}
}
executeCommand(commandGenerator.showChains(showThese), false);
}
/**
* Answers the structure viewer's model number given a PDB file name. Returns
* -1 if model number is not found.
*
* @param chainId
* @return
*/
protected abstract int getModelNoForFile(String chainId);
public boolean hasFileLoadingError()
{
return fileLoadingError != null && fileLoadingError.length() > 0;
}
/**
* Returns the FeatureRenderer for the given alignment view, or null if
* feature display is turned off in the view.
*
* @param avp
* @return
*/
public FeatureRenderer getFeatureRenderer(AlignmentViewPanel avp)
{
AlignmentViewPanel ap = (avp == null) ? getViewer().getAlignmentPanel()
: avp;
return ap.getAlignViewport().isShowSequenceFeatures()
? ap.getFeatureRenderer()
: null;
}
protected void setStructureCommands(StructureCommandsI cmd)
{
commandGenerator = cmd;
}
/**
* Records association of one chain id (formatted as "pdbid:chainCode") with
* the corresponding PDB file name
*
* @param chainId
* @param fileName
*/
public void addChainFile(String chainId, String fileName)
{
chainFile.put(chainId, fileName);
}
/**
* Returns the PDB filename for the given chain id (formatted as
* "pdbid:chainCode"), or null if not found
*
* @param chainId
* @return
*/
protected String getFileForChain(String chainId)
{
return chainFile.get(chainId);
}
@Override
public void updateColours(Object source)
{
AlignmentViewPanel ap = (AlignmentViewPanel) source;
// ignore events from panels not used to colour this view
if (!getViewer().isUsedForColourBy(ap))
{
return;
}
if (!isLoadingFromArchive())
{
colourBySequence(ap);
}
}
public StructureCommandsI getCommandGenerator()
{
return commandGenerator;
}
}