001package org.biopax.paxtools.query.wrapperL3;
002
003import org.biopax.paxtools.model.level3.*;
004
005/**
006 * Wrapper for the Conversion class.
007 *
008 * @author Ozgun Babur
009 */
010public class ConversionWrapper extends EventWrapper
011{
012        /**
013         * Wrapped Conversion.
014         */
015        protected Conversion conv;
016
017        /**
018         * The direction that the Conversion is wrapped. A separate wrapper is used for each direction
019         * if the Conversion is reversible.
020         */
021        protected boolean direction;
022
023        /**
024         * Link to the wrapper for the same Conversion but for the other direction, if exists.
025         */
026        private ConversionWrapper reverse;
027
028        /**
029         * Flag to say this Conversion is a transcription.
030         */
031        protected boolean transcription;
032
033        /**
034         * Constructor with the Conversion to wrap and the owner graph.
035         * @param conv Conversion to wrap
036         * @param graph Owner graph
037         */
038        protected ConversionWrapper(Conversion conv, GraphL3 graph)
039        {
040                super(graph);
041                this.conv = conv;
042        }
043
044        /**
045         * @return Direction of the conversion
046         */
047        public boolean getDirection()
048        {
049                return direction;
050        }
051
052        /**
053         * @return The reverse Conversion wrapper if exists
054         */
055        public ConversionWrapper getReverse()
056        {
057                return reverse;
058        }
059
060        /**
061         * Extracts the direction, creates the reverse if necessary.
062         */
063        public void init()
064        {
065                if (conv.getConversionDirection() == ConversionDirectionType.REVERSIBLE &&
066                        this.reverse == null)
067                {
068                        reverse = new ConversionWrapper(conv, (GraphL3) graph);
069                        this.direction = LEFT_TO_RIGHT;
070                        reverse.direction = RIGHT_TO_LEFT;
071                        reverse.reverse = this;
072                }
073                else if (conv.getConversionDirection() == ConversionDirectionType.RIGHT_TO_LEFT)
074                {
075                        this.direction = RIGHT_TO_LEFT;
076                }
077                else
078                {
079                        this.direction = LEFT_TO_RIGHT;
080                }
081        }
082
083        /**
084         * Binds inputs and controllers.
085         */
086        @Override
087        public void initUpstream()
088        {
089                if (direction == LEFT_TO_RIGHT)
090                {
091                        for (PhysicalEntity pe : conv.getLeft())
092                        {
093                                addToUpstream(pe, getGraph());
094                        }
095                }
096                else
097                {
098                        for (PhysicalEntity pe : conv.getRight())
099                        {
100                                addToUpstream(pe, getGraph());
101                        }
102                }
103
104                for (Control cont : conv.getControlledOf())
105                {
106                        if (cont instanceof Catalysis)
107                        {
108                                Catalysis cat = (Catalysis) cont;
109
110                                if ((cat.getCatalysisDirection() == CatalysisDirectionType.LEFT_TO_RIGHT && direction == RIGHT_TO_LEFT) ||
111                                        (cat.getCatalysisDirection() == CatalysisDirectionType.RIGHT_TO_LEFT && direction == LEFT_TO_RIGHT))
112                                {
113                                        continue;
114                                }
115                        }
116
117                        addToUpstream(cont, graph);
118                }
119        }
120
121        /**
122         * Binds products.
123         */
124        @Override
125        public void initDownstream()
126        {
127                if (direction == RIGHT_TO_LEFT)
128                {
129                        for (PhysicalEntity pe : conv.getLeft())
130                        {
131                                addToDownstream(pe, getGraph());
132                        }
133                }
134                else
135                {
136                        for (PhysicalEntity pe : conv.getRight())
137                        {
138                                addToDownstream(pe, getGraph());
139                        }
140                }
141        }
142
143        /**
144         * @return Whether this is a transcription
145         */
146        public boolean isTranscription()
147        {
148                return transcription;
149        }
150
151        /**
152         * RDF ID of the Conversion and the direction is used for the key.
153         * @return Key
154         */
155        public String getKey()
156        {
157                return conv.getRDFId() + "|" + direction;
158        }
159
160        /**
161         * @return Wrapped Conversion
162         */
163        public Conversion getConversion()
164        {
165                return conv;
166        }
167
168        /**
169         * @return Display name with the ID
170         */
171        @Override
172        public String toString()
173        {
174                return conv.getDisplayName() + " -- " + conv.getRDFId();
175        }
176
177        /**
178         * Direction LEFT_TO_RIGHT.
179         */
180        public static final boolean LEFT_TO_RIGHT = true;
181
182        /**
183         * Direction RIGHT_TO_LEFT.
184         */
185        public static final boolean RIGHT_TO_LEFT = false;
186}