Search in sources :

Example 6 with BlastException

use of org.jbei.ice.lib.search.blast.BlastException in project ice by JBEI.

the class PartTraceSequences method buildOrRebuildAlignment.

/**
 * Calculate sequence alignment between the given {@link TraceSequence} and {@link Sequence}
 * using bl2seq, and save the result into the database.
 *
 * @param traceSequence traceSequence
 * @param sequence      sequence
 */
public void buildOrRebuildAlignment(TraceSequence traceSequence, Sequence sequence) {
    if (traceSequence == null) {
        throw new IllegalArgumentException("Failed to rebuild alignment for null trace sequence!");
    }
    // if sequence is null => delete alignment
    if (sequence == null || sequence.getEntry() == null) {
        return;
    }
    // actually build alignment
    String traceSequenceString = traceSequence.getSequence();
    String entrySequenceString = sequence.getSequence();
    int entrySequenceLength = entrySequenceString.length();
    boolean isCircular = (sequence.getEntry() instanceof Plasmid) && ((Plasmid) sequence.getEntry()).getCircular();
    if (isCircular) {
        entrySequenceString += entrySequenceString;
    }
    try {
        List<Bl2SeqResult> bl2seqAlignmentResults = new BlastPlus().runBlast2Seq(entrySequenceString, traceSequenceString);
        if (bl2seqAlignmentResults.size() > 0) {
            int maxAlignedSequenceLength = -1;
            Bl2SeqResult maxBl2SeqResult = null;
            for (Bl2SeqResult bl2seqResult : bl2seqAlignmentResults) {
                int querySequenceLength = bl2seqResult.getQuerySequence().length();
                if (maxAlignedSequenceLength < querySequenceLength) {
                    maxAlignedSequenceLength = querySequenceLength;
                    maxBl2SeqResult = bl2seqResult;
                }
            }
            if (maxBl2SeqResult != null) {
                int strand = maxBl2SeqResult.getOrientation() == 0 ? 1 : -1;
                TraceSequenceAlignment traceSequenceAlignment = traceSequence.getTraceSequenceAlignment();
                int queryStart = maxBl2SeqResult.getQueryStart();
                int queryEnd = maxBl2SeqResult.getQueryEnd();
                int subjectStart = maxBl2SeqResult.getSubjectStart();
                int subjectEnd = maxBl2SeqResult.getSubjectEnd();
                if (isCircular) {
                    if (queryStart > entrySequenceLength - 1) {
                        queryStart = queryStart - entrySequenceLength;
                    }
                    if (queryEnd > entrySequenceLength - 1) {
                        queryEnd = queryEnd - entrySequenceLength;
                    }
                    if (subjectEnd > entrySequenceLength - 1) {
                        subjectEnd = subjectEnd - entrySequenceLength;
                    }
                    if (subjectStart > entrySequenceLength - 1) {
                        subjectStart = subjectStart - entrySequenceLength;
                    }
                }
                if (traceSequenceAlignment == null) {
                    traceSequenceAlignment = new TraceSequenceAlignment(traceSequence, maxBl2SeqResult.getScore(), strand, queryStart, queryEnd, subjectStart, subjectEnd, maxBl2SeqResult.getQuerySequence(), maxBl2SeqResult.getSubjectSequence(), sequence.getFwdHash(), new Date());
                    traceSequence.setTraceSequenceAlignment(traceSequenceAlignment);
                } else {
                    traceSequenceAlignment.setModificationTime(new Date());
                    traceSequenceAlignment.setScore(maxBl2SeqResult.getScore());
                    traceSequenceAlignment.setStrand(strand);
                    traceSequenceAlignment.setQueryStart(queryStart);
                    traceSequenceAlignment.setQueryEnd(queryEnd);
                    traceSequenceAlignment.setSubjectStart(subjectStart);
                    traceSequenceAlignment.setSubjectEnd(subjectEnd);
                    traceSequenceAlignment.setQueryAlignment(maxBl2SeqResult.getQuerySequence());
                    traceSequenceAlignment.setSubjectAlignment(maxBl2SeqResult.getSubjectSequence());
                    traceSequenceAlignment.setSequenceHash(sequence.getFwdHash());
                }
                dao.save(traceSequence);
            }
        }
    } catch (BlastException e) {
        Logger.error(e);
    }
}
Also used : BlastPlus(org.jbei.ice.lib.search.blast.BlastPlus) Bl2SeqResult(org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult) Date(java.util.Date) BlastException(org.jbei.ice.lib.search.blast.BlastException)

Example 7 with BlastException

use of org.jbei.ice.lib.search.blast.BlastException in project ice by JBEI.

the class Annotations method generate.

/**
 * Auto generate annotations for specified entry
 *
 * @param entryId       unique (local) identifier for entry
 * @param ownerFeatures whether to only include the features created by the requesting user
 * @return wrapper around generated annotations, if any are found
 */
public FeaturedDNASequence generate(long entryId, boolean ownerFeatures) {
    Entry entry = entryDAO.get(entryId);
    if (entry == null)
        throw new IllegalArgumentException("Could not retrieve entry with id \"" + entryId + "\"");
    Sequence sequence = sequenceDAO.getByEntry(entry);
    if (sequence == null)
        return null;
    String sequenceString = sequence.getSequence();
    BlastQuery query = new BlastQuery();
    query.setSequence(sequenceString);
    try {
        List<DNAFeature> features = featuresBlastDatabase.runBlast(query);
        FeaturedDNASequence dnaSequence = new FeaturedDNASequence();
        if (features.isEmpty())
            return dnaSequence;
        // check permissions
        Account account = accountDAO.getByEmail(userId);
        List<Group> groups = this.groupDAO.retrieveMemberGroups(account);
        for (DNAFeature dnaFeature : features) {
            Feature feature = this.featureDAO.get(dnaFeature.getId());
            if (feature == null)
                continue;
            List<Long> entries = this.sequenceFeatureDAO.getEntryIdsByFeature(feature);
            if (entries.isEmpty())
                continue;
            if (!isAdministrator()) {
                entries = this.permissionDAO.getCanReadEntries(account, groups, entries);
                if (entries.isEmpty())
                    continue;
            }
            if (ownerFeatures) {
                entries = this.entryDAO.filterByUserId(this.userId, entries);
            }
            if (entries != null && !entries.isEmpty()) {
                dnaFeature.getEntries().addAll(entries);
                dnaSequence.getFeatures().add(dnaFeature);
            }
        }
        dnaSequence.setLength(sequenceString.length());
        return dnaSequence;
    } catch (BlastException e) {
        Logger.error(e);
        return null;
    }
}
Also used : FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) DNAFeature(org.jbei.ice.lib.dto.DNAFeature) BlastQuery(org.jbei.ice.lib.dto.search.BlastQuery) DNAFeature(org.jbei.ice.lib.dto.DNAFeature) BlastException(org.jbei.ice.lib.search.blast.BlastException)

Example 8 with BlastException

use of org.jbei.ice.lib.search.blast.BlastException in project ice by JBEI.

the class SearchIndexes method runSearch.

/**
 * Executes search using parameters specified in the query.
 *
 * @param userId unique user identifier making the request. This can be null if the request is via web of
 *               registries
 * @param query  wrapper around search query
 * @return wrapper around the list of search results
 */
public SearchResults runSearch(String userId, SearchQuery query) {
    String queryString = query.getQueryString();
    HashMap<String, SearchResult> blastResults = null;
    // check if there is a blast result and run first
    if (query.hasBlastQuery()) {
        if (query.getBlastQuery().getBlastProgram() == null)
            query.getBlastQuery().setBlastProgram(BlastProgram.BLAST_N);
        try {
            StandardBlastDatabase blastDatabase = StandardBlastDatabase.getInstance();
            blastResults = blastDatabase.runBlast(query.getBlastQuery());
        } catch (BlastException e) {
            Logger.error("Exception running blast " + e.getMessage());
            return null;
        }
    }
    // if no other search query or filter and there are blast results (not null) then return the blast results
    if (StringUtils.isEmpty(queryString) && blastResults != null && !query.hasFilter()) {
        if (blastResults.isEmpty())
            return new SearchResults();
        int start = query.getParameters().getStart();
        int count = query.getParameters().getRetrieveCount();
        return HibernateSearch.getInstance().filterBlastResults(userId, start, count, query, blastResults);
    }
    // text query (may also include blast)
    // no filter type indicates a term or phrase query
    HibernateSearch hibernateSearch = HibernateSearch.getInstance();
    if (!StringUtils.isEmpty(queryString)) {
        HashMap<String, QueryType> terms = parseQueryString(queryString);
        return hibernateSearch.executeSearch(userId, terms, query, blastResults);
    } else {
        return hibernateSearch.executeSearchNoTerms(userId, blastResults, query);
    }
}
Also used : StandardBlastDatabase(org.jbei.ice.lib.search.blast.StandardBlastDatabase) HibernateSearch(org.jbei.ice.storage.hibernate.search.HibernateSearch) BlastException(org.jbei.ice.lib.search.blast.BlastException)

Aggregations

BlastException (org.jbei.ice.lib.search.blast.BlastException)8 Date (java.util.Date)4 Bl2SeqResult (org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult)4 DNAFeature (org.jbei.ice.lib.dto.DNAFeature)2 BlastQuery (org.jbei.ice.lib.dto.search.BlastQuery)2 BlastPlus (org.jbei.ice.lib.search.blast.BlastPlus)2 HibernateSearch (org.jbei.ice.storage.hibernate.search.HibernateSearch)2 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)1 StandardBlastDatabase (org.jbei.ice.lib.search.blast.StandardBlastDatabase)1