From 9e54c643f2bcf3e3c22685e6fadaa28e470fd97a Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Fri, 4 Oct 2024 21:32:57 +0100 Subject: [PATCH] JAL-4469 Added a checkbox to remove version numbers from accession ids enabled/visible only for selected dbs (currently only uniprot). Added some tests for expected query string manipulation. --- resources/lang/Messages.properties | 1 + resources/lang/Messages_es.properties | 1 + src/jalview/gui/SequenceFetcher.java | 52 +++++++++++++++++++++--- test/jalview/gui/SequenceFetcherTest.java | 62 +++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 test/jalview/gui/SequenceFetcherTest.java diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 9df42b9..a2960db 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1490,3 +1490,4 @@ warning.the_following_errors = The following errors and warnings occurred whilst action.show_hetatm = Show Ligands (HETATM) warning.running_from_installer_volume_title = Running from Installer warning.running_from_installer_volume_message = You appear to be launching {0} from the Installer volume.\nPlease drag and drop the "{0}" icon into the Applications folder or your user Applications folder, and launch from there. +label.remove_version_numbers_from_ids = Remove version numbers from IDs ("." and following number) diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index b8200fa..a67f238 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -1452,3 +1452,4 @@ warning.using_mixed_command_line_arguments = Jalview no puede utilizar argumento warning.the_following_errors = Se produjeron los siguientes errores y advertencias al procesar archivos: warning.running_from_installer_volume_title = Ejecutando desde el instalador warning.running_from_installer_volume_message = Parece que está iniciando {0} desde el Volumen del instalador.\nArrastre y suelte el ícono "{0}" en la carpeta Aplicaciones o en la carpeta Aplicaciones de su usuario e inícielo desde allí. +label.remove_version_numbers_from_ids = Eliminar la versión del identificador ("." y los números siguientes) diff --git a/src/jalview/gui/SequenceFetcher.java b/src/jalview/gui/SequenceFetcher.java index 137655f..252c855 100755 --- a/src/jalview/gui/SequenceFetcher.java +++ b/src/jalview/gui/SequenceFetcher.java @@ -113,6 +113,8 @@ public class SequenceFetcher extends JPanel implements Runnable JComboBox database; + JCheckBox removeVersions = null; + JCheckBox replacePunctuation; JButton okBtn; @@ -203,6 +205,16 @@ public class SequenceFetcher extends JPanel implements Runnable { this.setLayout(new BorderLayout()); + removeVersions = new JCheckBox(MessageManager + .getString("label.remove_version_numbers_from_ids")); + removeVersions.setHorizontalAlignment(SwingConstants.LEFT); + removeVersions.setFont(new Font("Verdana", Font.ITALIC, 11)); + boolean removeVersionsDefault = Cache + .getDefault("REMOVE_VERSION_FROM_IDENTIFIERS_DEFAULT", true); + removeVersions.setSelected(removeVersionsDefault); + removeVersions.setEnabled(false); + removeVersions.setVisible(false); + database = new JComboBox<>(); database.setFont(JvSwingUtils.getLabelFont()); StringPair instructionItem = new StringPair( @@ -238,6 +250,7 @@ public class SequenceFetcher extends JPanel implements Runnable .getKey(); updateExampleQuery(currentSelection); + boolean showRemoveVersions = false; if ("pdb".equalsIgnoreCase(currentSelection)) { frame.dispose(); @@ -247,6 +260,7 @@ public class SequenceFetcher extends JPanel implements Runnable { frame.dispose(); new UniprotFTSPanel(SequenceFetcher.this); + showRemoveVersions = true; } else if ("3d-beacons".equalsIgnoreCase(currentSelection)) { @@ -257,6 +271,9 @@ public class SequenceFetcher extends JPanel implements Runnable { otherSourceAction(); } + + removeVersions.setEnabled(showRemoveVersions); + removeVersions.setVisible(showRemoveVersions); } }); @@ -348,6 +365,7 @@ public class SequenceFetcher extends JPanel implements Runnable databasePanel.add(exampleAccession, BorderLayout.CENTER); JPanel jPanel2a = new JPanel(new BorderLayout()); jPanel2a.add(jLabel1, BorderLayout.NORTH); + jPanel2a.add(removeVersions, BorderLayout.CENTER); jPanel2a.add(replacePunctuation, BorderLayout.SOUTH); databasePanel.add(jPanel2a, BorderLayout.SOUTH); @@ -490,12 +508,10 @@ public class SequenceFetcher extends JPanel implements Runnable * tidy inputs and check there is something to search for */ String t0 = textArea.getText(); - String text = t0.trim(); - if (replacePunctuation.isEnabled() && replacePunctuation.isSelected()) - { - text = text.replace(",", ";"); - } - text = text.replaceAll("(\\s|[; ])+", ";"); + String text = replaceQueryText(t0, + replacePunctuation.isEnabled() + && replacePunctuation.isSelected(), + removeVersions.isEnabled() && removeVersions.isSelected()); if (!t0.equals(text)) { textArea.setText(text); @@ -526,6 +542,30 @@ public class SequenceFetcher extends JPanel implements Runnable worker.start(); } + protected static String replaceQueryText(String text0, + boolean replaceSemiColons, boolean removeVersions) + { + String text = text0.trim(); + if (replaceSemiColons) + { + text = text.replace(",", ";"); + } + text = text.replaceAll("(\\s|[; ])+", ";"); + if (removeVersions) + { + Console.debug("search text was '" + text + "'"); + // remove all *last* .123 from an ID. (ID is terminated just before a + // non-alphanumeric-or-dot [which is replaced] or end of string) + text = text.replaceAll("\\.[0-9]+([^0-9a-zA-Z\\.]|$)", "$1"); + /* or do we want: + // remove everything after a first .123.45.6 in an ID + text = text.replaceAll("\\.[0-9\\.]+", ""); + */ + Console.debug("search text now '" + text + "'"); + } + return text; + } + private void resetDialog() { exampleBtn.setEnabled(true); diff --git a/test/jalview/gui/SequenceFetcherTest.java b/test/jalview/gui/SequenceFetcherTest.java new file mode 100644 index 0000000..34c7956 --- /dev/null +++ b/test/jalview/gui/SequenceFetcherTest.java @@ -0,0 +1,62 @@ +package jalview.gui; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class SequenceFetcherTest +{ + @Test(groups = { "Functional" }, dataProvider = "queryConversions") + public void testQueryConversions(String query0, boolean replaceSemiColons, + boolean removeVersions, String result) + { + Assert.assertEquals( + SequenceFetcher.replaceQueryText(query0, replaceSemiColons, + removeVersions), + result, "Converted query string isn't as expected"); + } + + @DataProvider(name = "queryConversions") + public Object[][] queryConversions() + { + /** + * String typedInQuery, replaceSemiColons, removeVersions, resultantQuery + */ + return new Object[][] { + // + + { "P12345", false, false, "P12345" }, + { "P12345", true, false, "P12345" }, + { "P12345", false, true, "P12345" }, + { "P12345", true, true, "P12345" }, + // + { "P12345.1", false, false, "P12345.1" }, + { "P12345.1", true, false, "P12345.1" }, + { "P12345.1", false, true, "P12345" }, + { "P12345.1", true, true, "P12345" }, + // + { "P12345.1 P22222.2", false, false, "P12345.1;P22222.2" }, + { "P12345.1 P22222.2", true, false, "P12345.1;P22222.2" }, + { "P12345.1 P22222.2", false, true, "P12345;P22222" }, + { "P12345.1 P22222.2", true, true, "P12345;P22222" }, + // + { "P12345.1,P22222.2", false, false, "P12345.1,P22222.2" }, + { "P12345.1,P22222.2", true, false, "P12345.1;P22222.2" }, + { "P12345.1,P22222.2", false, true, "P12345,P22222" }, + { "P12345.1,P22222.2", true, true, "P12345;P22222" }, + // + { "P12345.1.9; P22222.2", false, false, "P12345.1.9;P22222.2" }, + { "P12345.1.9; P22222.2", true, false, "P12345.1.9;P22222.2" }, + { "P12345.1.9; P22222.2", false, true, "P12345.1;P22222" }, + { "P12345.1.9; P22222.2", true, true, "P12345.1;P22222" }, + // + { "P12345.1x; P22222.2", false, false, "P12345.1x;P22222.2" }, + { "P12345.1x; P22222.2", true, false, "P12345.1x;P22222.2" }, + { "P12345.1x; P22222.2", false, true, "P12345.1x;P22222" }, + { "P12345.1x; P22222.2", true, true, "P12345.1x;P22222" }, + + // + }; + } + +} -- 1.7.10.2