From 8d7c32427dc93f1c9453f4ec8631483a284708f4 Mon Sep 17 00:00:00 2001 From: danielgeiszler Date: Fri, 12 Apr 2024 23:25:43 +0300 Subject: [PATCH] Faster string formatting for spectrum name string operations This commit results in a 69% speedup to PTMShepherd.reNormName --- .../umich/andykong/ptmshepherd/PTMShepherd.java | 15 ++++++++++++--- test/PTMShepherdTest.java | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/PTMShepherdTest.java diff --git a/src/edu/umich/andykong/ptmshepherd/PTMShepherd.java b/src/edu/umich/andykong/ptmshepherd/PTMShepherd.java index 35bacaa..0b19f14 100644 --- a/src/edu/umich/andykong/ptmshepherd/PTMShepherd.java +++ b/src/edu/umich/andykong/ptmshepherd/PTMShepherd.java @@ -1102,9 +1102,18 @@ public static String reNormName(String s) { String[] sp = s.split("\\."); if (sp[sp.length-2].equals(sp[sp.length-1])) // If the format is XXX.Spec.Spec already, return input string return s; - int sn = Integer.parseInt(sp[1]); - //without charge state - return String.format("%s.%d.%d", sp[0], sn, sn); + + // Remove leading zeros from the string + String snString = sp[1].replaceFirst("^0+", ""); + + // Using StringBuilder for potentially better performance + StringBuilder sb = new StringBuilder(); + sb.append(sp[0]); + sb.append('.'); + sb.append(snString); + sb.append('.'); + sb.append(snString); + return sb.toString(); } public static String reNormNameWithCharge(String s) { diff --git a/test/PTMShepherdTest.java b/test/PTMShepherdTest.java new file mode 100644 index 0000000..422c414 --- /dev/null +++ b/test/PTMShepherdTest.java @@ -0,0 +1,15 @@ +import edu.umich.andykong.ptmshepherd.PTMShepherd; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class PTMShepherdTest { + + @Test + void reNormName() { + String specName = "filename.0001.0001.3"; + String expectedSpecName = "filename.1.1"; + String reNormedName = PTMShepherd.reNormName(specName); + System.out.println(reNormedName); + assertEquals(expectedSpecName, reNormedName); + } +}