use of com.google.api.expr.v1alpha1.Reference in project dishevelled by heuermh.
the class ImportGfa2Task method run.
@Override
public void run(final TaskMonitor taskMonitor) throws Exception {
taskMonitor.setTitle("Import a network in Graphical Fragment Assembly (GFA) 2.0 format");
final Map<String, Segment> segmentsById = new HashMap<String, Segment>();
taskMonitor.setStatusMessage("Reading segments from file ...");
try (BufferedReader readable = new BufferedReader(new FileReader(inputFile))) {
// stream segments, building cache
stream(readable, new Gfa2Adapter() {
@Override
protected boolean segment(final Segment segment) {
segmentsById.put(segment.getId(), segment);
return true;
}
});
}
taskMonitor.setStatusMessage("Finding reverse orientation references ...");
final Table<String, Orientation, Segment> segmentsByOrientation = HashBasedTable.create();
final List<Edge> edges = new ArrayList<Edge>();
final List<Gap> gaps = new ArrayList<Gap>();
final List<Path> paths = new ArrayList<Path>();
try (BufferedReader readable = new BufferedReader(new FileReader(inputFile))) {
// stream edges, gaps, and paths, looking for reverse orientation references
stream(readable, new Gfa2Adapter() {
private void putIfAbsent(final Reference reference) {
Segment segment = segmentsById.get(reference.getId());
if (segment == null) {
throw new RuntimeException("could not find segment by id " + reference.getId());
}
if (!segmentsByOrientation.contains(reference.getId(), reference.getOrientation())) {
segmentsByOrientation.put(reference.getId(), reference.getOrientation(), segment);
}
}
@Override
public boolean edge(final Edge edge) {
putIfAbsent(edge.getSource());
putIfAbsent(edge.getTarget());
edges.add(edge);
return true;
}
@Override
public boolean gap(final Gap gap) {
putIfAbsent(gap.getSource());
putIfAbsent(gap.getTarget());
gaps.add(gap);
return true;
}
@Override
public boolean path(final Path path) {
for (Reference reference : path.getReferences()) {
putIfAbsent(reference);
}
if (loadPaths) {
paths.add(path);
}
return true;
}
});
}
logger.info("read {} segments, {} edges, {} gaps, and {} paths from {}", new Object[] { segmentsById.size(), edges.size(), gaps.size(), paths.size(), inputFile });
segmentsById.clear();
taskMonitor.setStatusMessage("Building Cytoscape nodes from segments ...");
final CyNetwork network = applicationManager.getCurrentNetwork();
final Map<String, CyNode> nodes = new HashMap<String, CyNode>(segmentsByOrientation.size());
for (Table.Cell<String, Orientation, Segment> c : segmentsByOrientation.cellSet()) {
String id = c.getRowKey();
Orientation orientation = c.getColumnKey();
Segment segment = c.getValue();
String name = id + (orientation.isForward() ? "+" : "-");
if (!nodes.containsKey(name)) {
CyNode node = network.addNode();
CyTable nodeTable = network.getDefaultNodeTable();
CyRow nodeRow = nodeTable.getRow(node.getSUID());
Integer length = segment.getLength();
Integer readCount = segment.getReadCountOpt().orElse(null);
Integer fragmentCount = segment.getFragmentCountOpt().orElse(null);
Integer kmerCount = segment.getKmerCountOpt().orElse(null);
String sequenceChecksum = segment.containsSequenceChecksum() ? String.valueOf(segment.getSequenceChecksum()) : null;
String sequenceUri = segment.getSequenceUriOpt().orElse(null);
setValue(nodeTable, nodeRow, "name", String.class, name);
setValue(nodeTable, nodeRow, "length", Integer.class, length);
setValue(nodeTable, nodeRow, "readCount", Integer.class, readCount);
setValue(nodeTable, nodeRow, "fragmentCount", Integer.class, fragmentCount);
setValue(nodeTable, nodeRow, "kmerCount", Integer.class, kmerCount);
setValue(nodeTable, nodeRow, "sequenceChecksum", String.class, sequenceChecksum);
setValue(nodeTable, nodeRow, "sequenceUri", String.class, sequenceUri);
// default display length to length
Integer displayLength = length;
String sequence = orientation.isForward() ? segment.getSequence() : reverseComplement(segment.getSequence());
if (sequence != null) {
Integer sequenceLength = sequence.length();
String displaySequence = trimFromMiddle(sequence, displaySequenceLimit);
Integer displaySequenceLength = displaySequence.length();
if (loadSequences) {
setValue(nodeTable, nodeRow, "sequence", String.class, sequence);
}
setValue(nodeTable, nodeRow, "sequenceLength", Integer.class, sequenceLength);
setValue(nodeTable, nodeRow, "displaySequence", String.class, displaySequence);
setValue(nodeTable, nodeRow, "displaySequenceLength", Integer.class, displaySequenceLength);
// override display length with sequence length if necessary
if (length == null || length != sequenceLength) {
displayLength = sequenceLength;
}
}
StringBuilder sb = new StringBuilder();
sb.append(name);
if (displayLength != null) {
sb.append(" ");
sb.append(displayLength);
sb.append(" bp");
}
String displayName = sb.toString();
if (readCount != null) {
sb.append(" ");
sb.append(readCount);
sb.append(" reads");
}
if (fragmentCount != null) {
sb.append(" ");
sb.append(fragmentCount);
sb.append(" fragments");
}
if (kmerCount != null) {
sb.append(" ");
sb.append(kmerCount);
sb.append(" kmers");
}
String displayLabel = sb.toString();
setValue(nodeTable, nodeRow, "displayName", String.class, displayName);
setValue(nodeTable, nodeRow, "displayLength", Integer.class, displayLength);
setValue(nodeTable, nodeRow, "displayLabel", String.class, displayLabel);
nodes.put(name, node);
}
}
logger.info("converted segments and orientation to " + nodes.size() + " nodes");
segmentsByOrientation.clear();
taskMonitor.setStatusMessage("Building Cytoscape edges from edges and gaps ...");
for (Edge edge : edges) {
String sourceId = edge.getSource().getId();
String sourceOrientation = edge.getSource().isForwardOrientation() ? "+" : "-";
String targetId = edge.getTarget().getId();
String targetOrientation = edge.getTarget().isForwardOrientation() ? "+" : "-";
CyNode sourceNode = nodes.get(sourceId + sourceOrientation);
CyNode targetNode = nodes.get(targetId + targetOrientation);
CyEdge cyEdge = network.addEdge(sourceNode, targetNode, true);
CyTable edgeTable = network.getDefaultEdgeTable();
CyRow edgeRow = edgeTable.getRow(cyEdge.getSUID());
setValue(edgeTable, edgeRow, "id", String.class, edge.getIdOpt().orElse(null));
setValue(edgeTable, edgeRow, "type", String.class, "edge");
setValue(edgeTable, edgeRow, "sourceId", String.class, sourceId);
setValue(edgeTable, edgeRow, "sourceOrientation", String.class, sourceOrientation);
setValue(edgeTable, edgeRow, "targetId", String.class, targetId);
setValue(edgeTable, edgeRow, "targetOrientation", String.class, targetOrientation);
setValue(edgeTable, edgeRow, "sourceStart", String.class, edge.getSourceStart().toString());
setValue(edgeTable, edgeRow, "sourceEnd", String.class, edge.getSourceEnd().toString());
setValue(edgeTable, edgeRow, "targetStart", String.class, edge.getTargetStart().toString());
setValue(edgeTable, edgeRow, "targetEnd", String.class, edge.getTargetEnd().toString());
setValue(edgeTable, edgeRow, "alignment", String.class, edge.hasAlignment() ? edge.getAlignment().toString() : null);
setValue(edgeTable, edgeRow, "readCount", Integer.class, edge.getReadCountOpt().orElse(null));
setValue(edgeTable, edgeRow, "fragmentCount", Integer.class, edge.getFragmentCountOpt().orElse(null));
setValue(edgeTable, edgeRow, "kmerCount", Integer.class, edge.getKmerCountOpt().orElse(null));
setValue(edgeTable, edgeRow, "mappingQuality", Integer.class, edge.getMappingQualityOpt().orElse(null));
setValue(edgeTable, edgeRow, "mismatchCount", Integer.class, edge.getMismatchCountOpt().orElse(null));
}
logger.info("converted edges to " + edges.size() + " edges");
for (Gap gap : gaps) {
String sourceId = gap.getSource().getId();
String sourceOrientation = gap.getSource().isForwardOrientation() ? "+" : "-";
String targetId = gap.getTarget().getId();
String targetOrientation = gap.getTarget().isForwardOrientation() ? "+" : "-";
CyNode sourceNode = nodes.get(sourceId + sourceOrientation);
CyNode targetNode = nodes.get(targetId + targetOrientation);
CyEdge edge = network.addEdge(sourceNode, targetNode, true);
CyTable edgeTable = network.getDefaultEdgeTable();
CyRow edgeRow = edgeTable.getRow(edge.getSUID());
setValue(edgeTable, edgeRow, "id", String.class, gap.getIdOpt().orElse(null));
setValue(edgeTable, edgeRow, "type", String.class, "gap");
setValue(edgeTable, edgeRow, "sourceId", String.class, sourceId);
setValue(edgeTable, edgeRow, "sourceOrientation", String.class, sourceOrientation);
setValue(edgeTable, edgeRow, "targetId", String.class, targetId);
setValue(edgeTable, edgeRow, "targetOrientation", String.class, targetOrientation);
setValue(edgeTable, edgeRow, "distance", Integer.class, gap.getDistance());
setValue(edgeTable, edgeRow, "variance", Integer.class, gap.getVarianceOpt().orElse(null));
}
logger.info("converted gaps to " + gaps.size() + " edges");
nodes.clear();
edges.clear();
gaps.clear();
// pass paths to AssemblyApp if requested
if (loadPaths && !paths.isEmpty()) {
taskMonitor.setStatusMessage("Loading paths in path view ...");
assemblyModel.setInputFileName(inputFile.toString());
// todo: convert to gfa1 paths?
// note paths in gfa2 can have references to segments, edges, or other groups
// assemblyModel.setPaths(paths, traversalsByPathName);
}
}
use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.
the class ExecuteTest method testRequestAndMarshall.
@Test
public void testRequestAndMarshall() throws Exception {
final WebProcessingClient client = new WebProcessingClient(new URL("http://test.com"), null, WPSVersion.v100);
final ExecuteRequest request = client.createExecute();
final Execute execute = request.getContent();
final GeographicCRS epsg4326 = CommonCRS.WGS84.geographic();
final GeneralEnvelope env = new GeneralEnvelope(epsg4326);
env.setRange(0, 10, 10);
env.setRange(1, 10, 10);
execute.setIdentifier("identifier");
final List<DataInput> inputs = execute.getInput();
inputs.add(new DataInput("literal", new Data(new LiteralValue("10", null, null))));
inputs.add(new DataInput("bbox", new Data(new BoundingBoxType(env))));
inputs.add(new DataInput("complex", new Data(new Format("UTF-8", WPSMimeType.APP_GML.val(), WPSSchema.OGC_GML_3_1_1.getValue(), null), new PointType(new DirectPosition2D(epsg4326, 0, 0)))));
inputs.add(new DataInput("reference", new Reference("http://link.to/reference/", null, null)));
execute.getOutput().add(new OutputDefinition("output", false));
assertEquals("WPS", execute.getService());
assertEquals("1.0.0", execute.getVersion().toString());
assertEquals(execute.getIdentifier().getValue(), "identifier");
final StringWriter stringWriter = new StringWriter();
final Marshaller marshaller = WPSMarshallerPool.getInstance().acquireMarshaller();
marshaller.marshal(execute, stringWriter);
String result = stringWriter.toString();
try (final InputStream expected = expectedRequest()) {
assertXmlEquals(expected, result, "xmlns:*", "crs", "srsName");
}
WPSMarshallerPool.getInstance().recycle(marshaller);
}
use of com.google.api.expr.v1alpha1.Reference in project eclipselink by eclipse-ee4j.
the class MetadataResource method buildQuerySchema.
private ResourceSchema buildQuerySchema(PersistenceContext context, DatabaseQuery query) {
final ResourceSchema schema = new ResourceSchema();
schema.setTitle(query.getName());
schema.setSchema(HrefHelper.buildQueryMetadataHref(context, query.getName()) + "#");
schema.addAllOf(new Reference(HrefHelper.buildBaseRestSchemaRef("#/collectionBaseResource")));
// Link
final String method = query.isReadQuery() ? "GET" : "POST";
schema.setLinks((new ItemLinksBuilder()).addExecute(HrefHelper.buildQueryHref(context, query.getName(), getQueryParamString(query)), method).getList());
// Definitions
if (query.isReportQuery()) {
// In case of report query we need to define a returned type
final ResourceSchema returnType = new ResourceSchema();
query.checkPrepare((AbstractSession) context.getServerSession(), new DatabaseRecord());
for (ReportItem item : ((ReportQuery) query).getItems()) {
final Property property;
if (item.getMapping() != null) {
if (item.getAttributeExpression() != null && item.getAttributeExpression().isMapEntryExpression()) {
if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
property = buildProperty(context, Map.Entry.class);
} else {
property = buildProperty(context, ((Class<?>) item.getMapping().getContainerPolicy().getKeyType()));
}
} else {
property = buildProperty(context, item.getMapping().getAttributeClassification());
}
} else if (item.getResultType() != null) {
property = buildProperty(context, item.getResultType());
} else if (item.getDescriptor() != null) {
property = buildProperty(context, item.getDescriptor().getJavaClass());
} else if (item.getAttributeExpression() != null && item.getAttributeExpression().isConstantExpression()) {
property = buildProperty(context, ((ConstantExpression) item.getAttributeExpression()).getValue().getClass());
} else {
// Use Object.class by default.
property = buildProperty(context, Object.class);
}
returnType.addProperty(item.getName(), property);
}
schema.addDefinition("result", returnType);
final Property items = new Property();
items.setType("array");
items.setItems(new Property("#/definitions/result"));
schema.addProperty("items", items);
} else {
// Read all query. Each item is an entity. Make a JSON pointer.
if (query.getReferenceClassName() != null) {
final Property items = new Property();
items.setType("array");
items.setItems(new Property(HrefHelper.buildEntityMetadataHref(context, query.getReferenceClass().getSimpleName()) + "#"));
schema.addProperty("items", items);
}
}
return schema;
}
use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.
the class ConvertersTestUtils method createReference.
/**
* Helper method that creates a Reference with all its field filled
* @param mimeType can be null
* @param encoding can be null
* @param schema can be null
* @param url must be set
* @return the reference with its field filled using the above parameters
*/
public static final Reference createReference(String version, String mimeType, String encoding, String schema, URL url) {
final Reference reference = new Reference();
reference.setMimeType(mimeType);
reference.setEncoding(encoding);
reference.setSchema(schema);
reference.setHref(url.toString());
return reference;
}
use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.
the class FeatureSetToReferenceConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Reference convert(final FeatureSet source, final Map<String, Object> params) throws UnconvertibleObjectException {
if (params.get(TMP_DIR_PATH) == null) {
throw new UnconvertibleObjectException("The output directory should be defined.");
}
if (params.get(TMP_DIR_URL) == null) {
throw new UnconvertibleObjectException("The output directory URL should be defined.");
}
if (source == null) {
throw new UnconvertibleObjectException("The output data should be defined.");
}
// TODO : useless test, null test above is all we need, fix this and other converters
if (!(source instanceof FeatureSet)) {
throw new UnconvertibleObjectException("The requested output data is not an instance of FeatureSet.");
}
Reference reference = new Reference();
reference.setMimeType((String) params.get(MIME));
reference.setEncoding((String) params.get(ENCODING));
final FeatureType ft;
try {
ft = source.getType();
} catch (DataStoreException ex) {
throw new UnconvertibleObjectException("Can't write acess FeatureSet type.", ex);
}
final String namespace = NamesExt.getNamespace(ft.getName());
final Map<String, String> schemaLocation = new HashMap<>();
final String randomFileName = UUID.randomUUID().toString();
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
// create file
final Path dataFile = buildPath(params, randomFileName + ".json");
try (OutputStream fos = Files.newOutputStream(dataFile, CREATE, TRUNCATE_EXISTING, WRITE);
GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS);
Stream<Feature> st = source.features(false)) {
Iterator<Feature> iterator = st.iterator();
while (iterator.hasNext()) {
Feature next = iterator.next();
Feature neww = writer.next();
FeatureExt.copy(next, neww, false);
writer.write();
}
} catch (DataStoreException e) {
throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
} catch (IOException e) {
throw new UnconvertibleObjectException(e);
}
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
reference.setSchema(null);
} else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
try {
reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
schemaLocation.put(namespace, reference.getSchema());
} catch (JAXBException ex) {
throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
}
// Write Feature
final JAXPStreamFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
// create file
final Path dataFile = buildPath(params, randomFileName + ".xml");
try (final OutputStream dataStream = Files.newOutputStream(dataFile);
final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
// Write feature in file
featureWriter.write(source, dataStream);
final String relLoc = getRelativeLocation(dataFile, params);
reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
} catch (XMLStreamException ex) {
throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
} catch (DataStoreException ex) {
throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
} catch (FeatureStoreRuntimeException ex) {
throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
} catch (Exception ex) {
throw new UnconvertibleObjectException(ex);
}
} else {
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
}
return reference;
}
Aggregations