001package org.biopax.paxtools.io.sif.level3;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.biopax.paxtools.io.sif.BinaryInteractionType;
006import org.biopax.paxtools.io.sif.InteractionSet;
007import org.biopax.paxtools.io.sif.SimpleInteraction;
008import org.biopax.paxtools.model.BioPAXElement;
009import org.biopax.paxtools.model.Model;
010import org.biopax.paxtools.model.level3.PhysicalEntity;
011
012import java.util.*;
013
014/**
015 * Base class for level 3 interactions rules.
016 */
017public abstract class InteractionRuleL3Adaptor implements InteractionRuleL3
018{
019        /**
020         * Log for logging.
021         */
022        private final Log log = LogFactory.getLog(ParticipatesRule.class);
023
024        /**
025         * Selects only PhysicalEntity as seed.
026         * @param interactionSet inferred interactions
027         * @param entity this must be a PhysicalEntity for L3
028         * @param model BioPAX model
029         */
030        public final void inferInteractions(InteractionSet interactionSet, BioPAXElement entity,
031                Model model)
032        {
033                if (entity instanceof PhysicalEntity)
034                {
035                        inferInteractionsFromPE((InteractionSetL3) interactionSet, ((PhysicalEntity) entity),
036                                model);
037                }
038                else
039                {
040                        if (log.isInfoEnabled()) log.info("Not a PE Skipping." + entity.getRDFId());
041                }
042        }
043
044        /**
045         * Initializes the options.
046         * @param options options map
047         */
048        public void initOptions(Map options)
049        {
050                if (options == null)
051                {
052                        options = new HashMap();
053                }
054                initOptionsNotNull(options);
055        }
056
057        /**
058         * Does nothing for this class. Initializes the options where this is overwritten.
059         * @param options options map
060         */
061        protected void initOptionsNotNull(Map options)
062        {
063        }
064
065        /**
066         * Checks if the options map has the given option with the given value.
067         * @param key key of the option
068         * @param value value of the option
069         * @param options options map
070         * @return true if the options map has the given option with the given value
071         */
072    protected boolean checkOption(Object key, Object value, Map options)
073    {
074        return options.containsKey(key)&&options.get(key).equals(value);
075    }
076
077        /**
078         *
079         * @param pes physical entities
080         * @param set inferred binary interactions
081         * @return BioPAX objects
082         */
083        protected Set<BioPAXElement> collectEntities(Set<PhysicalEntity> pes, InteractionSetL3 set)
084        {
085                Set<BioPAXElement> entities = new HashSet<BioPAXElement>();
086                for (PhysicalEntity pe : pes)
087                {
088                        BioPAXElement entity = set.getGroupMap().getEntityReferenceOrGroup(pe);
089                        if (entity != null) entities.add(entity);
090                        if (entity instanceof Group)
091                        {
092                                getMembersRecursively(entities, (Group) entity);
093                        }
094                }
095                return entities;
096        }
097
098        /**
099         * Gets members of the Group recursively traversing sub-Groups.
100         * @param entities member set
101         * @param group group to query
102         */
103        private void getMembersRecursively(Set<BioPAXElement> entities, Group group)
104        {
105                entities.addAll(group.members);
106                for (Group subgroup : group.subgroups)
107                {
108                        getMembersRecursively(entities, subgroup);
109                }
110        }
111
112        /**
113         * Creates an interactions between every ordered pair of components.
114         * @param interactionSet inferred interactions
115         * @param components nodes of the clique
116         * @param type interaction type
117         * @param mediators mediator elements of te interaction
118         */
119        protected void createClique(InteractionSetL3 interactionSet, List<BioPAXElement> components,
120                        BinaryInteractionType type, BioPAXElement... mediators)
121        {
122        GroupMap groupMap = interactionSet.getGroupMap();
123
124                for (int j = 0; j < components.size(); j++)
125                {
126                        for (int i = 0; i < j; i++) {
127                createAndAdd(
128                        groupMap.getEntityReferenceOrGroup(components.get(i)),
129                        groupMap.getEntityReferenceOrGroup(components.get(j)), interactionSet,
130                        type,
131                        mediators);
132            }
133        }
134        }
135
136        /**
137         * Creates the binary interaction and adds to the inferred interaction list.
138         * @param source source of the interaction
139         * @param target target of the interaction
140         * @param is3 inferred interactions
141         * @param type interaction type
142         * @param mediators mediators of the interaction
143         */
144    protected void createAndAdd(
145            BioPAXElement source,
146            BioPAXElement target,
147            InteractionSetL3 is3,
148            BinaryInteractionType type,
149            BioPAXElement... mediators)
150    {
151        if(source!=null && target!=null && !source.equals(target))
152        {
153            SimpleInteraction sc = new SimpleInteraction(source, target, type);
154            for (BioPAXElement mediator : mediators) {
155                sc.addMediator(mediator);
156            }
157            is3.add(sc);
158        }
159    }
160}