Search in sources :

Example 1 with Bl2SeqResult

use of org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult in project ice by JBEI.

the class BlastPlus method runBlast2Seq.

/**
     * Run the bl2seq program on multiple subjects.
     * <p/>
     * This method requires disk space write temporary files. It tries to clean up after itself.
     *
     * @param query   reference sequence.
     * @param subject query sequence.
     * @return List of output string from bl2seq program.
     * @throws BlastException
     */
public static List<Bl2SeqResult> runBlast2Seq(String query, String subject) throws BlastException {
    try {
        Path queryFilePath = Files.write(Files.createTempFile("query-", ".seq"), query.getBytes());
        Path subjectFilePath = Files.write(Files.createTempFile("subject-", ".seq"), subject.getBytes());
        if (queryFilePath == null || subjectFilePath == null)
            throw new BlastException("Subject or query is null");
        String blastN = Utils.getConfigValue(ConfigurationKey.BLAST_INSTALL_DIR) + File.separator + BlastProgram.BLAST_N.getName();
        String[] command = new String[] { blastN, "-query", queryFilePath.toString(), "-subject", subjectFilePath.toString(), "-outfmt", "10 score qstart qend qseq sstart send sseq sstrand" };
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        List<Bl2SeqResult> results = new ArrayList<>();
        while ((line = input.readLine()) != null) {
            String[] data = line.split(",");
            if (data.length != 8) {
                Logger.error("Invalid bl2seq result line obtained. skipping");
                continue;
            }
            int score = Integer.decode(data[0]);
            int queryStart = Integer.decode(data[1]);
            int queryEnd = Integer.decode(data[2]);
            int subjectStart = Integer.decode(data[4]);
            int subjectEnd = Integer.decode(data[5]);
            int orientation = "plus".equalsIgnoreCase(data[7]) ? 0 : 1;
            Bl2SeqResult result = new Bl2SeqResult(score, queryStart, queryEnd, data[3], subjectStart, subjectEnd, data[6], orientation);
            results.add(result);
        }
        input.close();
        Files.deleteIfExists(subjectFilePath);
        Files.deleteIfExists(queryFilePath);
        return results;
    } catch (IOException e) {
        throw new BlastException(e);
    }
}
Also used : Bl2SeqResult(org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult)

Example 2 with Bl2SeqResult

use of org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult in project ice by JBEI.

the class SequenceAnalysisController 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().getRecordType().equalsIgnoreCase("plasmid")) && ((Plasmid) sequence.getEntry()).getCircular();
    if (isCircular) {
        entrySequenceString += entrySequenceString;
    }
    try {
        List<Bl2SeqResult> bl2seqAlignmentResults = 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());
                }
                traceDao.save(traceSequence);
            }
        }
    } catch (BlastException e) {
        Logger.error(e);
    }
}
Also used : Bl2SeqResult(org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult) Date(java.util.Date) BlastException(org.jbei.ice.lib.search.blast.BlastException)

Example 3 with Bl2SeqResult

use of org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult in project ice by JBEI.

the class TraceSequences 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 = 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 : Bl2SeqResult(org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult) Date(java.util.Date) BlastException(org.jbei.ice.lib.search.blast.BlastException)

Aggregations

Bl2SeqResult (org.jbei.ice.lib.parsers.bl2seq.Bl2SeqResult)3 Date (java.util.Date)2 BlastException (org.jbei.ice.lib.search.blast.BlastException)2