001package org.biopax.paxtools.query.wrapperL3undirected;
002
003import org.biopax.paxtools.model.level3.Interaction;
004import org.biopax.paxtools.model.level3.PhysicalEntity;
005import org.biopax.paxtools.query.model.AbstractNode;
006import org.biopax.paxtools.query.model.Edge;
007import org.biopax.paxtools.query.model.Node;
008
009import java.util.Collection;
010import java.util.HashSet;
011
012/**
013 * Wrapper for PhysicalEntity.
014 *
015 * @author Ozgun Babur
016 */
017public class PhysicalEntityWrapper extends AbstractNode
018{
019        /**
020         * Wrapped PhysicalEntity.
021         */
022        protected PhysicalEntity pe;
023
024        /**
025         * Flag to remember if parent equivalents initialized.
026         */
027        protected boolean upperEquivalentInited;
028
029        /**
030         * Flag to remember if child equivalents initialized.
031         */
032        protected boolean lowerEquivalentInited;
033
034        /**
035         * Flag to say this is a ubiquitous molecule.
036         */
037        protected boolean ubique;
038
039        /**
040         * Constructor with the wrapped PhysicalEntity and the owner graph.
041         * @param pe PhysicalEntity to wrap
042         * @param graph Owner graph
043         */
044        public PhysicalEntityWrapper(PhysicalEntity pe, GraphL3Undirected graph)
045        {
046                super(graph);
047                this.pe = pe;
048                this.upperEquivalentInited = false;
049                this.lowerEquivalentInited = false;
050                this.ubique = false;
051        }
052
053        /**
054         * @return Whether this is ubique
055         */
056        public boolean isUbique()
057        {
058                return ubique;
059        }
060
061        /**
062         * Set the ubique flag.
063         * @param ubique Whether this is a ubiquitous molecule
064         */
065        public void setUbique(boolean ubique)
066        {
067                this.ubique = ubique;
068        }
069
070        /**
071         * Binds to upstream interactions.
072         */
073        public void initUpstream()
074        {
075                for (Interaction inter : pe.getParticipantOf())
076                {
077                        AbstractNode wrapper = (AbstractNode) graph.getGraphObject(inter);
078
079                        if (wrapper == null) continue;
080
081                        Edge edge = new EdgeL3(wrapper, this, graph);
082                        wrapper.getDownstreamNoInit().add(edge);
083                        this.getUpstreamNoInit().add(edge);
084                }
085        }
086
087        /**
088         * Binds to downstream interactions.
089         */
090        public void initDownstream()
091        {
092                for (Interaction inter : pe.getParticipantOf())
093                {
094                        AbstractNode wrapper = (AbstractNode) graph.getGraphObject(inter);
095
096                        if (wrapper == null) continue;
097
098                        Edge edge = new EdgeL3(this, wrapper, graph);
099                        this.getDownstreamNoInit().add(edge);
100                        wrapper.getUpstreamNoInit().add(edge);
101                }
102        }
103
104        //----- Equivalence ---------------------------------------------------------------------------|
105
106        /**
107         * @return Parent equivalent objects
108         */
109        @Override
110        public Collection<Node> getUpperEquivalent()
111        {
112                if (!upperEquivalentInited)
113                {
114                        initUpperEquivalent();
115                }
116                return super.getUpperEquivalent();
117        }
118
119        /**
120         * @return Child equivalent objects
121         */
122        @Override
123        public Collection<Node> getLowerEquivalent()
124        {
125                if (!lowerEquivalentInited)
126                {
127                        initLowerEquivalent();
128                }
129                return super.getLowerEquivalent();
130        }
131
132        /**
133         * Finds homology parent.
134         */
135        protected void initUpperEquivalent()
136        {
137                this.upperEquivalent = new HashSet<Node>();
138
139                for (PhysicalEntity eq : pe.getMemberPhysicalEntityOf())
140                {
141                        Node node = (Node) graph.getGraphObject(eq);
142                        if (node != null) this.upperEquivalent.add(node);
143                }
144
145                upperEquivalentInited = true;
146        }
147
148        /**
149         * Finds member nodes if this is a homology node
150         */
151        protected void initLowerEquivalent()
152        {
153                this.lowerEquivalent = new HashSet<Node>();
154
155                for (PhysicalEntity eq : pe.getMemberPhysicalEntity())
156                {
157                        Node node = (Node) graph.getGraphObject(eq);
158                        if (node != null) this.lowerEquivalent.add(node);
159                }
160
161                lowerEquivalentInited = true;
162        }
163
164        //------ Other --------------------------------------------------------------------------------|
165
166        /**
167         * PhysicalEntity is a breadth node.
168         * @return True
169         */
170        public boolean isBreadthNode()
171        {
172                return true;
173        }
174
175        /**
176         * PhysicalEntity have positive sign.
177         * @return POSITIVE (1)
178         */
179        public int getSign()
180        {
181                return POSITIVE;
182        }
183
184        /**
185         * RDF ID of the PhysicalEntity is used as key.
186         * @return Key
187         */
188        public String getKey()
189        {
190                return pe.getRDFId();
191        }
192
193        /**
194         * @return Wrapped PhysicalEntity
195         */
196        public PhysicalEntity getPhysicalEntity()
197        {
198                return pe;
199        }
200
201        /**
202         * @return display name with ID added
203         */
204        @Override
205        public String toString()
206        {
207                return pe.getDisplayName() + " -- "+ pe.getRDFId();
208        }
209}