001package org.biopax.paxtools.query.wrapperL3;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.*;
005import org.biopax.paxtools.query.model.AbstractNode;
006import org.biopax.paxtools.query.model.Edge;
007import org.biopax.paxtools.query.model.Graph;
008import org.biopax.paxtools.query.model.Node;
009
010import java.util.Collection;
011import java.util.Collections;
012
013/**
014 * Wrapper for TemplateReaction class.
015 *
016 * @author Ozgun Babur
017 */
018public class TemplateReactionWrapper extends EventWrapper
019{
020        /**
021         * Wrapped TemplateReaction.
022         */
023        private TemplateReaction tempReac;
024
025        /**
026         * Constructor with the TemplateReaction and the owner graph.
027         * @param tempReac TemplateReaction to wrap
028         * @param graph Owner graph
029         */
030        protected TemplateReactionWrapper(TemplateReaction tempReac, GraphL3 graph)
031        {
032                super(graph);
033                this.tempReac = tempReac;
034        }
035
036        /**
037         * Binds to template and controllers.
038         */
039        @Override
040        public void initUpstream()
041        {
042                NucleicAcid nuc = tempReac.getTemplate();
043                if (nuc != null)
044                addToUpstream(nuc, getGraph());
045
046                for (Control cont : tempReac.getControlledOf())
047                {
048                        addToUpstream(cont, graph);
049                }
050        }
051
052        /**
053         * Binds to products.
054         */
055        @Override
056        public void initDownstream()
057        {
058                for (PhysicalEntity pe : tempReac.getProduct())
059                {
060                        addToDownstream(pe, getGraph());
061                }
062        }
063
064        /**
065         * This is transcription.
066         * @return True
067         */
068        @Override
069        public boolean isTranscription()
070        {
071                return true;
072        }
073
074        /**
075         * Binds the given PhysicalEntity to the downstream.
076         * @param pe PhysicalEntity to bind
077         * @param graph Owner graph
078         */
079        protected void addToDownstream(PhysicalEntity pe, Graph graph)
080        {
081                AbstractNode node = (AbstractNode) graph.getGraphObject(pe);
082
083                if (node != null)
084                {
085                        EdgeL3 edge = new EdgeL3(this, node, graph);
086                        edge.setTranscription(true);
087
088                        node.getUpstreamNoInit().add(edge);
089                        this.getDownstreamNoInit().add(edge);
090                }
091        }
092
093        /**
094         * Binds the given element to the upstream.
095         * @param ele Element to bind
096         * @param graph Owner graph
097         */
098        protected void addToUpstream(BioPAXElement ele, org.biopax.paxtools.query.model.Graph graph)
099        {
100                AbstractNode node = (AbstractNode) graph.getGraphObject(ele);
101                if (node == null) return;
102
103                Edge edge = new EdgeL3(node, this, graph);
104
105                if (isTranscription())
106                {
107                        if (node instanceof ControlWrapper)
108                        {
109                                ((ControlWrapper) node).setTranscription(true);
110                        }
111                }
112
113                node.getDownstreamNoInit().add(edge);
114                this.getUpstreamNoInit().add(edge);
115        }
116
117        /**
118         * Uses RDF ID of TemplateReaction as key.
119         * @return RDF ID of TemplateReaction
120         */
121        public String getKey()
122        {
123                return tempReac.getRDFId();
124        }
125
126        /**
127         * Gets the wrapped TemplateReaction
128         * @return The wrapped TemplateReaction
129         */
130        public TemplateReaction getTempReac()
131        {
132                return tempReac;
133        }
134}