use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper in project ice by JBEI.
the class RemoteEntriesAsCSV method writeDataEntries.
protected void writeDataEntries(RemotePartner partner, List<PartData> entries, List<EntryField> fields, CSVWriter writer, ZipOutputStream zos) {
if (entries == null)
return;
for (PartData partData : entries) {
String[] line = new String[fields.size() + 4];
line[0] = partner.getUrl();
line[1] = new Date(partData.getCreationTime()).toString();
line[2] = partData.getPartId();
int i = 2;
for (EntryField field : fields) {
line[i + 1] = PartDataUtil.entryFieldToValue(partData, field);
i += 1;
}
// write sequence to zip file
if (partData.isHasSequence()) {
try {
// get remote sequence
FeaturedDNASequence featuredDNASequence = remoteEntries.getPublicEntrySequence(partner.getId(), Long.toString(partData.getId()));
if (featuredDNASequence != null) {
String name = partData.getPartId() + ".gb";
// write sequence to zip
line[i + 1] = name;
Sequence sequence = SequenceController.dnaSequenceToSequence(featuredDNASequence);
GenbankFormatter genbankFormatter = new GenbankFormatter(name);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
genbankFormatter.format(sequence, byteStream);
ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
putZipEntry(wrapper, zos);
} else {
line[i + 1] = "";
}
} catch (Exception e) {
line[i + 1] = "";
}
} else {
line[i + 1] = "";
}
writer.writeNext(line);
}
}
use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper in project ice by JBEI.
the class FileResource method downloadSequence.
@GET
@Path("{partId}/sequence/{type}")
public Response downloadSequence(@PathParam("partId") final long partId, @PathParam("type") final String downloadType, @DefaultValue("-1") @QueryParam("remoteId") long remoteId, @QueryParam("sid") String sid) {
if (StringUtils.isEmpty(sessionId))
sessionId = sid;
final String userId = getUserId(sessionId);
final ByteArrayWrapper wrapper;
if (remoteId != -1) {
RemoteSequence sequence = new RemoteSequence(remoteId, partId);
wrapper = sequence.get(downloadType);
} else {
wrapper = sequenceController.getSequenceFile(userId, partId, downloadType);
}
StreamingOutput stream = output -> {
final ByteArrayInputStream input = new ByteArrayInputStream(wrapper.getBytes());
ByteStreams.copy(input, output);
};
return addHeaders(Response.ok(stream), wrapper.getName());
}
use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper in project ice by JBEI.
the class RemoteEntriesAsCSV method writeLocalEntries.
protected void writeLocalEntries(List<Entry> entries, List<EntryField> fields, CSVWriter writer, ZipOutputStream zos) {
if (entries == null)
return;
SequenceDAO sequenceDAO = DAOFactory.getSequenceDAO();
Configuration configuration = DAOFactory.getConfigurationDAO().get(ConfigurationKey.URI_PREFIX);
String thisUrl = configuration == null ? "" : configuration.getValue();
for (Entry entry : entries) {
String[] line = new String[fields.size() + 4];
line[0] = thisUrl;
line[1] = entry.getCreationTime().toString();
line[2] = entry.getPartNumber();
int i = 2;
for (EntryField field : fields) {
line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
i += 1;
}
// write sequence to zip file
long entryId = entry.getId();
if (sequenceDAO.hasSequence(entryId)) {
String name = entry.getPartNumber() + ".gb";
try {
Sequence sequence = sequenceDAO.getByEntry(entry);
line[i + 1] = name;
GenbankFormatter genbankFormatter = new GenbankFormatter(name);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
genbankFormatter.format(sequence, byteStream);
ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
putZipEntry(wrapper, zos);
} catch (Exception e) {
line[i + 1] = "";
}
} else {
line[i + 1] = "";
}
writer.writeNext(line);
}
}
use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper in project ice by JBEI.
the class RemoteEntriesAsCSV method writeZip.
private boolean writeZip(File tmpZip, ZipOutputStream zos) {
try {
// write the csv file
FileInputStream fis = new FileInputStream(csvPath.toFile());
ByteArrayWrapper wrapper = new ByteArrayWrapper(IOUtils.toByteArray(fis), "entries.csv");
putZipEntry(wrapper, zos);
zos.close();
csvPath = tmpZip.toPath();
return true;
} catch (Exception e) {
Logger.error(e);
return false;
}
}
use of org.jbei.ice.lib.entry.sequence.ByteArrayWrapper in project ice by JBEI.
the class RemoteSequence method get.
public ByteArrayWrapper get(String type) {
FeaturedDNASequence featuredDNASequence = remoteContact.getPublicEntrySequence(partner.getUrl(), Long.toString(remotePartId), partner.getApiKey());
if (featuredDNASequence == null)
return new ByteArrayWrapper(new byte[] { '\0' }, "no_sequence");
String name = featuredDNASequence.getName();
try {
Sequence sequence = SequenceController.dnaSequenceToSequence(featuredDNASequence);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
AbstractFormatter formatter;
switch(type.toLowerCase()) {
case "genbank":
default:
name = name + ".gb";
formatter = new GenbankFormatter(name);
break;
case "fasta":
formatter = new FastaFormatter();
break;
case "sbol1":
formatter = new SBOLFormatter();
name = name + ".xml";
break;
case "sbol2":
formatter = new SBOL2Formatter();
name = name + ".xml";
break;
case "pigeoni":
URI uri = PigeonSBOLv.generatePigeonVisual(sequence);
byte[] bytes = IOUtils.toByteArray(uri.toURL().openStream());
return new ByteArrayWrapper(bytes, name + ".png");
case "pigeons":
String sequenceString = PigeonSBOLv.generatePigeonScript(sequence);
name = name + ".txt";
return new ByteArrayWrapper(sequenceString.getBytes(), name);
}
formatter.format(sequence, byteStream);
return new ByteArrayWrapper(byteStream.toByteArray(), name);
} catch (Exception e) {
Logger.error(e);
return new ByteArrayWrapper(new byte[] { '\0' }, "no_sequence");
}
}
Aggregations