| 1 | package org.intermine.bio.postprocess; |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | * Copyright (C) 2002-2011 FlyMine |
|---|
| 5 | * |
|---|
| 6 | * This code may be freely distributed and modified under the |
|---|
| 7 | * terms of the GNU Lesser General Public Licence. This should |
|---|
| 8 | * be distributed with the code. See the LICENSE file for more |
|---|
| 9 | * information or http://www.gnu.org/copyleft/lesser.html. |
|---|
| 10 | * |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | import java.io.BufferedReader; |
|---|
| 14 | import java.io.File; |
|---|
| 15 | import java.io.InputStream; |
|---|
| 16 | import java.io.InputStreamReader; |
|---|
| 17 | import java.util.ArrayList; |
|---|
| 18 | import java.util.List; |
|---|
| 19 | import java.util.Map; |
|---|
| 20 | import java.util.Properties; |
|---|
| 21 | |
|---|
| 22 | import org.apache.log4j.Logger; |
|---|
| 23 | import org.apache.tools.ant.BuildException; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | import org.intermine.api.config.ClassKeyHelper; |
|---|
| 27 | import org.intermine.metadata.FieldDescriptor; |
|---|
| 28 | import org.intermine.modelproduction.MetadataManager; |
|---|
| 29 | import org.intermine.objectstore.ObjectStore; |
|---|
| 30 | import org.intermine.objectstore.ObjectStoreSummary; |
|---|
| 31 | import org.intermine.objectstore.ObjectStoreWriter; |
|---|
| 32 | import org.intermine.objectstore.ObjectStoreWriterFactory; |
|---|
| 33 | import org.intermine.objectstore.intermine.ObjectStoreInterMineImpl; |
|---|
| 34 | import org.intermine.sql.Database; |
|---|
| 35 | import org.intermine.task.CreateIndexesTask; |
|---|
| 36 | import org.intermine.task.DynamicAttributeTask; |
|---|
| 37 | import org.intermine.task.PrecomputeTask; |
|---|
| 38 | import org.intermine.util.PropertiesUtil; |
|---|
| 39 | import org.intermine.web.autocompletion.AutoCompleter; |
|---|
| 40 | import org.intermine.web.search.KeywordSearch; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Run operations on genomic model database after DataLoading |
|---|
| 44 | * |
|---|
| 45 | * @author Richard Smith |
|---|
| 46 | */ |
|---|
| 47 | public class PostProcessOperationsTask extends DynamicAttributeTask |
|---|
| 48 | { |
|---|
| 49 | private static final Logger LOGGER = Logger.getLogger(PostProcessOperationsTask.class); |
|---|
| 50 | |
|---|
| 51 | protected String operation, objectStoreWriter, ensemblDb, organisms = null; |
|---|
| 52 | protected File outputFile; |
|---|
| 53 | protected ObjectStoreWriter osw; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Sets the value of operation |
|---|
| 57 | * |
|---|
| 58 | * @param operation the operation to perform eg. 'Download publications' |
|---|
| 59 | */ |
|---|
| 60 | public void setOperation(String operation) { |
|---|
| 61 | this.operation = operation; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Sets the value of objectStoreWriter |
|---|
| 66 | * |
|---|
| 67 | * @param objectStoreWriter an objectStoreWriter alias for operations that require one |
|---|
| 68 | */ |
|---|
| 69 | public void setObjectStoreWriter(String objectStoreWriter) { |
|---|
| 70 | this.objectStoreWriter = objectStoreWriter; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Sets the value of outputFile |
|---|
| 75 | * |
|---|
| 76 | * @param outputFile an output file for operations that require one |
|---|
| 77 | */ |
|---|
| 78 | public void setOutputFile(File outputFile) { |
|---|
| 79 | this.outputFile = outputFile; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Sets the value of ensemblDb |
|---|
| 84 | * |
|---|
| 85 | * @param ensemblDb a database alias |
|---|
| 86 | */ |
|---|
| 87 | public void setEnsemblDb(String ensemblDb) { |
|---|
| 88 | this.ensemblDb = ensemblDb; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | private ObjectStoreWriter getObjectStoreWriter() throws Exception { |
|---|
| 92 | if (objectStoreWriter == null) { |
|---|
| 93 | throw new BuildException("objectStoreWriter attribute is not set"); |
|---|
| 94 | } |
|---|
| 95 | if (osw == null) { |
|---|
| 96 | osw = ObjectStoreWriterFactory.getObjectStoreWriter(objectStoreWriter); |
|---|
| 97 | } |
|---|
| 98 | return osw; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * {@inheritDoc} |
|---|
| 103 | */ |
|---|
| 104 | @Override |
|---|
| 105 | public void execute() { |
|---|
| 106 | if (operation == null) { |
|---|
| 107 | throw new BuildException("operation attribute is not set"); |
|---|
| 108 | } |
|---|
| 109 | long startTime = System.currentTimeMillis(); |
|---|
| 110 | try { |
|---|
| 111 | if ("create-chromosome-locations-and-lengths".equals(operation)) { |
|---|
| 112 | CalculateLocations cl = new CalculateLocations(getObjectStoreWriter()); |
|---|
| 113 | LOGGER.info("Starting CalculateLocations.setChromosomeLocationsAndLengths()"); |
|---|
| 114 | cl.setChromosomeLocationsAndLengths(); |
|---|
| 115 | } else if ("set-missing-chromosome-locations".equals(operation)) { |
|---|
| 116 | CalculateLocations cl = new CalculateLocations(getObjectStoreWriter()); |
|---|
| 117 | LOGGER.info("Starting CalculateLocations.setMissingChromosomeLocations()"); |
|---|
| 118 | cl.setMissingChromosomeLocations(); |
|---|
| 119 | } else if ("create-references".equals(operation)) { |
|---|
| 120 | CreateReferences cr = new CreateReferences(getObjectStoreWriter()); |
|---|
| 121 | LOGGER.info("Starting CreateReferences.insertReferences()"); |
|---|
| 122 | cr.insertReferences(); |
|---|
| 123 | } else if ("create-symmetrical-relation-references".equals(operation)) { |
|---|
| 124 | throw new BuildException("create-symmetrical-relation-references task is" |
|---|
| 125 | + " deprecated"); |
|---|
| 126 | } else if ("create-utr-references".equals(operation)) { |
|---|
| 127 | CreateReferences cr = new CreateReferences(getObjectStoreWriter()); |
|---|
| 128 | LOGGER.info("Starting CreateReferences.createUtrRefs()"); |
|---|
| 129 | cr.createUtrRefs(); |
|---|
| 130 | } else if ("transfer-sequences".equals(operation)) { |
|---|
| 131 | TransferSequences ts = new TransferSequences(getObjectStoreWriter()); |
|---|
| 132 | ts = new TransferSequences(getObjectStoreWriter()); |
|---|
| 133 | LOGGER.info("Starting TransferSequences.transferToLocatedSequenceFeatures()"); |
|---|
| 134 | ts.transferToLocatedSequenceFeatures(); |
|---|
| 135 | |
|---|
| 136 | ts = new TransferSequences(getObjectStoreWriter()); |
|---|
| 137 | LOGGER.info("Starting TransferSequences.transferToTranscripts()"); |
|---|
| 138 | ts.transferToTranscripts(); |
|---|
| 139 | } else if ("transfer-sequences-located-sequence-feature".equals(operation)) { |
|---|
| 140 | TransferSequences ts = new TransferSequences(getObjectStoreWriter()); |
|---|
| 141 | LOGGER.info("Starting TransferSequences.transferToLocatedSequenceFeatures()"); |
|---|
| 142 | ts.transferToLocatedSequenceFeatures(); |
|---|
| 143 | } else if ("transfer-sequences-transcripts".equals(operation)) { |
|---|
| 144 | TransferSequences ts = new TransferSequences(getObjectStoreWriter()); |
|---|
| 145 | LOGGER.info("Starting TransferSequences.transferToTranscripts()"); |
|---|
| 146 | ts.transferToTranscripts(); |
|---|
| 147 | } else if ("make-spanning-locations".equals(operation)) { |
|---|
| 148 | CalculateLocations cl = new CalculateLocations(getObjectStoreWriter()); |
|---|
| 149 | LOGGER.info("Starting CalculateLocations.createSpanningLocations()"); |
|---|
| 150 | cl.createSpanningLocations("Transcript", "Exon", "exons"); |
|---|
| 151 | cl.createSpanningLocations("Gene", "Transcript", "transcripts"); |
|---|
| 152 | } else if ("create-intergenic-region-features".equals(operation)) { |
|---|
| 153 | IntergenicRegionUtil ig = new IntergenicRegionUtil(getObjectStoreWriter()); |
|---|
| 154 | LOGGER.info("Starting IntergenicRegionUtil.createIntergenicRegionFeatures()"); |
|---|
| 155 | ig.createIntergenicRegionFeatures(); |
|---|
| 156 | } else if ("create-gene-flanking-features".equals(operation)) { |
|---|
| 157 | CreateFlankingRegions cfr = new CreateFlankingRegions(getObjectStoreWriter()); |
|---|
| 158 | LOGGER.info("Starting CreateFlankingRegions.createFlankingFeatures()"); |
|---|
| 159 | cfr.createFlankingFeatures(); |
|---|
| 160 | } else if ("create-intron-features".equals(operation)) { |
|---|
| 161 | IntronUtil iu = new IntronUtil(getObjectStoreWriter()); |
|---|
| 162 | configureDynamicAttributes(iu); |
|---|
| 163 | LOGGER.info("Starting IntronUtil.createIntronFeatures()"); |
|---|
| 164 | iu.createIntronFeatures(); |
|---|
| 165 | } else if ("create-overlap-relations-flymine".equals(operation)) { |
|---|
| 166 | LOGGER.info("Starting CalculateLocations.createOverlapRelations()"); |
|---|
| 167 | List<String> classNamesToIgnoreList = new ArrayList<String>(); |
|---|
| 168 | String ignoreFileName = "overlap.config"; |
|---|
| 169 | ClassLoader classLoader = PostProcessOperationsTask.class.getClassLoader(); |
|---|
| 170 | InputStream classesToIgnoreStream = |
|---|
| 171 | classLoader.getResourceAsStream(ignoreFileName); |
|---|
| 172 | if (classesToIgnoreStream == null) { |
|---|
| 173 | throw new RuntimeException("can't find resource: " + ignoreFileName); |
|---|
| 174 | } |
|---|
| 175 | BufferedReader classesToIgnoreReader = |
|---|
| 176 | new BufferedReader(new InputStreamReader(classesToIgnoreStream)); |
|---|
| 177 | String line = classesToIgnoreReader.readLine(); |
|---|
| 178 | while (line != null) { |
|---|
| 179 | classNamesToIgnoreList.add(line); |
|---|
| 180 | line = classesToIgnoreReader.readLine(); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | CalculateLocations cl = new CalculateLocations(getObjectStoreWriter()); |
|---|
| 184 | cl.createOverlapRelations(classNamesToIgnoreList, false); |
|---|
| 185 | } else if ("set-collection-counts".equals(operation)) { |
|---|
| 186 | SetCollectionCounts setCounts = new SetCollectionCounts(getObjectStoreWriter()); |
|---|
| 187 | setCounts.setCollectionCount(); |
|---|
| 188 | } else if ("create-attribute-indexes".equals(operation)) { |
|---|
| 189 | CreateIndexesTask cit = new CreateIndexesTask(); |
|---|
| 190 | cit.setAttributeIndexes(true); |
|---|
| 191 | cit.setObjectStore(getObjectStoreWriter().getObjectStore()); |
|---|
| 192 | cit.execute(); |
|---|
| 193 | } else if ("summarise-objectstore".equals(operation)) { |
|---|
| 194 | System.out .println("summarising objectstore ..."); |
|---|
| 195 | ObjectStore os = getObjectStoreWriter().getObjectStore(); |
|---|
| 196 | if (!(os instanceof ObjectStoreInterMineImpl)) { |
|---|
| 197 | throw new RuntimeException("cannot summarise ObjectStore - must be an " |
|---|
| 198 | + "instance of ObjectStoreInterMineImpl"); |
|---|
| 199 | } |
|---|
| 200 | String configFileName = "objectstoresummary.config.properties"; |
|---|
| 201 | ClassLoader classLoader = PostProcessOperationsTask.class.getClassLoader(); |
|---|
| 202 | InputStream configStream = |
|---|
| 203 | classLoader.getResourceAsStream(configFileName); |
|---|
| 204 | if (configStream == null) { |
|---|
| 205 | throw new RuntimeException("can't find resource: " + configFileName); |
|---|
| 206 | } |
|---|
| 207 | Properties config = new Properties(); |
|---|
| 208 | config.load(configStream); |
|---|
| 209 | ObjectStoreSummary oss = new ObjectStoreSummary(os, config); |
|---|
| 210 | Database db = ((ObjectStoreInterMineImpl) os).getDatabase(); |
|---|
| 211 | MetadataManager.store(db, MetadataManager.OS_SUMMARY, |
|---|
| 212 | PropertiesUtil.serialize(oss.toProperties())); |
|---|
| 213 | } else if ("precompute-queries".equals(operation)) { |
|---|
| 214 | (new PrecomputeTask()).precompute(false, getObjectStoreWriter().getObjectStore(), |
|---|
| 215 | 0); |
|---|
| 216 | } else if ("create-lucene-index".equals(operation) |
|---|
| 217 | || "create-autocomplete-index".equals(operation)) { |
|---|
| 218 | System.out .println("create lucene index ..."); |
|---|
| 219 | ObjectStore os = getObjectStoreWriter().getObjectStore(); |
|---|
| 220 | if (!(os instanceof ObjectStoreInterMineImpl)) { |
|---|
| 221 | throw new RuntimeException("cannot summarise ObjectStore - must be an " |
|---|
| 222 | + "instance of ObjectStoreInterMineImpl (create lucene index)"); |
|---|
| 223 | } |
|---|
| 224 | String configFileName = "objectstoresummary.config.properties"; |
|---|
| 225 | ClassLoader classLoader = PostProcessOperationsTask.class.getClassLoader(); |
|---|
| 226 | InputStream configStream = |
|---|
| 227 | classLoader.getResourceAsStream(configFileName); |
|---|
| 228 | if (configStream == null) { |
|---|
| 229 | throw new RuntimeException("can't find resource: " + configFileName); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | Properties properties = new Properties(); |
|---|
| 233 | properties.load(configStream); |
|---|
| 234 | |
|---|
| 235 | Database db = ((ObjectStoreInterMineImpl) os).getDatabase(); |
|---|
| 236 | |
|---|
| 237 | AutoCompleter ac = new AutoCompleter(os, properties); |
|---|
| 238 | if (ac.getBinaryIndexMap() != null) { |
|---|
| 239 | MetadataManager.storeBinary(db, MetadataManager.AUTOCOMPLETE_INDEX, |
|---|
| 240 | ac.getBinaryIndexMap()); |
|---|
| 241 | } |
|---|
| 242 | } else if ("create-search-index".equals(operation)) { |
|---|
| 243 | System .out.println("Creating lucene index for keyword search..."); |
|---|
| 244 | |
|---|
| 245 | ObjectStore os = getObjectStoreWriter().getObjectStore(); |
|---|
| 246 | if (!(os instanceof ObjectStoreInterMineImpl)) { |
|---|
| 247 | throw new RuntimeException("Got invalid ObjectStore - must be an " |
|---|
| 248 | + "instance of ObjectStoreInterMineImpl!"); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | ClassLoader classLoader = PostProcessOperationsTask.class.getClassLoader(); |
|---|
| 252 | |
|---|
| 253 | /* |
|---|
| 254 | String configFileName = "objectstoresummary.config.properties"; |
|---|
| 255 | InputStream configStream = classLoader.getResourceAsStream(configFileName); |
|---|
| 256 | if (configStream == null) { |
|---|
| 257 | throw new RuntimeException("can't find resource: " + configFileName); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | Properties properties = new Properties(); |
|---|
| 261 | properties.load(configStream);*/ |
|---|
| 262 | |
|---|
| 263 | //read class keys to figure out what are keyFields during indexing |
|---|
| 264 | InputStream is = classLoader.getResourceAsStream("class_keys.properties"); |
|---|
| 265 | Properties classKeyProperties = new Properties(); |
|---|
| 266 | classKeyProperties.load(is); |
|---|
| 267 | Map<String, List<FieldDescriptor>> classKeys = |
|---|
| 268 | ClassKeyHelper.readKeys(os.getModel(), classKeyProperties); |
|---|
| 269 | |
|---|
| 270 | //index and save |
|---|
| 271 | KeywordSearch.saveIndexToDatabase(os, classKeys); |
|---|
| 272 | KeywordSearch.deleteIndexDirectory(); |
|---|
| 273 | } else if ("create-overlap-view".equals(operation)) { |
|---|
| 274 | OverlapViewTask ovt = new OverlapViewTask(getObjectStoreWriter()); |
|---|
| 275 | ovt.createView(); |
|---|
| 276 | } else if ("create-bioseg-location-index".equals(operation)) { |
|---|
| 277 | BiosegIndexTask bit = new BiosegIndexTask(getObjectStoreWriter()); |
|---|
| 278 | bit.createIndex(); |
|---|
| 279 | } else if ("link-ins".equals(operation)) { |
|---|
| 280 | CreateFlyBaseLinkIns.createLinkInFile(getObjectStoreWriter().getObjectStore()); |
|---|
| 281 | } else if ("modmine-metadata-cache".equals(operation)) { |
|---|
| 282 | CreateModMineMetaDataCache.createCache(getObjectStoreWriter().getObjectStore()); |
|---|
| 283 | } else { |
|---|
| 284 | throw new BuildException("unknown operation: " + operation); |
|---|
| 285 | } |
|---|
| 286 | LOGGER.info("PP - " + operation + " took " |
|---|
| 287 | + (System.currentTimeMillis() - startTime) + " ms."); |
|---|
| 288 | } catch (BuildException e) { |
|---|
| 289 | LOGGER.error("Failed postprocess. Operation was: " + operation, e); |
|---|
| 290 | throw e; |
|---|
| 291 | } catch (Exception e) { |
|---|
| 292 | LOGGER.error("Failed postprocess. Operation was: " + operation, e); |
|---|
| 293 | throw new BuildException("Operation was:" + operation, e); |
|---|
| 294 | } finally { |
|---|
| 295 | try { |
|---|
| 296 | if (osw != null) { |
|---|
| 297 | osw.close(); |
|---|
| 298 | } |
|---|
| 299 | } catch (Exception e) { |
|---|
| 300 | throw new BuildException(e); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | } |
|---|