001package org.biopax.paxtools.io.sbgn;
002
003import org.biopax.paxtools.model.level3.*;
004import org.sbgn.bindings.Glyph;
005import org.sbgn.bindings.ObjectFactory;
006
007import java.io.BufferedReader;
008import java.io.InputStream;
009import java.io.InputStreamReader;
010import java.util.HashMap;
011import java.util.Map;
012import java.util.Set;
013
014/**
015 * Prepares displayable Stat class to generate labels like "pY@123" for modification features and
016 * like x[30 - 122] for fragment features.
017 *
018 * @author Ozgun Babur
019 */
020public class CommonFeatureStringGenerator implements FeatureDecorator
021{
022        /**
023         * Map from modification term to its symbol.
024         */
025        private static Map<String, String> symbolMapping;
026
027        /**
028         * Map from location term (amino acid name) to is symbol (letter).
029         */
030        private static Map<String, String> locMapping;
031
032        /**
033         * Creates State to represent the entity feature.
034         * @param ef feature to represent
035         * @param factory factory that can create the State class
036         * @return State representing the feature
037         */
038        @Override
039        public Glyph.State createStateVar(EntityFeature ef, ObjectFactory factory)
040        {
041                if (ef instanceof FragmentFeature)
042                {
043                        FragmentFeature ff = (FragmentFeature) ef;
044                        SequenceLocation loc = ff.getFeatureLocation();
045                        if (loc instanceof SequenceInterval)
046                        {
047                                SequenceInterval si = (SequenceInterval) loc;
048                                SequenceSite begin = si.getSequenceIntervalBegin();
049                                SequenceSite end = si.getSequenceIntervalEnd();
050                                
051                                if (begin != null && end != null)
052                                {
053                                        Glyph.State state = factory.createGlyphState();
054                                        state.setValue("x[" + begin.getSequencePosition() + " - " +
055                                                end.getSequencePosition() + "]");
056                                        return state;
057                                }
058                        }
059                }
060                else if (ef instanceof ModificationFeature)
061                {
062                        ModificationFeature mf = (ModificationFeature) ef;
063                        SequenceModificationVocabulary modType = mf.getModificationType();
064                        
065                        if (modType != null)
066                        {
067                                Set<String> terms = modType.getTerm();
068                                if (terms != null && !terms.isEmpty())
069                                {
070                                        String orig = terms.iterator().next();
071                                        String term = orig.toLowerCase();
072                                        
073                                        String s = symbolMapping.containsKey(term) ? symbolMapping.get(term) : orig;
074
075                                        Glyph.State state = factory.createGlyphState();
076                                        state.setValue(s);
077                                        
078                                        SequenceLocation loc = mf.getFeatureLocation();
079                                        if (locMapping.containsKey(term))
080                                        {
081                                                state.setVariable(locMapping.get(term));
082                                        }
083
084                                        if (loc instanceof SequenceSite)
085                                        {
086                                                SequenceSite ss = (SequenceSite) loc;
087                                                if (ss.getSequencePosition() > 0)
088                                                {
089                                                        state.setVariable(
090                                                                (state.getVariable() != null ? state.getVariable() : "") +
091                                                                ss.getSequencePosition());
092                                                }
093                                        }
094                                        
095                                        return state;
096                                }
097                        }
098                }
099
100                // Binding features are ignored
101                return null;
102        }
103
104        /**
105         * Initializes resources.
106         */
107        static
108        {
109                symbolMapping = new HashMap<String, String>();
110                locMapping = new HashMap<String, String>();
111                
112                try
113                {
114                        InputStream is = CommonFeatureStringGenerator.class.getResourceAsStream(
115                                "feature-shorts.txt");
116
117                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
118
119                        for (String line = reader.readLine(); line != null; line = reader.readLine())
120                        {
121                                String[] token = line.split("\t");
122
123                                if (token.length > 1 && token[1] != null && token[1].length() > 0)
124                                {
125                                        String key = token[0].replace("\"", "").toLowerCase();
126                                        symbolMapping.put(key, token[1].replace("\"", ""));
127                                        
128                                        if (token.length > 2 && token[2] != null && token[2].length() > 0)
129                                        {
130                                                locMapping.put(key, token[2].replace("\"", ""));
131                                        }
132                                }
133                        }
134
135                        reader.close();
136                }
137                catch(Exception e){e.printStackTrace();}
138        }
139}