From 1730be35c0c3fda8f9c63f1fb6dcea7b5fb2ba35 Mon Sep 17 00:00:00 2001 From: James Procter Date: Fri, 1 Sep 2023 16:09:51 +0100 Subject: [PATCH] =?utf8?q?JAL-4124=20add=20strategic=20newlines=20in=20matri?= =?utf8?q?x=20stringified=20representation=20so=20it=20doesn=E2=80=99t=20tri?= =?utf8?q?gger=20buffer=20overflows=20for=20some=20parsers/editors?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/jalview/datamodel/ContactMatrix.java | 12 +++++++-- test/jalview/project/Jalview2xmlTests.java | 39 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) 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