X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fjmol%2FJalviewJmolBinding.java;h=67327c13b26aed3f956a86c4c5b7bbf9e3c251e6;hb=c8f5deb89ce327cd4a103025122e2ee11a4c97fb;hp=c6871a9d0c6a403f08a70ac75c4c38e54976681e;hpb=153dd62dc91da13ae732600e6ea55ddbe15eab39;p=jalview.git diff --git a/src/jalview/ext/jmol/JalviewJmolBinding.java b/src/jalview/ext/jmol/JalviewJmolBinding.java index c6871a9..67327c1 100644 --- a/src/jalview/ext/jmol/JalviewJmolBinding.java +++ b/src/jalview/ext/jmol/JalviewJmolBinding.java @@ -26,6 +26,7 @@ import java.awt.event.*; import javax.swing.JPanel; +import jalview.api.AlignmentViewPanel; import jalview.api.FeatureRenderer; import jalview.api.SequenceRenderer; import jalview.api.SequenceStructureBinding; @@ -116,7 +117,7 @@ public abstract class JalviewJmolBinding implements StructureListener, public PDBEntry[] pdbentry; /** - * datasource protocol for access to PDBEntry + * datasource protocol for access to PDBEntrylatest */ String protocol = null; @@ -293,198 +294,242 @@ public abstract class JalviewJmolBinding implements StructureListener, public void superposeStructures(AlignmentI alignment, int refStructure, ColumnSelection hiddenCols) { + superposeStructures(new AlignmentI[] + { alignment }, new int[] + { refStructure }, new ColumnSelection[] + { hiddenCols }); + } + + public void superposeStructures(AlignmentI[] _alignment, + int[] _refStructure, ColumnSelection[] _hiddenCols) + { String[] files = getPdbFile(); - if (refStructure >= files.length) + StringBuffer selectioncom = new StringBuffer(); + assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length); + // union of all aligned positions are collected together. + for (int a = 0; a < _alignment.length; a++) { - System.err.println("Invalid reference structure value " - + refStructure); - refStructure = -1; - } - if (refStructure < -1) - { - refStructure = -1; - } - StringBuffer command = new StringBuffer(), selectioncom = new StringBuffer(); + int refStructure = _refStructure[a]; + AlignmentI alignment = _alignment[a]; + ColumnSelection hiddenCols = _hiddenCols[a]; + if (a > 0 + && selectioncom.length() > 0 + && !selectioncom.substring(selectioncom.length() - 1).equals( + "|")) + { + selectioncom.append("|"); + } + // process this alignment + if (refStructure >= files.length) + { + System.err.println("Invalid reference structure value " + + refStructure); + refStructure = -1; + } + if (refStructure < -1) + { + refStructure = -1; + } + StringBuffer command = new StringBuffer(); - boolean matched[] = new boolean[alignment.getWidth()]; - for (int m = 0; m < matched.length; m++) - { + boolean matched[] = new boolean[alignment.getWidth()]; + for (int m = 0; m < matched.length; m++) + { - matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true; - } + matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true; + } - int commonrpositions[][] = new int[files.length][alignment.getWidth()]; - String isel[] = new String[files.length]; - // reference structure - all others are superposed in it - String[] targetC = new String[files.length]; - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); + int commonrpositions[][] = new int[files.length][alignment.getWidth()]; + String isel[] = new String[files.length]; + // reference structure - all others are superposed in it + String[] targetC = new String[files.length]; + String[] chainNames = new String[files.length]; + for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) + { + StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]); - if (mapping == null || mapping.length < 1) - continue; + if (mapping == null || mapping.length < 1) + continue; - int lastPos = -1; - for (int s = 0; s < sequence[pdbfnum].length; s++) - { - for (int sp, m = 0; m < mapping.length; m++) + int lastPos = -1; + for (int s = 0; s < sequence[pdbfnum].length; s++) { - if (mapping[m].getSequence() == sequence[pdbfnum][s] - && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1) + for (int sp, m = 0; m < mapping.length; m++) { - if (refStructure == -1) - { - refStructure = pdbfnum; - } - SequenceI asp = alignment.getSequenceAt(sp); - for (int r = 0; r < matched.length; r++) + if (mapping[m].getSequence() == sequence[pdbfnum][s] + && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1) { - if (!matched[r]) + if (refStructure == -1) { - continue; + refStructure = pdbfnum; } - matched[r] = false; // assume this is not a good site - if (r >= asp.getLength()) + SequenceI asp = alignment.getSequenceAt(sp); + for (int r = 0; r < matched.length; r++) { - continue; - } + if (!matched[r]) + { + continue; + } + matched[r] = false; // assume this is not a good site + if (r >= asp.getLength()) + { + continue; + } - if (jalview.util.Comparison.isGap(asp.getCharAt(r))) + if (jalview.util.Comparison.isGap(asp.getCharAt(r))) + { + // no mapping to gaps in sequence + continue; + } + int t = asp.findPosition(r); // sequence position + int apos = mapping[m].getAtomNum(t); + int pos = mapping[m].getPDBResNum(t); + + if (pos < 1 || pos == lastPos) + { + // can't align unmapped sequence + continue; + } + matched[r] = true; // this is a good ite + lastPos = pos; + // just record this residue position + commonrpositions[pdbfnum][r] = pos; + } + // create model selection suffix + isel[pdbfnum] = "/" + (pdbfnum + 1) + ".1"; + if (mapping[m].getChain() == null + || mapping[m].getChain().trim().length() == 0) { - // no mapping to gaps in sequence - continue; + targetC[pdbfnum] = ""; } - int t = asp.findPosition(r); // sequence position - int apos = mapping[m].getAtomNum(t); - int pos = mapping[m].getPDBResNum(t); - - if (pos < 1 || pos == lastPos) + else { - // can't align unmapped sequence - continue; + targetC[pdbfnum] = ":" + mapping[m].getChain(); } - matched[r] = true; // this is a good ite - lastPos = pos; - // just record this residue position - commonrpositions[pdbfnum][r] = pos; - } - // create model selection suffix - isel[pdbfnum] = "/" + (pdbfnum + 1) + ".1"; - if (mapping[m].getChain() == null - || mapping[m].getChain().trim().length() == 0) - { - targetC[pdbfnum] = ""; + chainNames[pdbfnum] = mapping[m].getPdbId() + + targetC[pdbfnum]; + // move on to next pdb file + s = sequence[pdbfnum].length; + break; } - else - { - targetC[pdbfnum] = ":" + mapping[m].getChain(); - } - // move on to next pdb file - s = sequence[pdbfnum].length; - break; } } } - } - String[] selcom = new String[files.length]; - int nmatched = 0; - // generate select statements to select regions to superimpose structures - { - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) + String[] selcom = new String[files.length]; + int nmatched = 0; + // generate select statements to select regions to superimpose structures { - String chainCd = targetC[pdbfnum]; - int lpos = -1; - boolean run = false; - StringBuffer molsel = new StringBuffer(); - molsel.append("{"); - for (int r = 0; r < matched.length; r++) + for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) { - if (matched[r]) + String chainCd = targetC[pdbfnum]; + int lpos = -1; + boolean run = false; + StringBuffer molsel = new StringBuffer(); + molsel.append("{"); + for (int r = 0; r < matched.length; r++) { - if (pdbfnum == 0) + if (matched[r]) { - nmatched++; - } - if (lpos != commonrpositions[pdbfnum][r] - 1) - { - // discontinuity - if (lpos != -1) + if (pdbfnum == 0) { - molsel.append(lpos); - molsel.append(chainCd); - // molsel.append("} {"); - molsel.append("|"); + nmatched++; } - } - else - { - // continuous run - and lpos >-1 - if (!run) + if (lpos != commonrpositions[pdbfnum][r] - 1) + { + // discontinuity + if (lpos != -1) + { + molsel.append(lpos); + molsel.append(chainCd); + // molsel.append("} {"); + molsel.append("|"); + } + } + else { - // at the beginning, so add dash - molsel.append(lpos); - molsel.append("-"); + // continuous run - and lpos >-1 + if (!run) + { + // at the beginning, so add dash + molsel.append(lpos); + molsel.append("-"); + } + run = true; } - run = true; + lpos = commonrpositions[pdbfnum][r]; + // molsel.append(lpos); } - lpos = commonrpositions[pdbfnum][r]; - // molsel.append(lpos); + } + // add final selection phrase + if (lpos != -1) + { + molsel.append(lpos); + molsel.append(chainCd); + molsel.append("}"); + } + selcom[pdbfnum] = molsel.toString(); + selectioncom.append("(("); + selectioncom.append(selcom[pdbfnum].substring(1, + selcom[pdbfnum].length() - 1)); + selectioncom.append(" )& "); + selectioncom.append(pdbfnum + 1); + selectioncom.append(".1)"); + if (pdbfnum < files.length - 1) + { + selectioncom.append("|"); } } - // add final selection phrase - if (lpos != -1) + } + // TODO: consider bailing if nmatched less than 4 because superposition + // not + // well defined. + // TODO: refactor superposable position search (above) from jmol selection + // construction (below) + for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) + { + if (pdbfnum == refStructure) { - molsel.append(lpos); - molsel.append(chainCd); - molsel.append("}"); + continue; } - selcom[pdbfnum] = molsel.toString(); - selectioncom.append("(("); - selectioncom.append(selcom[pdbfnum].substring(1, - selcom[pdbfnum].length() - 1)); - selectioncom.append(" )& "); - selectioncom.append(pdbfnum + 1); - selectioncom.append(".1)"); - if (pdbfnum < files.length - 1) + command.append("echo "); + command.append("\"Superposing ("); + command.append(chainNames[pdbfnum]); + command.append(") against reference ("); + command.append(chainNames[refStructure]); + command.append(")\";\ncompare "); + command.append("{"); + command.append(1 + pdbfnum); + command.append(".1} {"); + command.append(1 + refStructure); + command.append(".1} SUBSET {*.CA | *.P} ATOMS "); + + // form the matched pair strings + String sep = ""; + for (int s = 0; s < 2; s++) { - selectioncom.append("|"); + command.append(selcom[(s == 0 ? pdbfnum : refStructure)]); } + command.append(" ROTATE TRANSLATE;\n"); } + System.out.println("Select regions:\n" + selectioncom.toString()); + evalStateCommand("select *; cartoons off; backbone; select (" + + selectioncom.toString() + "); cartoons; "); + // selcom.append("; ribbons; "); + System.out.println("Superimpose command(s):\n" + command.toString()); + + evalStateCommand(command.toString()); } - // TODO: consider bailing if nmatched less than 4 because superposition not - // well defined. - // TODO: refactor superposable position search (above) from jmol selection - // construction (below) - for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) - { - if (pdbfnum == refStructure) - { - continue; - } - command.append("compare "); - command.append("{"); - command.append(1 + pdbfnum); - command.append(".1} {"); - command.append(1 + refStructure); - command.append(".1} SUBSET {*.CA | *.P} ATOMS "); - - // form the matched pair strings - String sep = ""; - for (int s = 0; s < 2; s++) + if (selectioncom.length() > 0) + {// finally, mark all regions that were superposed. + if (selectioncom.substring(selectioncom.length() - 1).equals("|")) { - command.append(selcom[(s == 0 ? pdbfnum : refStructure)]); + selectioncom.setLength(selectioncom.length() - 1); } - command.append(" ROTATE TRANSLATE;\n"); + System.out.println("Select regions:\n" + selectioncom.toString()); + evalStateCommand("select *; cartoons off; backbone; select (" + + selectioncom.toString() + "); cartoons; "); + // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString()); } - System.out.println("Select regions:\n" + selectioncom.toString()); - evalStateCommand("select *; cartoons off; backbone; select (" - + selectioncom.toString() + "); cartoons; "); - // selcom.append("; ribbons; "); - System.out.println("Superimpose command(s):\n" + command.toString()); - - evalStateCommand(command.toString()); - - // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString()); } public void evalStateCommand(String command) @@ -503,7 +548,8 @@ public abstract class JalviewJmolBinding implements StructureListener, * using the getFeatureRenderer() and getSequenceRenderer() renderers but only * if colourBySequence is enabled. */ - public void colourBySequence(boolean showFeatures, AlignmentI alignment) + public void colourBySequence(boolean showFeatures, + jalview.api.AlignmentViewPanel alignmentv) { if (!colourBySequence) return; @@ -512,14 +558,15 @@ public abstract class JalviewJmolBinding implements StructureListener, return; } String[] files = getPdbFile(); - SequenceRenderer sr = getSequenceRenderer(); + + SequenceRenderer sr = getSequenceRenderer(alignmentv); FeatureRenderer fr = null; if (showFeatures) { - fr = getFeatureRenderer(); + fr = getFeatureRenderer(alignmentv); } - + AlignmentI alignment = alignmentv.getAlignment(); StringBuffer command = new StringBuffer(); for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++) @@ -666,9 +713,12 @@ public abstract class JalviewJmolBinding implements StructureListener, * returns the current featureRenderer that should be used to colour the * structures * + * @param alignment + * * @return */ - public abstract FeatureRenderer getFeatureRenderer(); + public abstract FeatureRenderer getFeatureRenderer( + AlignmentViewPanel alignment); /** * instruct the Jalview binding to update the pdbentries vector if necessary @@ -744,9 +794,12 @@ public abstract class JalviewJmolBinding implements StructureListener, * returns the current sequenceRenderer that should be used to colour the * structures * + * @param alignment + * * @return */ - public abstract SequenceRenderer getSequenceRenderer(); + public abstract SequenceRenderer getSequenceRenderer( + AlignmentViewPanel alignment); // /////////////////////////////// // JmolStatusListener @@ -841,6 +894,7 @@ public abstract class JalviewJmolBinding implements StructureListener, public void mouseOverStructure(int atomIndex, String strInfo) { int pdbResNum; + int alocsep = strInfo.indexOf("^"); int mdlSep = strInfo.indexOf("/"); int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1; @@ -853,9 +907,18 @@ public abstract class JalviewJmolBinding implements StructureListener, chainSeparator = mdlSep; } } - pdbResNum = Integer.parseInt(strInfo.substring( - strInfo.indexOf("]") + 1, chainSeparator)); + // handle insertion codes + if (alocsep != -1) + { + pdbResNum = Integer.parseInt(strInfo.substring( + strInfo.indexOf("]") + 1, alocsep)); + } + else + { + pdbResNum = Integer.parseInt(strInfo.substring( + strInfo.indexOf("]") + 1, chainSeparator)); + } String chainId; if (strInfo.indexOf(":") > -1) @@ -1213,7 +1276,7 @@ public abstract class JalviewJmolBinding implements StructureListener, ssm.addStructureViewerListener(this); if (notifyLoaded) { - FeatureRenderer fr = getFeatureRenderer(); + FeatureRenderer fr = getFeatureRenderer(null); if (fr != null) { fr.featuresAdded(); @@ -1375,25 +1438,21 @@ public abstract class JalviewJmolBinding implements StructureListener, protected org.jmol.api.JmolAppConsoleInterface console = null; - @Override public void componentResized(ComponentEvent e) { } - @Override public void componentMoved(ComponentEvent e) { } - @Override public void componentShown(ComponentEvent e) { showConsole(true); } - @Override public void componentHidden(ComponentEvent e) { showConsole(false);