From: gmungoc Date: Tue, 19 May 2020 10:50:41 +0000 (+0100) Subject: JAL-3624 use temp file for SIFT download in JS, correct unmarshalling X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9c7418155500b39b96d2d6f92d9ce12012b58622;p=jalview.git JAL-3624 use temp file for SIFT download in JS, correct unmarshalling --- diff --git a/src/jalview/ws/sifts/SiftsClient.java b/src/jalview/ws/sifts/SiftsClient.java index d065666..4fb9ca9 100644 --- a/src/jalview/ws/sifts/SiftsClient.java +++ b/src/jalview/ws/sifts/SiftsClient.java @@ -20,28 +20,6 @@ */ package jalview.ws.sifts; -import jalview.analysis.AlignSeq; -import jalview.analysis.scoremodels.ScoreMatrix; -import jalview.analysis.scoremodels.ScoreModels; -import jalview.api.DBRefEntryI; -import jalview.api.SiftsClientI; -import jalview.datamodel.DBRefEntry; -import jalview.datamodel.DBRefSource; -import jalview.datamodel.SequenceI; -import jalview.io.StructureFile; -import jalview.schemes.ResidueProperties; -import jalview.structure.StructureMapping; -import jalview.util.Comparison; -import jalview.util.DBRefUtils; -import jalview.util.Format; -import jalview.xml.binding.sifts.Entry; -import jalview.xml.binding.sifts.Entry.Entity; -import jalview.xml.binding.sifts.Entry.Entity.Segment; -import jalview.xml.binding.sifts.Entry.Entity.Segment.ListMapRegion.MapRegion; -import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue; -import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.CrossRefDb; -import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.ResidueDetail; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -67,10 +45,33 @@ import java.util.TreeMap; import java.util.zip.GZIPInputStream; import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; +import jalview.analysis.AlignSeq; +import jalview.analysis.scoremodels.ScoreMatrix; +import jalview.analysis.scoremodels.ScoreModels; +import jalview.api.DBRefEntryI; +import jalview.api.SiftsClientI; +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.DBRefSource; +import jalview.datamodel.SequenceI; +import jalview.io.StructureFile; +import jalview.schemes.ResidueProperties; +import jalview.structure.StructureMapping; +import jalview.util.Comparison; +import jalview.util.DBRefUtils; +import jalview.util.Format; +import jalview.util.Platform; +import jalview.xml.binding.sifts.Entry; +import jalview.xml.binding.sifts.Entry.Entity; +import jalview.xml.binding.sifts.Entry.Entity.Segment; +import jalview.xml.binding.sifts.Entry.Entity.Segment.ListMapRegion.MapRegion; +import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue; +import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.CrossRefDb; +import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.ResidueDetail; import mc_view.Atom; import mc_view.PDBChain; @@ -187,7 +188,8 @@ public class SiftsClient implements SiftsClientI XMLStreamReader streamReader = XMLInputFactory.newInstance() .createXMLStreamReader(gzis); Unmarshaller um = jc.createUnmarshaller(); - return (Entry) um.unmarshal(streamReader); + JAXBElement jbe = um.unmarshal(streamReader, Entry.class); + return jbe.getValue(); } catch (Exception e) { e.printStackTrace(); @@ -299,21 +301,36 @@ public class SiftsClient implements SiftsClientI } String siftFile = pdbId + ".xml.gz"; String siftsFileFTPURL = SIFTS_FTP_BASE_URL + siftFile; - String downloadedSiftsFile = SiftsSettings.getSiftDownloadDirectory() - + siftFile; - File siftsDownloadDir = new File( - SiftsSettings.getSiftDownloadDirectory()); - if (!siftsDownloadDir.exists()) + + /* + * Download the file from URL to either + * Java: directory of cached downloaded SIFTS files + * Javascript: temporary 'file' (in-memory cache) + */ + File downloadTo = null; + if (Platform.isJS()) + { + downloadTo = File.createTempFile(siftFile, ".xml.gz"); + } + else { - siftsDownloadDir.mkdirs(); + downloadTo = new File( + SiftsSettings.getSiftDownloadDirectory() + siftFile); + File siftsDownloadDir = new File( + SiftsSettings.getSiftDownloadDirectory()); + if (!siftsDownloadDir.exists()) + { + siftsDownloadDir.mkdirs(); + } } + // System.out.println(">> Download ftp url : " + siftsFileFTPURL); // long now = System.currentTimeMillis(); URL url = new URL(siftsFileFTPURL); URLConnection conn = url.openConnection(); InputStream inputStream = conn.getInputStream(); FileOutputStream outputStream = new FileOutputStream( - downloadedSiftsFile); + downloadTo); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) @@ -324,7 +341,7 @@ public class SiftsClient implements SiftsClientI inputStream.close(); // System.out.println(">>> File downloaded : " + downloadedSiftsFile // + " took " + (System.currentTimeMillis() - now) + "ms"); - return new File(downloadedSiftsFile); + return downloadTo; } /**