Search in sources :

Example 1 with Reference

use of org.molgenis.emx2.Reference in project xtext-core by eclipse.

the class Bug311337TestLanguageSemanticSequencer method sequence.

@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
    EPackage epackage = semanticObject.eClass().getEPackage();
    ParserRule rule = context.getParserRule();
    Action action = context.getAssignedAction();
    Set<Parameter> parameters = context.getEnabledBooleanParameters();
    if (epackage == Bug311337Package.eINSTANCE)
        switch(semanticObject.eClass().getClassifierID()) {
            case Bug311337Package.CHILD:
                sequence_Child(context, (Child) semanticObject);
                return;
            case Bug311337Package.DEFINITION:
                sequence_Definition(context, (Definition) semanticObject);
                return;
            case Bug311337Package.MODEL:
                sequence_Model(context, (Model) semanticObject);
                return;
            case Bug311337Package.NESTED_REF:
                sequence_Reference(context, (NestedRef) semanticObject);
                return;
            case Bug311337Package.REFERENCE:
                sequence_Reference(context, (Reference) semanticObject);
                return;
        }
    if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) NestedRef(org.eclipse.xtext.linking.lazy.bug311337.NestedRef) Action(org.eclipse.xtext.Action) Reference(org.eclipse.xtext.linking.lazy.bug311337.Reference) Definition(org.eclipse.xtext.linking.lazy.bug311337.Definition) Model(org.eclipse.xtext.linking.lazy.bug311337.Model) Parameter(org.eclipse.xtext.Parameter) Child(org.eclipse.xtext.linking.lazy.bug311337.Child) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with Reference

use of org.molgenis.emx2.Reference in project xtext-core by eclipse.

the class NestedRefImpl method basicSetLeft.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetLeft(Reference newLeft, NotificationChain msgs) {
    Reference oldLeft = left;
    left = newLeft;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, Bug311337Package.NESTED_REF__LEFT, oldLeft, newLeft);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : Reference(org.eclipse.xtext.linking.lazy.bug311337.Reference) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Example 3 with Reference

use of org.molgenis.emx2.Reference in project dishevelled by heuermh.

the class AssemblyModel method traversalsFor.

/**
 * Create and return a list of path traversals for the specified path.
 *
 * @param path path, must not be null
 * @return a list of path traversals for the specified path
 */
static List<Traversal> traversalsFor(final Path path) {
    checkNotNull(path);
    int size = path.getSegments().size();
    List<Traversal> traversals = new ArrayList<Traversal>(size);
    Reference source = null;
    Reference target = null;
    String overlap = null;
    Map<String, Annotation> emptyAnnotations = Collections.emptyMap();
    for (int i = 0; i < size; i++) {
        target = path.getSegments().get(i);
        if (i > 0) {
            overlap = (path.getOverlaps() != null && path.getOverlaps().size() > i) ? path.getOverlaps().get(i - 1) : null;
        }
        if (source != null) {
            Traversal traversal = new Traversal(path.getName(), i - 1, source, target, overlap, emptyAnnotations);
            traversals.add(traversal);
        }
        source = target;
    }
    return traversals;
}
Also used : Reference(org.dishevelled.bio.assembly.gfa1.Reference) ArrayList(java.util.ArrayList) Traversal(org.dishevelled.bio.assembly.gfa1.Traversal) Annotation(org.dishevelled.bio.annotation.Annotation)

Example 4 with Reference

use of org.molgenis.emx2.Reference in project dishevelled by heuermh.

the class ImportGfa1Task method run.

@Override
public void run(final TaskMonitor taskMonitor) throws Exception {
    taskMonitor.setTitle("Import a network in Graphical Fragment Assembly (GFA) 1.0 format");
    final Map<String, Segment> segmentsById = new HashMap<String, Segment>();
    final Table<String, Orientation, Segment> segmentsByOrientation = HashBasedTable.create();
    final ListMultimap<String, Traversal> traversalsByPathName = ArrayListMultimap.create();
    taskMonitor.setStatusMessage("Reading segments from file ...");
    try (BufferedReader readable = new BufferedReader(new FileReader(inputFile))) {
        // stream segments, building cache
        stream(readable, new Gfa1Adapter() {

            @Override
            protected boolean segment(final Segment segment) {
                segmentsById.put(segment.getId(), segment);
                return true;
            }
        });
    }
    taskMonitor.setStatusMessage("Finding reverse orientation references ...");
    final List<Path> paths = new ArrayList<Path>();
    final List<Link> links = new ArrayList<Link>();
    try (BufferedReader readable = new BufferedReader(new FileReader(inputFile))) {
        // stream paths and links, looking for reverse orientation references
        stream(readable, new Gfa1Adapter() {

            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
            protected boolean path(final Path path) {
                for (Reference reference : path.getSegments()) {
                    putIfAbsent(reference);
                }
                if (loadPaths) {
                    paths.add(path);
                }
                return true;
            }

            @Override
            protected boolean link(final Link link) {
                putIfAbsent(link.getSource());
                putIfAbsent(link.getTarget());
                links.add(link);
                return true;
            }

            @Override
            protected boolean traversal(final Traversal traversal) {
                traversalsByPathName.put(traversal.getPathName(), traversal);
                return true;
            }
        });
    }
    logger.info("read {} segments, {} links, {} paths, and {} traversals from {}", new Object[] { segmentsById.size(), links.size(), paths.size(), traversalsByPathName.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.getLengthOpt().orElse(null);
            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 links ...");
    for (Link link : links) {
        String sourceId = link.getSource().getId();
        String sourceOrientation = link.getSource().isForwardOrientation() ? "+" : "-";
        String targetId = link.getTarget().getId();
        String targetOrientation = link.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, link.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, "overlap", String.class, link.getOverlapOpt().orElse(null));
        setValue(edgeTable, edgeRow, "readCount", Integer.class, link.getReadCountOpt().orElse(null));
        setValue(edgeTable, edgeRow, "fragmentCount", Integer.class, link.getFragmentCountOpt().orElse(null));
        setValue(edgeTable, edgeRow, "kmerCount", Integer.class, link.getKmerCountOpt().orElse(null));
        setValue(edgeTable, edgeRow, "mappingQuality", Integer.class, link.getMappingQualityOpt().orElse(null));
        setValue(edgeTable, edgeRow, "mismatchCount", Integer.class, link.getMismatchCountOpt().orElse(null));
    }
    logger.info("converted links to " + links.size() + " edges");
    nodes.clear();
    links.clear();
    // pass paths to AssemblyApp if requested
    if (loadPaths && !paths.isEmpty()) {
        taskMonitor.setStatusMessage("Loading paths in path view ...");
        assemblyModel.setInputFileName(inputFile.toString());
        assemblyModel.setPaths(paths, traversalsByPathName);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Traversal(org.dishevelled.bio.assembly.gfa1.Traversal) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) Segment(org.dishevelled.bio.assembly.gfa1.Segment) CyTable(org.cytoscape.model.CyTable) FileReader(java.io.FileReader) CyNode(org.cytoscape.model.CyNode) Path(org.dishevelled.bio.assembly.gfa1.Path) HashBasedTable(com.google.common.collect.HashBasedTable) CyTable(org.cytoscape.model.CyTable) Table(com.google.common.collect.Table) Reference(org.dishevelled.bio.assembly.gfa1.Reference) Orientation(org.dishevelled.bio.assembly.gfa1.Orientation) CyEdge(org.cytoscape.model.CyEdge) BufferedReader(java.io.BufferedReader) Gfa1Adapter(org.dishevelled.bio.assembly.gfa1.Gfa1Adapter) Link(org.dishevelled.bio.assembly.gfa1.Link)

Example 5 with Reference

use of org.molgenis.emx2.Reference in project molgenis-emx2 by molgenis.

the class Column method getReferences.

/**
 * will return self in case of single, and multiple in case of composite key wrapper
 */
public List<Reference> getReferences() {
    // no ref
    if (getRefTable() == null) {
        return new ArrayList<>();
    }
    List<Column> pkeys = getRefTable().getPrimaryKeyColumns();
    List<Reference> refColumns = new ArrayList<>();
    // check if primary key exists
    if (pkeys.size() == 0) {
        throw new MolgenisException("Error in column '" + getName() + "': Reference to " + getRefTableName() + " fails because that table has no primary key");
    }
    // create the refs
    Column refLink = getRefLinkColumn();
    for (Column keyPart : pkeys) {
        if (keyPart.isReference()) {
            for (Reference ref : keyPart.getReferences()) {
                ColumnType type = ref.getPrimitiveType();
                if (!isRef()) {
                    type = getArrayType(type);
                }
                List<String> path = ref.getPath();
                path.add(0, keyPart.getName());
                String name = null;
                if (refLink != null) {
                    for (Reference overlap : refLink.getReferences()) {
                        if (overlap.getTargetTable().equals(ref.getTargetTable()) && overlap.getTargetColumn().equals(ref.getTargetColumn())) {
                            name = overlap.getName();
                        }
                    }
                }
                if (name == null) {
                    name = getName();
                    if (pkeys.size() > 1) {
                        name += COMPOSITE_REF_SEPARATOR + ref.getName();
                    }
                }
                refColumns.add(new Reference(this, name, ref.getName(), getColumnType(), type, keyPart.getColumnType().isArray(), ref.getTargetTable(), ref.getTargetColumn(), ref.isRequired() || this.isRequired(), path));
            }
        } else {
            ColumnType type = keyPart.getColumnType();
            // all but ref is array
            if (!isRef()) {
                type = getArrayType(type);
            }
            // create the ref
            String name = getName();
            if (pkeys.size() > 1) {
                name += COMPOSITE_REF_SEPARATOR + keyPart.getName();
            }
            refColumns.add(new Reference(this, name, keyPart.getName(), getColumnType(), type, getColumnType().isArray(), getRefTableName(), keyPart.getName(), keyPart.isRequired() || this.isRequired(), new ArrayList<>(List.of(keyPart.getName()))));
        }
    }
    // clean up in case only one
    if (refColumns.stream().filter(r -> r.getName().startsWith(getName())).count() == 1) {
        refColumns = refColumns.stream().map(r -> {
            if (r.getName().startsWith(getName()))
                r.setName(getName());
            return r;
        }).collect(Collectors.toList());
    }
    // remove duplicates
    HashSet<Object> seen = new HashSet<>();
    refColumns.removeIf(e -> !seen.add(e.getName()));
    return refColumns;
}
Also used : ColumnType(org.molgenis.emx2.ColumnType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)5 BufferedReader (java.io.BufferedReader)4 Reference (org.dishevelled.bio.assembly.gfa1.Reference)4 Traversal (org.dishevelled.bio.assembly.gfa1.Traversal)4 Path (org.dishevelled.bio.assembly.gfa1.Path)3 Column (org.molgenis.emx2.Column)3 SqlColumnExecutor.validateColumn (org.molgenis.emx2.sql.SqlColumnExecutor.validateColumn)3 HashBasedTable (com.google.common.collect.HashBasedTable)2 Table (com.google.common.collect.Table)2 FileReader (java.io.FileReader)2 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 CyEdge (org.cytoscape.model.CyEdge)2 CyNetwork (org.cytoscape.model.CyNetwork)2 CyNode (org.cytoscape.model.CyNode)2 CyRow (org.cytoscape.model.CyRow)2 CyTable (org.cytoscape.model.CyTable)2 Gfa1Listener (org.dishevelled.bio.assembly.gfa1.Gfa1Listener)2