From: James Procter Date: Fri, 1 Sep 2023 15:09:51 +0000 (+0100) Subject: JAL-4124 add strategic newlines in matrix stringified representation so it doesn... X-Git-Tag: Release_2_11_4_0~124^2~7 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=1730be35c0c3fda8f9c63f1fb6dcea7b5fb2ba35;p=jalview.git JAL-4124 add strategic newlines in matrix stringified representation so it doesn’t trigger buffer overflows for some parsers/editors --- diff --git a/src/jalview/datamodel/ContactMatrix.java b/src/jalview/datamodel/ContactMatrix.java index 32fa5b1..2e12a91 100644 --- a/src/jalview/datamodel/ContactMatrix.java +++ b/src/jalview/datamodel/ContactMatrix.java @@ -176,13 +176,21 @@ public abstract class ContactMatrix extends GroupSetHolder implements ContactMat for (int c = 0; c < cm.getWidth(); c++) { ContactListI cl = cm.getContactList(c); + long lastsb=-1; if (cl != null) { for (int h = 0; h <= cl.getContactHeight(); h++) { if (sb.length() > 0) { - sb.append('\t'); + if (sb.length()-lastsb>320) + { + // newline + sb.append('\n'); + lastsb=sb.length(); + } else { + sb.append('\t'); + } } sb.append(cl.getContactAt(h)); } @@ -195,7 +203,7 @@ public abstract class ContactMatrix extends GroupSetHolder implements ContactMat int rows) { float[][] vals = new float[cols][rows]; - StringTokenizer tabsep = new StringTokenizer(values, "" + '\t'); + StringTokenizer tabsep = new StringTokenizer(values, "" + '\t'+'\n'); int c = 0, r = 0; while (tabsep.hasMoreTokens()) { diff --git a/test/jalview/project/Jalview2xmlTests.java b/test/jalview/project/Jalview2xmlTests.java index cbbd0dc..e39d900 100644 --- a/test/jalview/project/Jalview2xmlTests.java +++ b/test/jalview/project/Jalview2xmlTests.java @@ -67,6 +67,7 @@ import jalview.datamodel.HiddenSequences; import jalview.datamodel.Mapping; import jalview.datamodel.PDBEntry; import jalview.datamodel.PDBEntry.Type; +import jalview.datamodel.Sequence; import jalview.datamodel.Sequence.DBModList; import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceFeature; @@ -1547,6 +1548,44 @@ public class Jalview2xmlTests extends Jalview2xmlBase } @Test(groups = { "Functional" }) + public void testMatrixToFloatsAndBack() + { + int imax=2000; + int i=imax; + SequenceI sq = new Sequence("dummy","SEQ"); + while (sq.getLength()= 0; i--) + { + for (int j = 0; j <= i; j++) + { + paevals[i][j] = ((i - j < 2) + || ((i > 1 && i < 5) && (j > 1 && i < 5))) ? 1 : 0f; + paevals[j][i] = -paevals[i][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]); + assertEquals(vals[4][3], paevals[4][3]); + + // test recovery + for (i=0;i