Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AsyncFactoryBean for loading ontologies #621

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import ubic.basecode.ontology.model.OntologyTerm;
import ubic.basecode.util.DateUtil;
import ubic.basecode.util.FileTools;
import ubic.gemma.core.ontology.providers.GeneOntologyService;
import ubic.gemma.core.ontology.providers.GeneOntologyServiceFactory;
import ubic.gemma.core.ontology.providers.GeneOntologyServiceImpl;
import ubic.gemma.model.association.BioSequence2GeneProduct;
import ubic.gemma.model.common.description.Characteristic;
Expand All @@ -40,14 +42,18 @@
import ubic.gemma.persistence.service.association.Gene2GOAssociationService;
import ubic.gemma.persistence.service.expression.arrayDesign.ArrayDesignService;
import ubic.gemma.persistence.service.expression.designElement.CompositeSequenceService;
import ubic.gemma.persistence.util.AsyncFactoryBeanUtils;
import ubic.gemma.persistence.util.EntityUtils;
import ubic.gemma.persistence.util.Settings;

import java.io.*;
import java.util.*;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;

import static ubic.gemma.persistence.util.AsyncFactoryBeanUtils.getSilently;

/**
* @author Paul
* @see ArrayDesignAnnotationService
Expand All @@ -68,7 +74,7 @@ public static File getFileName( String fileBaseName ) {
* Remove file separators (e.g., "/") from the file names.
*
* @param fileBaseName file base name
* @return munged name
* @return munged name
*/
public static String mungeFileName( String fileBaseName ) {
if ( fileBaseName == null ) {
Expand All @@ -79,7 +85,7 @@ public static String mungeFileName( String fileBaseName ) {

/**
* @param arrayDesign array design
* @return Map of composite sequence ids and transient (incomplete) genes. The genes only have the
* @return Map of composite sequence ids and transient (incomplete) genes. The genes only have the
* symbol filled in.
*/
public static Map<Long, Collection<Gene>> readAnnotationFile( ArrayDesign arrayDesign ) {
Expand All @@ -95,7 +101,7 @@ public static Map<Long, Collection<Gene>> readAnnotationFile( ArrayDesign arrayD
Map<String, Long> probeNameToId = new HashMap<>();
ArrayDesignAnnotationServiceImpl.populateProbeNameToIdMap( arrayDesign, results, probeNameToId );
ArrayDesignAnnotationServiceImpl.log.info( "Reading annotations from: " + f );
try (InputStream is = FileTools.getInputStreamFromPlainOrCompressedFile( f.getAbsolutePath() )) {
try ( InputStream is = FileTools.getInputStreamFromPlainOrCompressedFile( f.getAbsolutePath() ) ) {
return ArrayDesignAnnotationServiceImpl.parseAnnotationFile( results, is, probeNameToId );
} catch ( IOException e ) {
throw new RuntimeException( e );
Expand All @@ -107,7 +113,7 @@ public static Map<Long, Collection<Gene>> readAnnotationFile( ArrayDesign arrayD
* GO annotations are not part of the result.
*
* @param arrayDesign array design
* @return Map of composite sequence ids to an array of delimited strings: [probe name,genes symbol,
* @return Map of composite sequence ids to an array of delimited strings: [probe name,genes symbol,
* gene Name,
* gemma gene id, ncbi id] for a given probe id. format of string is geneSymbol then geneNames
* same as found
Expand Down Expand Up @@ -155,8 +161,8 @@ public static Map<Long, String[]> readAnnotationFileAsString( ArrayDesign arrayD
probeNameToId.put( cs.getName(), cs.getId() );
}

try (InputStream is = FileTools.getInputStreamFromPlainOrCompressedFile( f.getAbsolutePath() );
BufferedReader br = new BufferedReader( new InputStreamReader( is ) )) {
try ( InputStream is = FileTools.getInputStreamFromPlainOrCompressedFile( f.getAbsolutePath() );
BufferedReader br = new BufferedReader( new InputStreamReader( is ) ) ) {
ArrayDesignAnnotationServiceImpl.log.info( "Reading annotations from: " + f );

String line;
Expand Down Expand Up @@ -315,7 +321,8 @@ private static void populateProbeNameToIdMap( ArrayDesign arrayDesign, Map<Long,
private Gene2GOAssociationService gene2GOAssociationService;

@Autowired
private GeneOntologyService goService;
@Qualifier("geneOntologyServiceFactory")
private Future<GeneOntologyService> goService;

private final Transformer goTermExtractor = new Transformer() {
@Override
Expand All @@ -326,15 +333,15 @@ public Object transform( Object input ) {

/*
* (non-Javadoc)
*
*
* @see
* ubic.gemma.core.analysis.service.ArrayDesignAnnotationService#create(ubic.gemma.model.expression.arrayDesign.
* ArrayDesign, java.lang.Boolean)
*/
@Override
public void create( ArrayDesign inputAd, Boolean overWrite ) throws IOException {

if ( !goService.isOntologyLoaded() ) {
if ( !goService.isDone() ) {
throw new IllegalStateException( "GO was not loaded" );
}

Expand Down Expand Up @@ -387,8 +394,9 @@ public void create( ArrayDesign inputAd, Boolean overWrite ) throws IOException
* them (or no annotations)
*/
Collection<ExpressionExperiment> ees = arrayDesignService.getExpressionExperiments( ad );
if ( !ees.isEmpty() ) log.info( "Deleting data files for " + ees.size() + " experiments which use " + ad.getShortName()
+ ", that may have outdated annotations" );
if ( !ees.isEmpty() )
log.info( "Deleting data files for " + ees.size() + " experiments which use " + ad.getShortName()
+ ", that may have outdated annotations" );
for ( ExpressionExperiment ee : ees ) {
this.expressionDataFileService.deleteAllFiles( ee );
}
Expand Down Expand Up @@ -440,7 +448,7 @@ public void deleteExistingFiles( ArrayDesign ad ) {

/*
* (non-Javadoc)
*
*
* @see ubic.gemma.core.analysis.service.ArrayDesignAnnotationService#generateAnnotationFile(java.io.Writer,
* java.util.Collection)
*/
Expand Down Expand Up @@ -573,7 +581,7 @@ private Map<Gene, Collection<Characteristic>> getGOMappings(
/**
* @param ty Configures which GO terms to return: With all parents, biological process only, or direct annotations
* only.
* @return the goTerms for a given gene, as configured
* @return the goTerms for a given gene, as configured
*/
private Collection<OntologyTerm> getGoTerms( Collection<Characteristic> ontologyTerms, OutputType ty ) {

Expand All @@ -582,14 +590,14 @@ private Collection<OntologyTerm> getGoTerms( Collection<Characteristic> ontology
return results;

for ( Characteristic vc : ontologyTerms ) {
results.add( goService.getTermForId( vc.getValue() ) );
results.add( getSilently( goService, GeneOntologyServiceFactory.class ).getTermForId( vc.getValue() ) );
}

if ( ty.equals( OutputType.SHORT ) )
return results;

if ( ty.equals( OutputType.LONG ) ) {
Collection<OntologyTerm> oes = goService.getAllParents( results );
Collection<OntologyTerm> oes = getSilently( goService, GeneOntologyServiceFactory.class ).getAllParents( results );
results.addAll( oes );
} else if ( ty.equals( OutputType.BIOPROCESS ) ) {
Collection<OntologyTerm> toRemove = new HashSet<>();
Expand All @@ -598,7 +606,7 @@ private Collection<OntologyTerm> getGoTerms( Collection<Characteristic> ontology
if ( ( ont == null ) ) {
continue; // / shouldn't happen!
}
if ( !goService.isBiologicalProcess( ont ) ) {
if ( !getSilently( goService, GeneOntologyServiceFactory.class ).isBiologicalProcess( ont ) ) {
toRemove.add( ont );
}
}
Expand Down Expand Up @@ -663,7 +671,7 @@ private void processCompositeSequences( ArrayDesign arrayDesign, String fileBase
return;
}

try (Writer writer = initOutputFile( arrayDesign, fileBaseName, overWrite )) {
try ( Writer writer = initOutputFile( arrayDesign, fileBaseName, overWrite ) ) {

// if no writer then we should abort (this could happen in case where we don't want to overwrite files)
if ( writer == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import ubic.basecode.math.Rank;
import ubic.basecode.ontology.model.OntologyTerm;
import ubic.gemma.core.genome.gene.service.GeneService;
import ubic.gemma.core.ontology.OntologyService;
import ubic.gemma.core.ontology.providers.GeneOntologyService;
import ubic.gemma.core.ontology.providers.GeneOntologyServiceFactory;
import ubic.gemma.model.common.description.Characteristic;
import ubic.gemma.model.common.description.ExternalDatabase;
import ubic.gemma.model.common.description.ExternalDatabases;
Expand All @@ -39,6 +40,9 @@
import ubic.gemma.persistence.service.genome.taxon.TaxonService;

import java.util.*;
import java.util.concurrent.Future;

import static ubic.gemma.persistence.util.AsyncFactoryBeanUtils.getSilently;

/**
* Compute gene multifunctionality and store it in the database.
Expand All @@ -57,10 +61,8 @@ public class GeneMultifunctionalityPopulationServiceImpl implements GeneMultifun
private GeneService geneService;

@Autowired
private GeneOntologyService goService;

@Autowired
private OntologyService ontologyService;
@Qualifier("geneOntologyServiceFactory")
private Future<GeneOntologyService> goService;

@Autowired
private ExternalDatabaseService externalDatabaseService;
Expand Down Expand Up @@ -209,20 +211,6 @@ private Map<Gene, Multifunctionality> computeMultifunctionality( Map<Gene, Colle
}

private Map<Gene, Collection<String>> fetchGoAnnotations( Collection<Gene> genes ) {

if ( !goService.isOntologyLoaded() ) {
goService.startInitializationThread( true, false );
}

while ( !goService.isOntologyLoaded() ) {
try {
Thread.sleep( 2000 );
} catch ( InterruptedException e ) {
//
}
GeneMultifunctionalityPopulationServiceImpl.log.info( "Waiting for GO to load" );
}

/*
* Build the GO 'matrix'.
*/
Expand All @@ -243,7 +231,7 @@ private Map<Gene, Collection<String>> fetchGoAnnotations( Collection<Gene> genes
*/
Set<OntologyTerm> terms = new HashSet<>( annots.size() );
for ( Characteristic t : annots ) {
OntologyTerm term = goService.getTerm( t.getValueUri() );
OntologyTerm term = getSilently( goService, GeneOntologyServiceFactory.class ).getTerm( t.getValueUri() );
if ( term == null || term.isObsolete() ) {
GeneMultifunctionalityPopulationServiceImpl.log
.warn( "Obsolete term annotated to " + gene + " : " + t );
Expand All @@ -254,7 +242,7 @@ private Map<Gene, Collection<String>> fetchGoAnnotations( Collection<Gene> genes
}

// add all the parents
Collection<OntologyTerm> parents = goService.getAllParents( terms );
Collection<OntologyTerm> parents = getSilently( goService, GeneOntologyServiceFactory.class ).getAllParents( terms );
for ( OntologyTerm p : parents ) {
if ( p.isObsolete() ) {
continue;
Expand Down
Loading