Search in sources :

Example 1 with SearchResult

use of org.jbei.ice.lib.dto.search.SearchResult in project ice by JBEI.

the class BlastPlus method parseBlastOutputLine.

/**
     * Parses a blast output that represents a single hit
     *
     * @param line blast output for hit
     * @return object wrapper around details of the hit
     */
private static SearchResult parseBlastOutputLine(String[] line) {
    // extract part information
    PartData view = new PartData(EntryType.nameToType(line[1]));
    view.setId(Long.decode(line[0]));
    view.setName(line[2]);
    view.setPartId(line[3]);
    String summary = DAOFactory.getEntryDAO().getEntrySummary(view.getId());
    view.setShortDescription(summary);
    //search result object
    SearchResult searchResult = new SearchResult();
    searchResult.setEntryInfo(view);
    searchResult.seteValue(line[9]);
    searchResult.setScore(Float.valueOf(line[11]));
    searchResult.setAlignment(line[13]);
    searchResult.setQueryLength(Integer.parseInt(line[12]));
    searchResult.setNident(Integer.parseInt(line[13]));
    return searchResult;
}
Also used : PartData(org.jbei.ice.lib.dto.entry.PartData) SearchResult(org.jbei.ice.lib.dto.search.SearchResult)

Example 2 with SearchResult

use of org.jbei.ice.lib.dto.search.SearchResult in project ice by JBEI.

the class BlastPlus method processBlastOutput.

/**
     * Processes the result of a blast search
     *
     * @param blastOutput result output from running blast on the command line
     * @param queryLength length of query sequence
     * @return mapping of entryId to search result object containing information about the blast search for that particular hit
     */
private static LinkedHashMap<String, SearchResult> processBlastOutput(String blastOutput, int queryLength) {
    LinkedHashMap<String, SearchResult> hashMap = new LinkedHashMap<>();
    try (CSVReader reader = new CSVReader(new StringReader(blastOutput))) {
        List<String[]> lines = reader.readAll();
        reader.close();
        for (String[] line : lines) {
            SearchResult info = parseBlastOutputLine(line);
            info.setQueryLength(queryLength);
            String idString = Long.toString(info.getEntryInfo().getId());
            SearchResult currentResult = hashMap.get(idString);
            // if there is an existing record for same entry with a lower relative score then replace
            if (currentResult == null)
                hashMap.put(idString, info);
        }
    } catch (IOException e) {
        Logger.error(e);
        return null;
    }
    return hashMap;
}
Also used : CSVReader(com.opencsv.CSVReader) SearchResult(org.jbei.ice.lib.dto.search.SearchResult)

Example 3 with SearchResult

use of org.jbei.ice.lib.dto.search.SearchResult in project ice by JBEI.

the class StandardBlastDatabase method processBlastOutput.

/**
 * Processes the result of a blast search
 *
 * @param blastOutput result output from running blast on the command line
 * @param queryLength length of query sequence
 * @return mapping of entryId to search result object containing information about the blast search for that particular hit
 */
private LinkedHashMap<String, SearchResult> processBlastOutput(String blastOutput, int queryLength) {
    LinkedHashMap<String, SearchResult> hashMap = new LinkedHashMap<>();
    try (CSVReader reader = new CSVReader(new StringReader(blastOutput))) {
        List<String[]> lines = reader.readAll();
        reader.close();
        for (String[] line : lines) {
            SearchResult info = parseBlastOutputLine(line);
            if (info == null)
                continue;
            info.setQueryLength(queryLength);
            String idString = Long.toString(info.getEntryInfo().getId());
            // if there is an existing record for same entry with a lower relative score then replace
            hashMap.putIfAbsent(idString, info);
        }
    } catch (IOException | CsvException e) {
        Logger.error(e);
        return null;
    }
    return hashMap;
}
Also used : CSVReader(com.opencsv.CSVReader) StringReader(java.io.StringReader) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) IOException(java.io.IOException) CsvException(com.opencsv.exceptions.CsvException)

Example 4 with SearchResult

use of org.jbei.ice.lib.dto.search.SearchResult in project ice by JBEI.

the class HibernateSearch method filterBlastResults.

/**
 * Intended to be called after running a blast search to filter the results. Blast search runs
 * on a fasta file which contains all the sequences. Hibernate search has a security filter for permissions
 * and is therefore used to filter out the blast results, as well as to filter based on the entry
 * attributes not handled by blast; such as "has sequence" and "bio-safety level"
 *
 * @param userId       identifier for account of user performing search
 * @param start        paging start
 * @param count        maximum number of results to return
 * @param blastResults raw results of the blast search
 * @return wrapper around list of filtered results
 */
public SearchResults filterBlastResults(String userId, int start, int count, SearchQuery searchQuery, final HashMap<String, SearchResult> blastResults) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Entry.class).get();
    Query query = qb.keyword().onField("visibility").matching(Visibility.OK.getValue()).createQuery();
    // todo : there is a limit of 1024 boolean clauses so return only return top blast results
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(query, BooleanClause.Occur.FILTER);
    // wrap Lucene query in a org.hibernate.Query
    Class<?>[] classes = SearchFieldFactory.classesForTypes(searchQuery.getEntryTypes());
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), classes);
    // enable security filter if an admin
    checkEnableSecurityFilter(userId, fullTextQuery);
    // enable has attachment/sequence/sample (if needed)
    checkEnableHasAttribute(fullTextQuery, searchQuery.getParameters());
    // bio-safety level
    if (searchQuery.getBioSafetyOption() != null) {
        TermContext levelContext = qb.keyword();
        Query biosafetyQuery = levelContext.onField("bioSafetyLevel").ignoreFieldBridge().matching(searchQuery.getBioSafetyOption().getValue()).createQuery();
        builder.add(biosafetyQuery, BooleanClause.Occur.MUST);
    }
    // execute search
    fullTextQuery.setProjection("id");
    // list contains an object array with one Long object
    List<?> luceneResult = fullTextQuery.list();
    HashSet<String> resultSet = new HashSet<>();
    // page
    for (Object object : luceneResult) {
        Long result = (Long) ((Object[]) object)[0];
        resultSet.add(result.toString());
    }
    blastResults.keySet().removeIf(key -> !resultSet.contains(key));
    SearchResult[] searchResults = new SearchResult[count];
    int limit = Math.min((start + count), blastResults.size());
    LinkedList<SearchResult> list = new LinkedList<>(Arrays.asList(blastResults.values().toArray(searchResults)).subList(start, limit));
    SearchResults results = new SearchResults();
    results.setResultCount(blastResults.size());
    results.setResults(list);
    return results;
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) SearchQuery(org.jbei.ice.lib.dto.search.SearchQuery) FullTextQuery(org.hibernate.search.FullTextQuery) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) SearchResults(org.jbei.ice.lib.dto.search.SearchResults) TermContext(org.hibernate.search.query.dsl.TermContext) Entry(org.jbei.ice.storage.model.Entry) FullTextQuery(org.hibernate.search.FullTextQuery) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 5 with SearchResult

use of org.jbei.ice.lib.dto.search.SearchResult in project ice by JBEI.

the class Entries method getSearchResults.

private List<Long> getSearchResults(SearchQuery searchQuery) {
    SearchIndexes searchIndexes = new SearchIndexes();
    SearchResults searchResults = searchIndexes.runSearch(userId, searchQuery);
    // todo : inefficient: have search return ids only
    List<Long> results = new LinkedList<>();
    for (SearchResult result : searchResults.getResults()) {
        results.add(result.getEntryInfo().getId());
    }
    return results;
}
Also used : SearchIndexes(org.jbei.ice.lib.search.SearchIndexes) SearchResult(org.jbei.ice.lib.dto.search.SearchResult) SearchResults(org.jbei.ice.lib.dto.search.SearchResults)

Aggregations

SearchResult (org.jbei.ice.lib.dto.search.SearchResult)8 PartData (org.jbei.ice.lib.dto.entry.PartData)4 SearchResults (org.jbei.ice.lib.dto.search.SearchResults)4 Session (org.hibernate.Session)3 FullTextQuery (org.hibernate.search.FullTextQuery)3 FullTextSession (org.hibernate.search.FullTextSession)3 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)3 CSVReader (com.opencsv.CSVReader)2 CsvException (com.opencsv.exceptions.CsvException)2 IOException (java.io.IOException)2 TermContext (org.hibernate.search.query.dsl.TermContext)2 SearchQuery (org.jbei.ice.lib.dto.search.SearchQuery)2 BioSafetyOption (org.jbei.ice.lib.shared.BioSafetyOption)2 Entry (org.jbei.ice.storage.model.Entry)2 StringReader (java.io.StringReader)1 IllegalSymbolException (org.biojava.bio.symbol.IllegalSymbolException)1 EntryType (org.jbei.ice.lib.dto.entry.EntryType)1 FieldFilter (org.jbei.ice.lib.dto.search.FieldFilter)1 QueryType (org.jbei.ice.lib.search.QueryType)1 SearchIndexes (org.jbei.ice.lib.search.SearchIndexes)1