use of org.jbei.ice.lib.parsers.InvalidFormatParserException in project ice by JBEI.
the class Sequences method parseSequence.
// public FeaturedDNASequence retrievePartSequence(String userId, String recordId) {
// Entry entry = hasEntry.getEntry(recordId);
// if (entry == null)
// throw new IllegalArgumentException("The part " + recordId + " could not be located");
//
// if (entry.getVisibility() == Visibility.REMOTE.getValue()) {
// WebEntries webEntries = new WebEntries();
// return webEntries.getSequence(recordId);
// }
//
// if (!new PermissionsController().isPubliclyVisible(entry))
// authorization.expectRead(userId, entry);
//
// boolean canEdit = authorization.canWrite(userId, entry);
// return getFeaturedSequence(entry, canEdit);
// }
public SequenceInfo parseSequence(InputStream inputStream, String fileName) throws InvalidFormatParserException {
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex != -1) {
String ext = fileName.substring(dotIndex + 1);
// unique case for sbol since it can result in multiple entries created
if ("rdf".equalsIgnoreCase(ext) || "xml".equalsIgnoreCase(ext) || "sbol".equalsIgnoreCase(ext)) {
// todo : cannot parse sbol yet
return null;
}
}
// parse actual sequence
try {
String sequenceString = IOUtils.toString(inputStream, Charset.defaultCharset());
FeaturedDNASequence dnaSequence = GeneralParser.parse(sequenceString);
if (dnaSequence == null)
throw new InvalidFormatParserException("Could not parse sequence string");
Sequence sequence = SequenceUtil.dnaSequenceToSequence(dnaSequence);
sequence.setSequenceUser(sequenceString);
if (!StringUtils.isBlank(fileName))
sequence.setFileName(fileName);
SequenceInfo info = sequence.toDataTransferObject();
info.setSequence(dnaSequence);
return info;
} catch (IOException e) {
throw new InvalidFormatParserException(e);
}
}
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
* @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);
}
}
use of org.jbei.ice.lib.parsers.InvalidFormatParserException in project ice by JBEI.
the class TraceSequences 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.getInstance().parse(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 FileResource method createSequenceModel.
/**
* Create a model of the uploaded sequence file. Note that this does not associate the sequence
* with any existing entry. It just parses the uploaded file
*/
@POST
@Path("sequence/model")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response createSequenceModel(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
final String fileName = contentDispositionHeader.getFileName();
Sequences sequences = new Sequences(requireUserId());
try {
return super.respond(sequences.parseSequence(fileInputStream, fileName));
} catch (InvalidFormatParserException e) {
throw new WebApplicationException(e.getMessage());
}
}
use of org.jbei.ice.lib.parsers.InvalidFormatParserException in project ice by JBEI.
the class FastaParser method parse.
@Override
public FeaturedDNASequence parse(Iterator<String> iterator, String... entryType) throws InvalidFormatParserException {
try {
String textSequence = getSequence(iterator);
textSequence = cleanSequence(textSequence);
textSequence = textSequence.replaceAll("\t", "\n");
try (BufferedReader br = new BufferedReader(new StringReader(textSequence))) {
FeaturedDNASequence sequence;
RichSequenceIterator richSequences;
if (entryType.length > 0 && entryType[0].equals("protein")) {
richSequences = IOTools.readFastaProtein(br, null);
} else {
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);
}
}
Aggregations