From: Ben Soares Date: Wed, 12 Apr 2023 14:49:00 +0000 (+0100) Subject: Merge branch 'develop' into features/r2_11_2_alphafold/JAL-629 X-Git-Tag: Release_2_11_4_0~392 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=b7e9e2edf0c1557089eff0e4b5c17c0b31e698c2;p=jalview.git Merge branch 'develop' into features/r2_11_2_alphafold/JAL-629 --- b7e9e2edf0c1557089eff0e4b5c17c0b31e698c2 diff --cc src/jalview/datamodel/ContactMatrix.java index 25aefd3,1b1889e..8434b4a --- a/src/jalview/datamodel/ContactMatrix.java +++ b/src/jalview/datamodel/ContactMatrix.java @@@ -1,7 -1,12 +1,10 @@@ package jalview.datamodel; + import java.awt.Color; -import java.math.BigInteger; import java.util.ArrayList; + import java.util.BitSet; + import java.util.HashMap; import java.util.List; -import java.util.Spliterator; import java.util.StringTokenizer; import jalview.bin.Console; @@@ -169,7 -174,53 +172,64 @@@ public abstract class ContactMatrix imp { return "Contact Matrix"; } - List groups=null; + ++ List groups = null; ++ + @Override + public void updateGroups(List colGroups) + { + groups = colGroups; - colorMap=new HashMap<>(); ++ colorMap = new HashMap<>(); + } ++ + @Override + public boolean hasGroups() + { - return groups!=null && groups.size()>0; ++ return groups != null && groups.size() > 0; + } ++ + @Override + public List getGroups() + { + return groups; + } ++ + @Override + public BitSet getGroupsFor(int column) + { - for (BitSet gp:groups) { ++ for (BitSet gp : groups) ++ { + if (gp.get(column)) + { + return gp; + } + } + return ContactMatrixI.super.getGroupsFor(column); + } - HashMap colorMap = new HashMap<>(); - @Override ++ ++ HashMap colorMap = new HashMap<>(); ++ ++ @Override + public Color getColourForGroup(BitSet bs) + { - if (bs==null) { ++ if (bs == null) ++ { + return Color.white; + } - Color groupCol=colorMap.get(bs); - if (groupCol==null) ++ Color groupCol = colorMap.get(bs); ++ if (groupCol == null) + { + return Color.white; + } + return groupCol; + } - @Override - public void setColorForGroup(BitSet bs,Color color) ++ ++ @Override ++ public void setColorForGroup(BitSet bs, Color color) + { - colorMap.put(bs,color); ++ colorMap.put(bs, color); + } ++ public static String contactToFloatString(ContactMatrixI cm) { StringBuilder sb = new StringBuilder(); diff --cc src/jalview/datamodel/ContactMatrixI.java index bac3abf,1c169ef..3510ed1 --- a/src/jalview/datamodel/ContactMatrixI.java +++ b/src/jalview/datamodel/ContactMatrixI.java @@@ -26,7 -29,48 +31,49 @@@ public interface ContactMatrix String getType(); int getWidth(); + int getHeight(); + + default boolean hasGroups() { + return false; + } + default BitSet getGroupsFor(int column) { + BitSet colbitset = new BitSet(); + colbitset.set(column); + return colbitset; + } + + default List getGroups() { + return Arrays.asList(); + } + + default boolean hasTree() { + return false; + } + + /** + * Newick representation of clustered matrix + * @return null unless hasTree is true + */ + default String getNewick() { + return null; + } + + default String getTreeMethod() { + return null; + } + + default boolean hasCutHeight() { + return false; + } + + default double getCutHeight() { + return 0; + } + + void updateGroups(List colGroups); + + void setColorForGroup(BitSet bs, Color color); + default Color getColourForGroup(BitSet bs) { return Color.white;}; } diff --cc src/jalview/io/NewickFile.java index 98978d0,269ffb3..b3c0011 --- a/src/jalview/io/NewickFile.java +++ b/src/jalview/io/NewickFile.java @@@ -35,10 -40,6 +35,11 @@@ import java.util.StringTokenizer import com.stevesoft.pat.Regex; +import jalview.bin.Jalview; ++import jalview.datamodel.BinaryNode; +import jalview.datamodel.SequenceNode; +import jalview.util.MessageManager; + /** * Parse a new hanpshire style tree Caveats: NHX files are NOT supported and the * tree distances and topology are unreliable when they are parsed. TODO: on @@@ -877,20 -877,20 +878,20 @@@ public class NewickFile extends FilePar { if (root.isDummy()) { - _print(tf, (SequenceNode) root.right()); - _print(tf, (SequenceNode) root.left()); - _print(tf, root.right()); - _print(tf, root.left()); ++ _print(tf, root.right()); ++ _print(tf, root.left()); } else { tf.append("("); - _print(tf, (SequenceNode) root.right()); - _print(tf, root.right()); ++ _print(tf, root.right()); if (root.left() != null) { tf.append(","); } - _print(tf, (SequenceNode) root.left()); - _print(tf, root.left()); ++ _print(tf, root.left()); tf.append(")" + printRootField(root)); } } @@@ -910,24 -910,24 +911,24 @@@ { if (c.isDummy()) { - _print(tf, (SequenceNode) c.left()); - _print(tf, c.left()); ++ _print(tf, c.left()); if (c.left() != null) { tf.append(","); } - _print(tf, (SequenceNode) c.right()); - _print(tf, c.right()); ++ _print(tf, c.right()); } else { tf.append("("); - _print(tf, (SequenceNode) c.right()); - _print(tf, c.right()); ++ _print(tf, c.right()); if (c.left() != null) { tf.append(","); } - _print(tf, (SequenceNode) c.left()); - _print(tf, c.left()); ++ _print(tf, c.left()); tf.append(")" + printNodeField(c)); } } diff --cc src/jalview/ws/datamodel/alphafold/PAEContactMatrix.java index cefdbd2,41e677a..1ec856b --- a/src/jalview/ws/datamodel/alphafold/PAEContactMatrix.java +++ b/src/jalview/ws/datamodel/alphafold/PAEContactMatrix.java @@@ -217,15 -224,17 +224,18 @@@ public class PAEContactMatrix implement @Override public String getAnnotDescr() { - return "Predicted Alignment Error for " + refSeq.getName(); - return "Predicted Alignment Error"+((refSeq==null) ? "" : (" for " + refSeq.getName())); ++ return "Predicted Alignment Error" ++ + ((refSeq == null) ? "" : (" for " + refSeq.getName())); } @Override public String getAnnotLabel() { - StringBuilder label = new StringBuilder("pAE Matrix"); + StringBuilder label = new StringBuilder("PAE Matrix"); - //if (this.getReferenceSeq() != null) - //{ - // label.append(":").append(this.getReferenceSeq().getDisplayId(false)); - //} + // if (this.getReferenceSeq() != null) ++ // { + // label.append(":").append(this.getReferenceSeq().getDisplayId(false)); ++ // } return label.toString(); } @@@ -248,4 -257,120 +258,142 @@@ { return length; } - List groups=null; ++ ++ List groups = null; ++ + @Override + public boolean hasGroups() + { - return groups!=null; ++ return groups != null; + } - String newick=null; ++ ++ String newick = null; ++ + @Override + public String getNewick() + { + return newick; + } ++ + @Override + public boolean hasTree() + { - return newick!=null && newick.length()>0; ++ return newick != null && newick.length() > 0; + } ++ + boolean abs; ++ + double thresh; - String treeType=null; - public void makeGroups(float thresh,boolean abs) ++ ++ String treeType = null; ++ ++ public void makeGroups(float thresh, boolean abs) + { - AverageDistanceEngine clusterer = new AverageDistanceEngine(null, null, this); ++ AverageDistanceEngine clusterer = new AverageDistanceEngine(null, null, ++ this); + double height = clusterer.findHeight(clusterer.getTopNode()); - newick = new jalview.io.NewickFile(clusterer.getTopNode(),false,true).print(); ++ newick = new jalview.io.NewickFile(clusterer.getTopNode(), false, true) ++ .print(); + treeType = "UPGMA"; - Console.trace("Newick string\n"+newick); ++ Console.trace("Newick string\n" + newick); + + List nodegroups; + if (abs ? height > thresh : 0 < thresh && thresh < 1) + { + float cut = abs ? (float) (thresh / height) : thresh; - Console.debug("Threshold "+cut+" for height="+height); ++ Console.debug("Threshold " + cut + " for height=" + height); + + nodegroups = clusterer.groupNodes(cut); + } + else + { + nodegroups = new ArrayList(); + nodegroups.add(clusterer.getTopNode()); + } - this.abs=abs; - this.thresh=thresh; ++ this.abs = abs; ++ this.thresh = thresh; + groups = new ArrayList<>(); - for (BinaryNode root:nodegroups) ++ for (BinaryNode root : nodegroups) + { - BitSet gpset=new BitSet(); - for (BinaryNode leaf:clusterer.findLeaves(root)) ++ BitSet gpset = new BitSet(); ++ for (BinaryNode leaf : clusterer.findLeaves(root)) + { - gpset.set((Integer)leaf.element()); ++ gpset.set((Integer) leaf.element()); + } + groups.add(gpset); + } + } ++ + @Override + public void updateGroups(List colGroups) + { - if (colGroups!=null) ++ if (colGroups != null) + { - groups=colGroups; - } ++ groups = colGroups; ++ } + } ++ + @Override + public BitSet getGroupsFor(int column) + { - for (BitSet gp:groups) { ++ for (BitSet gp : groups) ++ { + if (gp.get(column)) + { + return gp; + } + } + return ContactMatrixI.super.getGroupsFor(column); + } + - HashMap colorMap = new HashMap<>(); - @Override ++ HashMap colorMap = new HashMap<>(); ++ ++ @Override + public Color getColourForGroup(BitSet bs) + { - if (bs==null) { ++ if (bs == null) ++ { + return Color.white; + } - Color groupCol=colorMap.get(bs); - if (groupCol==null) ++ Color groupCol = colorMap.get(bs); ++ if (groupCol == null) + { + return Color.white; + } + return groupCol; + } - @Override - public void setColorForGroup(BitSet bs,Color color) ++ ++ @Override ++ public void setColorForGroup(BitSet bs, Color color) + { - colorMap.put(bs,color); ++ colorMap.put(bs, color); + } ++ + public void restoreGroups(List newgroups, String treeMethod, + String tree, double thresh2) + { - treeType=treeMethod; ++ treeType = treeMethod; + groups = newgroups; - thresh=thresh2; - newick =tree; - ++ thresh = thresh2; ++ newick = tree; ++ + } ++ + @Override - public boolean hasCutHeight() { - return groups!=null && thresh!=0; ++ public boolean hasCutHeight() ++ { ++ return groups != null && thresh != 0; + } ++ + @Override + public double getCutHeight() + { + return thresh; + } ++ + @Override + public String getTreeMethod() + { + return treeType; + } } diff --cc src/jalview/ws/dbsources/EBIAlfaFold.java index 19eaf78,d9cbbd9..d0538c1 --- a/src/jalview/ws/dbsources/EBIAlfaFold.java +++ b/src/jalview/ws/dbsources/EBIAlfaFold.java @@@ -445,10 -442,9 +445,11 @@@ public class EBIAlfaFold extends EbiFil } ContactMatrixI matrix = new PAEContactMatrix(sequence, (Map) paeDict); + ((PAEContactMatrix) matrix).makeGroups(5f, true); AlignmentAnnotation cmannot = sequence.addContactList(matrix); + if (label != null) + cmannot.label = label; pdbAlignment.addAnnotation(cmannot); return true; @@@ -498,17 -494,11 +499,14 @@@ return false; } - ContactMatrixI matrix = new PAEContactMatrix(sm.getSequence(), + SequenceI seq = sm.getSequence(); + Console.debug("##### SEQUENCE FOUND=" + seq.getName()); - Map paeObject = (Map) pae_obj; - - ContactMatrixI matrix = new PAEContactMatrix(seq, paeObject); ++ ContactMatrixI matrix = new PAEContactMatrix(seq, + (Map) pae_obj); + ((PAEContactMatrix) matrix).makeGroups(5f, true); - AlignmentAnnotation cmannot = sm.getSequence().addContactList(matrix); - sm.getSequence().addAlignmentAnnotation(cmannot); + AlignmentAnnotation cmannot = seq.addContactList(matrix); - if (label != null) - { - cmannot.label = label; - } ++ seq.addAlignmentAnnotation(cmannot); + // seq.addAlignmentAnnotation(cmannot); return true; } diff --cc test/jalview/project/Jalview2xmlTests.java index 13df735,81756e0..af52eda --- a/test/jalview/project/Jalview2xmlTests.java +++ b/test/jalview/project/Jalview2xmlTests.java @@@ -1566,12 -1564,11 +1566,13 @@@ public class Jalview2xmlTests extends J } PAEContactMatrix dummyMat = new PAEContactMatrix(sq, paevals); String content = ContactMatrix.contactToFloatString(dummyMat); - Assert.assertTrue(content.contains("\t1.")); // at least one element must be 1 - float[][] vals = ContactMatrix.fromFloatStringToContacts(content, sq.getLength(), sq.getLength()); - assertEquals(vals[3][4],paevals[3][4]); + Assert.assertTrue(content.contains("\t1.")); // at least one element must be + // 1 + float[][] vals = ContactMatrix.fromFloatStringToContacts(content, + sq.getLength(), sq.getLength()); + assertEquals(vals[3][4], paevals[3][4]); - + dummyMat.makeGroups(0.5f, false); + Assert.assertNotSame(dummyMat.getNewick(), ""); AlignmentAnnotation paeCm = sq.addContactList(dummyMat); al.addAnnotation(paeCm); File tfile = File.createTempFile("testStoreAndRecoverPAEmatrix", @@@ -1602,7 -1599,12 +1603,11 @@@ Assert.assertEquals(oldCM.getContactAt(j), newCM.getContactAt(j)); } } + Assert.assertEquals(restoredMat.hasGroups(), dummyMat.hasGroups()); + Assert.assertEquals(restoredMat.getGroups(), dummyMat.getGroups()); + Assert.assertEquals(restoredMat.hasTree(), dummyMat.hasTree()); - Assert.assertEquals( restoredMat.getNewick(),dummyMat.getNewick()); - - ++ Assert.assertEquals(restoredMat.getNewick(), dummyMat.getNewick()); + } }