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);
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations