From: gmungoc Date: Thu, 2 Jul 2020 14:28:36 +0000 (+0100) Subject: JAL-3615 used gzip endpoints for Pfam and Rfam X-Git-Tag: Develop-2_11_2_0-d20201215~24^2~22^2~5 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=f263182c25e549d39438219b3f5b948f153f233f;p=jalview.git JAL-3615 used gzip endpoints for Pfam and Rfam --- diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index a5a4e36..dac8fe6 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -40,6 +40,8 @@ import jalview.api.AlignmentViewPanel; import jalview.api.FeatureSettingsModelI; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.ws.dbsources.Pfam; +import jalview.ws.dbsources.Rfam; /** * implements a random access wrapper around a particular datasource, for @@ -51,6 +53,8 @@ public class FileParse protected static final String TAB = "\t"; + private static final String GZ_EXT = ".gz"; + /** * text specifying source of data. usually filename or url. */ @@ -64,6 +68,7 @@ public class FileParse { return bytes; } + /** * a viewport associated with the current file operation. May be null. May * move to different object. @@ -190,7 +195,7 @@ public class FileParse } if (!error) { - if (fileStr.toLowerCase().endsWith(".gz")) + if (fileStr.toLowerCase().endsWith(GZ_EXT)) { try { @@ -232,7 +237,7 @@ public class FileParse // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via // Kenny Sabir Exception e = null; - if (fileStr.toLowerCase().endsWith(".gz")) + if (isGzipped(fileStr)) { try { @@ -264,6 +269,27 @@ public class FileParse } /** + * Answers true if the filename (or URL) has a format which Jalview recognises + * as denoting gzipped content. + *

+ * Currently this means having a ".gz" extension, or ending in "/gzipped" or + * "?gz=1" (used to retrieve gzipped from Pfam and Rfam respectively). + * + * @param filename + * @return + */ + protected static boolean isGzipped(String filename) + { + if (filename == null) + { + return false; + } + String lower = filename.toLowerCase(); + return lower.endsWith(GZ_EXT) || lower.endsWith(Pfam.GZIPPED) + || lower.endsWith(Rfam.GZIPPED); + } + + /** * sets the suffix string (if any) and returns remainder (if suffix was * detected) * @@ -345,7 +371,8 @@ public class FileParse { // this will be from JavaScript inFile = file; - dataIn = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes))); + dataIn = new BufferedReader( + new InputStreamReader(new ByteArrayInputStream(bytes))); dataName = fileStr; } else if (checkFileSource(fileStr)) @@ -453,8 +480,7 @@ public class FileParse { // pass up the reason why we have no source to read from throw new IOException(MessageManager.formatMessage( - "exception.failed_to_read_data_from_source", - new String[] + "exception.failed_to_read_data_from_source", new String[] { errormessage })); } error = false; diff --git a/src/jalview/ws/dbsources/Pfam.java b/src/jalview/ws/dbsources/Pfam.java index 8877c34..de19078 100644 --- a/src/jalview/ws/dbsources/Pfam.java +++ b/src/jalview/ws/dbsources/Pfam.java @@ -35,6 +35,11 @@ import com.stevesoft.pat.Regex; */ abstract public class Pfam extends Xfam { + /* + * append to URLs to retrieve as a gzipped file + */ + public static final String GZIPPED = "/gzipped"; + static final String PFAM_BASEURL_KEY = "PFAM_BASEURL"; private static final String DEFAULT_PFAM_BASEURL = "https://pfam.xfam.org"; diff --git a/src/jalview/ws/dbsources/PfamFull.java b/src/jalview/ws/dbsources/PfamFull.java index 0600427..d71892b 100644 --- a/src/jalview/ws/dbsources/PfamFull.java +++ b/src/jalview/ws/dbsources/PfamFull.java @@ -34,7 +34,7 @@ public class PfamFull extends Pfam @Override public String getURLSuffix() { - return "/alignment/full"; + return "/alignment/full" + GZIPPED; } /* diff --git a/src/jalview/ws/dbsources/PfamSeed.java b/src/jalview/ws/dbsources/PfamSeed.java index dff8a17..f64d07f 100644 --- a/src/jalview/ws/dbsources/PfamSeed.java +++ b/src/jalview/ws/dbsources/PfamSeed.java @@ -36,7 +36,7 @@ public class PfamSeed extends Pfam @Override public String getURLSuffix() { - return "/alignment/seed"; + return "/alignment/seed" + GZIPPED; } /* diff --git a/src/jalview/ws/dbsources/Rfam.java b/src/jalview/ws/dbsources/Rfam.java index 1d9d99a..dba3e31 100644 --- a/src/jalview/ws/dbsources/Rfam.java +++ b/src/jalview/ws/dbsources/Rfam.java @@ -36,6 +36,11 @@ abstract public class Rfam extends Xfam private static final String DEFAULT_RFAM_BASEURL = "https://rfam.xfam.org"; + /* + * append to URLs to retrieve as a gzipped file + */ + public static final String GZIPPED = "?gz=1&download=1"; + @Override protected String getURLPrefix() { diff --git a/src/jalview/ws/dbsources/RfamFull.java b/src/jalview/ws/dbsources/RfamFull.java index d815336..396511c 100644 --- a/src/jalview/ws/dbsources/RfamFull.java +++ b/src/jalview/ws/dbsources/RfamFull.java @@ -36,7 +36,7 @@ public class RfamFull extends Rfam @Override public String getURLSuffix() { - return "/alignment/full"; + return "/alignment/full" + GZIPPED; } /* diff --git a/src/jalview/ws/dbsources/RfamSeed.java b/src/jalview/ws/dbsources/RfamSeed.java index a74e829..eaa574b 100644 --- a/src/jalview/ws/dbsources/RfamSeed.java +++ b/src/jalview/ws/dbsources/RfamSeed.java @@ -36,8 +36,7 @@ public class RfamSeed extends Rfam @Override public String getURLSuffix() { - // to download gzipped file add '?gzip=1' - return "/alignment/stockholm"; + return "/alignment/stockholm" + GZIPPED; } /* diff --git a/src/jalview/ws/dbsources/Xfam.java b/src/jalview/ws/dbsources/Xfam.java index b83f558..f0cb14b 100644 --- a/src/jalview/ws/dbsources/Xfam.java +++ b/src/jalview/ws/dbsources/Xfam.java @@ -36,7 +36,6 @@ import jalview.ws.seqfetcher.DbSourceProxyImpl; */ public abstract class Xfam extends DbSourceProxyImpl { - public Xfam() { super(); diff --git a/test/jalview/io/FileParseTest.java b/test/jalview/io/FileParseTest.java new file mode 100644 index 0000000..7eb2c9c --- /dev/null +++ b/test/jalview/io/FileParseTest.java @@ -0,0 +1,34 @@ +package jalview.io; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import org.testng.annotations.Test; + +public class FileParseTest +{ + @Test(groups = "Functional") + public void setIsGzipped() + { + assertFalse(FileParse.isGzipped(null)); + assertFalse(FileParse.isGzipped("foobar")); + assertFalse(FileParse.isGzipped(".gz.foobar")); + + assertTrue(FileParse.isGzipped("abc.gz")); + assertTrue(FileParse.isGzipped("abc.GZ")); + assertTrue(FileParse.isGzipped(".gz")); + assertFalse(FileParse.isGzipped("abc/gz")); + assertFalse(FileParse.isGzipped("gz")); + + assertTrue(FileParse.isGzipped("http:/xy.com/abc/gzipped")); + assertTrue(FileParse.isGzipped("abc/gzipped")); + assertTrue(FileParse.isGzipped("abc/GZIPPED")); + assertTrue(FileParse.isGzipped("/gzipped")); + assertFalse(FileParse.isGzipped("gzipped")); + + assertTrue(FileParse.isGzipped("http:/xy.com/abc?gz=1")); + assertTrue(FileParse.isGzipped("http:/xy.com/abc?GZ=1")); + // currently only recognised if the last token on the URL + assertFalse(FileParse.isGzipped("http:/xy.com/abc?gz=1&content-type=text/xml")); + } +}