001package org.biopax.paxtools.query.wrapperL3;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.*;
005import org.biopax.paxtools.model.level3.Process;
006import org.biopax.paxtools.query.model.AbstractNode;
007import org.biopax.paxtools.query.model.Edge;
008import org.biopax.paxtools.query.model.Graph;
009import org.biopax.paxtools.query.model.Node;
010
011import java.util.Collection;
012import java.util.Collections;
013
014/**
015 * Wrapper for the Control class.
016 *
017 * @author Ozgun Babur
018 */
019public class ControlWrapper extends AbstractNode
020{
021        /**
022         * Wrapped control.
023         */
024        protected Control ctrl;
025
026        /**
027         * Sign of the control.
028         */
029        protected int sign;
030
031        /**
032         * Flag to indicate if this control is related to a transcription.
033         */
034        protected boolean transcription;
035
036        /**
037         * Constructor with the Control and the owner graph.
038         * @param ctrl Control to be wrapped
039         * @param graph Owner graph
040         */
041        protected ControlWrapper(Control ctrl, Graph graph)
042        {
043                super(graph);
044                this.ctrl = ctrl;
045                this.transcription = false;
046        }
047
048        /**
049         * Control is not a breadth node.
050         * @return False
051         */
052        public boolean isBreadthNode()
053        {
054                return false;
055        }
056
057        /**
058         * @return Sign of the Control
059         */
060        public int getSign()
061        {
062                return sign;
063        }
064
065        /**
066         * Controls are not ubiquitous molecules.
067         * @return False
068         */
069        public boolean isUbique()
070        {
071                return false;
072        }
073
074        /**
075         * RDF ID of the Control is its key.
076         * @return Key
077         */
078        public String getKey()
079        {
080                return ctrl.getRDFId();
081        }
082
083        /**
084         * Extracts the sign and the type of the Control.
085         */
086        @Override
087        public void init()
088        {
089                ControlType type = ctrl.getControlType();
090
091                if (type != null && type.toString().startsWith("I"))
092                {
093                        sign = NEGATIVE;
094                }
095                else
096                {
097                        sign = POSITIVE;
098                }
099
100                if (ctrl instanceof TemplateReactionRegulation)
101                        transcription = true;
102        }
103
104        /**
105         * Puts the wrapper of the parameter element to the upstream of this Control.
106         * @param element to put at upstream
107         */
108        private void bindUpstream(BioPAXElement element)
109        {
110                AbstractNode node = (AbstractNode) graph.getGraphObject(element);
111
112                if (node != null)
113                {
114                        Edge edge = new EdgeL3(node, this, graph);
115                        node.getDownstreamNoInit().add(edge);
116                        this.getUpstreamNoInit().add(edge);
117                }
118        }
119
120        /**
121         * Binds the controller and other Controls that controls this control.
122         */
123        @Override
124        public void initUpstream()
125        {
126                for (Controller controller : ctrl.getController())
127                {
128                        if (controller instanceof Pathway) continue;
129
130                        PhysicalEntity pe = (PhysicalEntity) controller;
131                        bindUpstream(pe);
132                }
133
134                for (Control control : ctrl.getControlledOf())
135                {
136                        bindUpstream(control);
137                }
138        }
139
140        /**
141         * Binds the controlled objects.
142         */
143        @Override
144        public void initDownstream()
145        {
146                for (Process prc : ctrl.getControlled())
147                {
148                        if (prc instanceof Conversion ||
149                                prc instanceof Control ||
150                                prc instanceof TemplateReaction)
151                        {
152                                AbstractNode node = (AbstractNode) graph.getGraphObject(prc);
153
154                                if (node != null)
155                                {
156                                        Edge edge = new EdgeL3(this, node, graph);
157                                        node.getUpstreamNoInit().add(edge);
158                                        getDownstreamNoInit().add(edge);
159                                }
160                        }
161                }
162        }
163
164        /**
165         * Gets the wrapped Control.
166         * @return The Control
167         */
168        public Control getControl()
169        {
170                return ctrl;
171        }
172
173        /**
174         * Control cannot have an equivalent.
175         * @return Empty set
176         */
177        @Override
178        public Collection<Node> getUpperEquivalent()
179        {
180                return Collections.emptySet();
181        }
182
183        /**
184         * Control cannot have an equivalent.
185         * @return Empty set
186         */
187        @Override
188        public Collection<Node> getLowerEquivalent()
189        {
190                return Collections.emptySet();
191        }
192
193        /**
194         * @return whether this Control is related to a transcription event
195         */
196        public boolean isTranscription()
197        {
198                return transcription;
199        }
200
201        /**
202         * Make or not make this control related to a transcription.
203         * @param transcription whether this Control is related to a transcription
204         */
205        public void setTranscription(boolean transcription)
206        {
207                this.transcription = transcription;
208        }
209}