001package org.biopax.paxtools.io.sif.level3;
002
003import org.biopax.paxtools.io.sif.BinaryInteractionType;
004import org.biopax.paxtools.model.BioPAXElement;
005import org.biopax.paxtools.model.Model;
006import org.biopax.paxtools.model.level3.Complex;
007import org.biopax.paxtools.model.level3.EntityReference;
008import org.biopax.paxtools.model.level3.PhysicalEntity;
009
010import java.lang.reflect.Array;
011import java.util.*;
012
013import static org.biopax.paxtools.io.sif.BinaryInteractionType.COMPONENT_OF;
014import static org.biopax.paxtools.io.sif.BinaryInteractionType.IN_SAME_COMPONENT;
015
016/**
017 * This rule class mines complex membership relations. It mines two types of relations.
018 * COMPONENT_OF: A is a member of the Complex B, membership relation can be multi step.
019 * IN_SAME_COMPONENT: A and B are members of the same complex, and this same complex can be in
020 * different level of nesting.
021 * @author Emek Demir
022 */
023public class ComponentRule extends InteractionRuleL3Adaptor
024{
025        /**
026         * List of interaction types mined.
027         */
028        private static List<BinaryInteractionType> binaryInteractionTypes =
029                Arrays.asList(IN_SAME_COMPONENT);
030
031        /**
032         * Option to mine IN_SAME_COMPONENT.
033         */
034        private boolean inSameComponent;
035
036        /**
037         * Option to mine COMPONENT_OF.
038         */
039        private boolean componentOf;
040
041        /**
042         * Infer interactions where A = the given PhysicalEntity.
043         * @param interactionSet to be populated
044         * @param pe PhysicalEntity that will be the seed of the inference
045         * @param model BioPAX model
046         */
047        public void inferInteractionsFromPE(InteractionSetL3 interactionSet, PhysicalEntity pe,
048                Model model)
049        {
050                if (pe instanceof Complex)
051                {
052                        Group group = interactionSet.getGroupMap().getMap().get(pe);
053                        if (group != null)
054                        {
055                                Set<EntityReference> members = group.members;
056                                Set<Group> subGroups = group.subgroups;
057                                ArrayList<BioPAXElement> components = new ArrayList<BioPAXElement>(members.size() +
058                                        subGroups.size());
059                                components.addAll(members);
060                                components.addAll(subGroups);
061
062                                if (inSameComponent)
063                                {
064                                        BioPAXElement[] sources = group.sources.toArray((BioPAXElement[])
065                                                Array.newInstance(BioPAXElement.class, group.sources.size()));
066
067                                        createClique(interactionSet, components,
068                                                BinaryInteractionType.IN_SAME_COMPONENT, sources);
069                                }
070                                if (componentOf)
071                                {
072                                        for (BioPAXElement component : components)
073                                        {
074
075                                                addComponent(component, group, interactionSet);
076                                        }
077                                }
078                        }
079                }
080        }
081
082        /**
083         * Adds the component to the group.
084         * @param component component to add
085         * @param group group to add to
086         * @param interactionSet current set of inferred interactions
087         */
088        private void addComponent(BioPAXElement component, Group group, InteractionSetL3 interactionSet)
089        {
090                createAndAdd(interactionSet.getGroupMap().getEntityReferenceOrGroup(component),
091                        group,interactionSet, BinaryInteractionType.COMPONENT_OF);
092        }
093
094        /**
095         * Initializes options.
096         * @param options options map
097         */
098        @Override public void initOptionsNotNull(Map options)
099        {
100                inSameComponent = !checkOption(IN_SAME_COMPONENT,Boolean.FALSE,options);
101                componentOf = !checkOption(COMPONENT_OF,Boolean.FALSE,options);
102        }
103
104        /**
105         * Gets supported interaction types.
106         * @return interaction types supported
107         */
108        public List<BinaryInteractionType> getRuleTypes()
109        {
110                return binaryInteractionTypes;
111        }
112}