use of org.jbei.ice.lib.parsers.genbank.GenBankParser in project ice by JBEI.
the class GeneralParser method registerParsers.
private void registerParsers() {
parsers.add(new GenBankParser());
parsers.add(new FastaParser());
parsers.add(new PlainParser());
}
use of org.jbei.ice.lib.parsers.genbank.GenBankParser 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);
}
}
use of org.jbei.ice.lib.parsers.genbank.GenBankParser in project ice by JBEI.
the class SBOLParser method parseToGenBank.
/**
* Parse the SBOL document to genbank format, save and associate with entry
*
* @param sbolDocument SBOL document to parse
* @param fileName name of file that was parsed to extract the SBOL information
* @param entry ICE entry to associate sequence document with
* @param uri optional uri to associate with sequence
* @return Sequence info data transfer object for saved sequence
*/
private SequenceInfo parseToGenBank(SBOLDocument sbolDocument, String fileName, Entry entry, String uri) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
String rdf;
// convert to rdf string to save raw document
try {
sbolDocument.write(out);
rdf = out.toString();
} catch (SBOLConversionException e) {
rdf = null;
}
// convert to genbank
Sequence sequence = null;
FeaturedDNASequence dnaSequence = null;
try {
out.reset();
SBOLWriter.write(sbolDocument, out, "GENBANK");
if (out.size() > 0) {
GenBankParser parser = new GenBankParser();
dnaSequence = parser.parse(IOUtils.lineIterator(new ByteArrayInputStream(out.toByteArray()), Charset.defaultCharset()));
sequence = SequenceUtil.dnaSequenceToSequence(dnaSequence);
}
} catch (SBOLConversionException | IOException e) {
Logger.error("Error converting SBOL to genBank: " + e.getMessage());
}
// convert to ice data model (sequence)
if (sequence == null) {
sequence = new Sequence();
}
if (!StringUtils.isEmpty(rdf))
sequence.setSequenceUser(rdf);
sequence.setFormat(SequenceFormat.SBOL2);
sequence.setEntry(entry);
if (fileName != null)
sequence.setFileName(fileName);
if (!StringUtils.isEmpty(uri))
sequence.setUri(uri);
sequence = DAOFactory.getSequenceDAO().create(sequence);
SequenceInfo sequenceInfo = new SequenceInfo();
sequenceInfo.setEntryId(entry.getId());
sequenceInfo.setSequence(dnaSequence);
sequenceInfo.setFormat(sequence.getFormat());
if (fileName != null)
sequenceInfo.setFilename(fileName);
return sequenceInfo;
}
use of org.jbei.ice.lib.parsers.genbank.GenBankParser 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
* @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) throws IOException {
try {
AbstractParser parser;
String sequenceString = Utils.getString(inputStream);
switch(detectFormat(sequenceString)) {
case GENBANK:
parser = new GenBankParser();
break;
case SBOL2:
SBOLParser sbolParser = new SBOLParser(this.userId, Long.toString(this.entry.getId()));
return sbolParser.parseToEntry(sequenceString, fileName);
case FASTA:
parser = new FastaParser();
break;
default:
case PLAIN:
parser = new PlainParser();
break;
}
// parse actual sequence
DNASequence sequence = parser.parse(sequenceString);
return save(sequence, sequenceString, fileName);
} catch (InvalidFormatParserException e) {
Logger.error(e);
throw new IOException(e);
}
}
Aggregations