Search in sources :

Example 1 with InvalidFormatParserException

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

the class FastaParser method parse.

@Override
public FeaturedDNASequence parse(String textSequence) throws InvalidFormatParserException {
    try {
        textSequence = cleanSequence(textSequence);
        try (BufferedReader br = new BufferedReader(new StringReader(textSequence))) {
            FeaturedDNASequence sequence;
            RichSequenceIterator richSequences = IOTools.readFastaDNA(br, null);
            if (richSequences.hasNext()) {
                RichSequence richSequence = richSequences.nextRichSequence();
                sequence = new FeaturedDNASequence(richSequence.seqString(), new LinkedList<>());
            } else {
                throw new InvalidFormatParserException("No sequence found in sequence file!");
            }
            return sequence;
        }
    } catch (BioException | IOException e) {
        throw new InvalidFormatParserException("Couldn't parse FASTA sequence!", e);
    }
}
Also used : BioException(org.biojava.bio.BioException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) RichSequenceIterator(org.biojavax.bio.seq.RichSequenceIterator) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) IOException(java.io.IOException) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) LinkedList(java.util.LinkedList) RichSequence(org.biojavax.bio.seq.RichSequence)

Example 2 with InvalidFormatParserException

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

the class SBOLParser method parseToEntry.

public SequenceInfo parseToEntry(String textSequence, String fileName) throws InvalidFormatParserException {
    SBOLDocument document;
    try {
        document = SBOLReader.read(new ByteArrayInputStream(textSequence.getBytes(StandardCharsets.UTF_8)));
    } catch (SBOLValidationException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Invalid SBOL file: " + e.getMessage());
    } catch (IOException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Server error parsing file");
    } catch (SBOLConversionException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Error converting file to SBOL 2.0");
    }
    // parse raw document and return
    SequenceInfo sequenceInfo = parseToGenBank(document, fileName, entry, null);
    // document parsed successfully, go through module definitions
    for (ModuleDefinition moduleDefinition : document.getModuleDefinitions()) {
        try {
            createICEModuleDefinitionRecord(document, moduleDefinition);
        } catch (SBOLValidationException e) {
            Logger.error("Could not import module definition", e);
        }
    }
    // go through component definitions
    for (ComponentDefinition componentDefinition : document.getComponentDefinitions()) {
        try {
            createICEComponentDefinitionRecord(document, componentDefinition);
        } catch (SBOLValidationException e) {
            Logger.error("Could not import component definition", e);
        }
    }
    return sequenceInfo;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) IOException(java.io.IOException)

Example 3 with InvalidFormatParserException

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

the class SBOLParser method parseToEntry.

public SequenceInfo parseToEntry(InputStream stream, String fileName) throws InvalidFormatParserException {
    SBOLDocument document;
    try {
        document = SBOLReader.read(stream);
    } catch (SBOLValidationException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Invalid SBOL file: " + e.getMessage());
    } catch (IOException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Server error parsing file");
    } catch (SBOLConversionException e) {
        Logger.error(e);
        throw new InvalidFormatParserException("Error converting file to SBOL 2.0");
    }
    // parse raw document and return
    SequenceInfo sequenceInfo = parseToGenBank(document, fileName, entry, null);
    if (!this.extractHierarchy)
        return sequenceInfo;
    // document parsed successfully, go through module definitions
    for (ModuleDefinition moduleDefinition : document.getModuleDefinitions()) {
        try {
            createICEModuleDefinitionRecord(document, moduleDefinition);
        } catch (SBOLValidationException e) {
            Logger.error("Could not import module definition", e);
        }
    }
    // go through component definitions
    for (ComponentDefinition componentDefinition : document.getComponentDefinitions()) {
        try {
            createICEComponentDefinitionRecord(document, componentDefinition);
        } catch (SBOLValidationException e) {
            Logger.error("Could not import component definition", e);
        }
    }
    return sequenceInfo;
}
Also used : SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) IOException(java.io.IOException)

Example 4 with InvalidFormatParserException

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

the class PartTraceSequences method parseTraceSequence.

private boolean parseTraceSequence(String fileName, byte[] bytes) {
    DNASequence dnaSequence = null;
    // First try parsing as ABI
    ABIParser abiParser = new ABIParser();
    try {
        dnaSequence = abiParser.parse(bytes);
    } catch (InvalidFormatParserException e) {
    // 
    }
    if (dnaSequence == null) {
        // try parsing as fasta, genbank, etc
        dnaSequence = GeneralParser.parse(new String(bytes));
        if (dnaSequence == null || dnaSequence.getSequence() == null) {
            String errMsg = ("Could not parse \"" + fileName + "\". Only Fasta, GenBank & ABI files are supported.");
            Logger.error(errMsg);
            return false;
        }
    }
    TraceSequence traceSequence = importTraceSequence(fileName, dnaSequence.getSequence().toLowerCase(), new ByteArrayInputStream(bytes));
    if (traceSequence == null)
        return false;
    Sequence sequence = DAOFactory.getSequenceDAO().getByEntry(entry);
    if (sequence == null)
        return true;
    buildOrRebuildAlignment(traceSequence, sequence);
    return true;
}
Also used : DNASequence(org.jbei.ice.lib.dto.DNASequence) ABIParser(org.jbei.ice.lib.parsers.ABIParser) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) DNASequence(org.jbei.ice.lib.dto.DNASequence)

Example 5 with InvalidFormatParserException

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

the class PartSequence method parseSequenceFile.

/**
 * Parses a sequence in a file and associates it with the current entry
 *
 * @param inputStream      input stream of bytes representing the file
 * @param fileName         name of file being parsed
 * @param extractHierarchy for SBOL2 sequences only. If set to <code>true</code>, creates a hierarchy of ICE entries
 *                         as needed
 * @return wrapper around the internal model used to represent sequence information
 * @throws IOException on Exception parsing the contents of the file
 */
public SequenceInfo parseSequenceFile(InputStream inputStream, String fileName, boolean extractHierarchy) throws IOException {
    AbstractParser parser;
    // write sequence file to disk (tmp)
    String tmpDir = new ConfigurationSettings().getPropertyValue(ConfigurationKey.TEMPORARY_DIRECTORY);
    if (StringUtils.isEmpty(tmpDir))
        throw new IllegalArgumentException("Cannot parse sequence without valid tmp directory");
    Path tmpPath = Paths.get(tmpDir);
    if (!Files.isDirectory(tmpPath) || !Files.isWritable(tmpPath))
        throw new IllegalArgumentException("Cannot write to tmp directory: " + tmpPath.toString());
    Path sequencePath = Paths.get(tmpPath.toString(), UUID.randomUUID().toString() + "-" + fileName);
    Files.copy(inputStream, sequencePath, StandardCopyOption.REPLACE_EXISTING);
    // detect sequence
    SequenceFormat format;
    try (InputStream fileInputStream = Files.newInputStream(sequencePath);
        LineIterator iterator = IOUtils.lineIterator(fileInputStream, StandardCharsets.UTF_8)) {
        if (!iterator.hasNext())
            throw new IOException("Cannot read stream for " + fileName);
        String firstLine = iterator.next();
        format = SequenceUtil.detectFormat(firstLine);
    }
    // special handling for sbol format
    try {
        if (format == SBOL2) {
            SBOLParser sbolParser = new SBOLParser(this.userId, Long.toString(this.entry.getId()), extractHierarchy);
            return sbolParser.parseToEntry(Files.newInputStream(sequencePath), fileName);
        }
        switch(format) {
            case GENBANK:
                parser = new GenBankParser();
                break;
            case FASTA:
                parser = new FastaParser();
                break;
            default:
            case PLAIN:
                parser = new PlainParser();
                break;
        }
        LineIterator iterator = IOUtils.lineIterator(Files.newInputStream(sequencePath), StandardCharsets.UTF_8);
        SequenceFile sequenceFile = new SequenceFile();
        String entryType = this.entry.getRecordType();
        // special handling for SBOL (todo: clean this up in future release)
        FeaturedDNASequence dnaSequence = parser.parse(iterator, entryType);
        Sequence sequence = SequenceUtil.dnaSequenceToSequence(dnaSequence);
        if (sequence == null)
            throw new IOException("Could not create sequence object");
        // copy original sequence file to file system
        try {
            Files.copy(sequencePath, sequenceFile.getFilePath(), StandardCopyOption.REPLACE_EXISTING);
            sequence.setSequenceUser(sequenceFile.getFileName());
        } catch (Exception e) {
            // ok to ignore. Can get back sequence as long as sequence object is saved. cannot download original
            Logger.warn("Exception writing sequence to file: " + e.getMessage());
        }
        sequence.setFileName(fileName);
        sequence.setFormat(format);
        sequence = saveSequenceObject(sequence);
        SequenceInfo info = sequence.toDataTransferObject();
        info.setSequence(dnaSequence);
        return info;
    } catch (InvalidFormatParserException ifpe) {
        Logger.error(ifpe);
        return null;
    } finally {
        Files.deleteIfExists(sequencePath);
    }
}
Also used : Path(java.nio.file.Path) AbstractParser(org.jbei.ice.lib.parsers.AbstractParser) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) InputStream(java.io.InputStream) IOException(java.io.IOException) FastaParser(org.jbei.ice.lib.parsers.fasta.FastaParser) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) LineIterator(org.apache.commons.io.LineIterator) FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) IOException(java.io.IOException) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) GenBankParser(org.jbei.ice.lib.parsers.genbank.GenBankParser) SBOLParser(org.jbei.ice.lib.parsers.sbol.SBOLParser) PlainParser(org.jbei.ice.lib.parsers.PlainParser) ConfigurationSettings(org.jbei.ice.lib.config.ConfigurationSettings)

Aggregations

InvalidFormatParserException (org.jbei.ice.lib.parsers.InvalidFormatParserException)10 IOException (java.io.IOException)7 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)4 SequenceInfo (org.jbei.ice.lib.dto.entry.SequenceInfo)4 BufferedReader (java.io.BufferedReader)2 StringReader (java.io.StringReader)2 LinkedList (java.util.LinkedList)2 BioException (org.biojava.bio.BioException)2 RichSequence (org.biojavax.bio.seq.RichSequence)2 RichSequenceIterator (org.biojavax.bio.seq.RichSequenceIterator)2 DNASequence (org.jbei.ice.lib.dto.DNASequence)2 ABIParser (org.jbei.ice.lib.parsers.ABIParser)2 AbstractParser (org.jbei.ice.lib.parsers.AbstractParser)2 PlainParser (org.jbei.ice.lib.parsers.PlainParser)2 FastaParser (org.jbei.ice.lib.parsers.fasta.FastaParser)2 GenBankParser (org.jbei.ice.lib.parsers.genbank.GenBankParser)2 SBOLParser (org.jbei.ice.lib.parsers.sbol.SBOLParser)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1