/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.ext.rbvi.chimera;
import jalview.structure.AtomSpecModel;
import jalview.util.ColorUtils;
import jalview.util.IntRangeComparator;
import java.awt.Color;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Routines for generating ChimeraX commands for Jalview/ChimeraX binding
*/
public class ChimeraXCommands extends ChimeraCommands
{
private static final String CMD_COLOUR_BY_CHARGE = "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow";
@Override
public String colourByCharge()
{
return CMD_COLOUR_BY_CHARGE;
}
@Override
public String getResidueSpec(String residue)
{
return ":" + residue;
}
@Override
public String setBackgroundColour(Color col)
{
// https://www.cgl.ucsf.edu/chimerax/docs/user/commands/set.html
return "set bgColor " + ColorUtils.toTkCode(col);
}
@Override
protected String getColourCommand(AtomSpecModel colourData,
Color colour)
{
// https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
String colourCode = getColourString(colour);
return "color " + getAtomSpec(colourData, false) + " " + colourCode;
}
@Override
public String focusView()
{
// https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
return "view";
}
/**
* {@inheritDoc}
*
* @return
*/
@Override
public int getModelStartNo()
{
return 1;
}
/**
* Returns a viewer command to set the given residue attribute value on
* residues specified by the AtomSpecModel, for example
*
*
* setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
*
*
* @param attributeName
* @param attributeValue
* @param atomSpecModel
* @return
*/
@Override
protected String getSetAttributeCommand(String attributeName,
String attributeValue, AtomSpecModel atomSpecModel)
{
StringBuilder sb = new StringBuilder(128);
sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
sb.append(" res ").append(attributeName).append(" '")
.append(attributeValue).append("'");
sb.append(" create true");
return sb.toString();
}
@Override
public String openCommandFile(String path)
{
// https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
return "open " + path;
}
@Override
public String saveSession(String filepath)
{
// https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
return "save session " + filepath;
}
/**
* Returns the range(s) formatted as a ChimeraX atomspec, for example
*
* #1/A:2-20,30-40/B:10-20|#2/A:12-30
*
* @return
*/
@Override
public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
{
StringBuilder sb = new StringBuilder(128);
boolean firstModel = true;
for (Integer model : atomSpec.getModels())
{
if (!firstModel)
{
sb.append("|");
}
firstModel = false;
appendModel(sb, model, atomSpec);
if (alphaOnly)
{
sb.append("@CA|P");
}
// todo: is there ChimeraX syntax to exclude altlocs?
}
return sb.toString();
}
/**
* A helper method to append an atomSpec string for atoms in the given model
*
* @param sb
* @param model
* @param atomSpec
*/
protected void appendModel(StringBuilder sb, Integer model,
AtomSpecModel atomSpec)
{
sb.append("#").append(model);
for (String chain : atomSpec.getChains(model))
{
boolean firstPositionForChain = true;
chain = " ".equals(chain) ? chain : chain.trim();
sb.append("/").append(chain).append(":");
List rangeList = atomSpec.getRanges(model, chain);
/*
* sort ranges into ascending start position order
*/
Collections.sort(rangeList, IntRangeComparator.ASCENDING);
int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
Iterator iterator = rangeList.iterator();
while (iterator.hasNext())
{
int[] range = iterator.next();
if (range[0] <= end + 1)
{
/*
* range overlaps or is contiguous with the last one
* - so just extend the end position, and carry on
* (unless this is the last in the list)
*/
end = Math.max(end, range[1]);
}
else
{
/*
* we have a break so append the last range
*/
appendRange(sb, start, end, chain, firstPositionForChain, true);
start = range[0];
end = range[1];
firstPositionForChain = false;
}
}
/*
* and append the last range
*/
if (!rangeList.isEmpty())
{
appendRange(sb, start, end, chain, firstPositionForChain, true);
}
firstPositionForChain = false;
}
}
@Override
public String showBackbone()
{
return "~display all;show @CA|P pbonds";
}
@Override
public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
{
/*
* Form ChimeraX match command to match spec to ref
*
* match #1/A:2-94 toAtoms #2/A:1-93
*
* @see
* https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
*/
StringBuilder cmd = new StringBuilder();
String atomSpec = getAtomSpec(spec, true);
String refSpec = getAtomSpec(ref, true);
cmd.append("align ").append(atomSpec).append(" toAtoms ")
.append(refSpec);
/*
* show superposed residues as ribbon, others as chain
*/
cmd.append("; ribbon ");
cmd.append(getAtomSpec(spec, false)).append("|");
cmd.append(getAtomSpec(ref, false)).append("; view");
return cmd.toString();
}
}