root/trunk/tools/AtlasMapper/src/org/pathvisio/wikipathways/server/ImageCache.java @ 3151

Revision 3151, 3.9 KB (checked in by thomas, 6 months ago)

fixed build path errors due to API and jar name changes
increased version number of superpathways plugin (for release with new gpml schema)

Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.wikipathways.server;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23import java.io.InputStream;
24import java.util.List;
25
26import javax.xml.rpc.ServiceException;
27
28import org.bridgedb.IDMapperException;
29import org.bridgedb.bio.Organism;
30import org.bridgedb.rdb.GdbProvider;
31import org.pathvisio.model.BatikImageExporter;
32import org.pathvisio.model.ConverterException;
33import org.pathvisio.model.ImageExporter;
34
35import atlas.model.Factor;
36import atlas.model.GeneSet;
37
38public class ImageCache {
39        static final String CACHE_PATH = "cache_images/";
40        static final String SEP_REV = "@";
41        public static final String GET_ID = "id";
42
43        private PathwayCache pathwayCache;
44        private AtlasCache atlasCache;
45        private String basePath;
46        private GdbProvider gdbs;
47
48        private long retention_time = -1;
49
50        public ImageCache(String basePath, PathwayCache pathwayCache, AtlasCache atlasCache, GdbProvider gdbs) {
51                this.pathwayCache = pathwayCache;
52                this.atlasCache = atlasCache;
53                this.basePath = basePath;
54                this.gdbs = gdbs;
55
56                new File(basePath + "/" + CACHE_PATH).mkdirs();
57        }
58
59        public void setRetentionTime(long retention_time) {
60                this.retention_time = retention_time;
61        }
62
63        public String getImageUrl(String pathwayId, List<Factor> factors) throws ConverterException, FileNotFoundException, ServiceException, IOException, ClassNotFoundException, IDMapperException {
64                WPPathway pathway = pathwayCache.getPathway(pathwayId);
65                GeneSet atlasGenes = atlasCache.getGeneSet(pathwayId);
66
67                File cache = getCacheFile(pathway.getId(), pathway.getRevision(), factors);
68                if(!cache.exists() || !CacheManager.checkCacheAge(cache, retention_time)) {
69                        Organism org = Organism.fromLatinName(pathway.getPathway().getMappInfo().getOrganism());
70                        AtlasVisualizer visualizer = new AtlasVisualizer(
71                                pathway.getPathway(),
72                                atlasGenes,
73                                factors,
74                                gdbs.getGdbs(org)
75                        );
76                        visualizer.export(new BatikImageExporter(ImageExporter.TYPE_PNG), cache);
77                }
78                return "getImage?" + GET_ID + "=" + cache.getName();
79        }
80
81        public byte[] getImageData(String id) throws IOException {
82                return getBytesFromFile(getCacheFile(id));
83        }
84
85        /**
86         * Read the given file into a byte array.
87         */
88    public static byte[] getBytesFromFile(File file) throws IOException {
89        InputStream is = new FileInputStream(file);
90        // Get the size of the file
91        long length = file.length();
92        byte[] bytes = new byte[(int)length];
93        // Read in the data
94        int offset = 0;
95        int numRead = 0;
96        while (offset < bytes.length
97               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
98            offset += numRead;
99        }
100        // Ensure all the bytes have been read in
101        if (offset < bytes.length) {
102            throw new IOException("Could not completely read file "+file.getName());
103        }
104        is.close();
105        return bytes;
106    }
107
108        private File getCacheFile(String imageId) {
109                return new File(basePath + "/" + CACHE_PATH, imageId);
110        }
111
112        private File getCacheFile(String id, String revision, List<Factor> factors) {
113                String factorId = "";
114                for(Factor f : factors) factorId += f.hashCode() + SEP_REV;
115                return new File(basePath + "/" + CACHE_PATH, id + SEP_REV + revision + SEP_REV + factorId.hashCode());
116        }
117}
Note: See TracBrowser for help on using the browser.