001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.aaproperties.profeat;
022
023import org.biojava.nbio.aaproperties.profeat.convertor.*;
024import org.biojava.nbio.core.sequence.ProteinSequence;
025
026import java.util.HashMap;
027import java.util.Map;
028
029public class ProfeatPropertiesImpl implements IProfeatProperties{
030
031        @Override
032        public double getComposition(ProteinSequence sequence, ATTRIBUTE attribute, GROUPING group) throws Exception {
033                Convertor convertor = getConvertor(attribute);
034                String convertedSequence = convertor.convert(sequence);
035                return (getTotalCount(convertedSequence, group) + 0.0) / convertedSequence.length();
036        }
037
038        private int getTotalCount(String convertedSeq, GROUPING group) throws Exception{
039                char g;
040                switch(group){
041                case GROUP1: g = Convertor.group1; break;
042                case GROUP2: g = Convertor.group2; break;
043                case GROUP3: g = Convertor.group3; break;
044                default: throw new Exception("Unhandled Case: " + group);
045                }
046                int total = 0;
047                for(char c:convertedSeq.toCharArray()){
048                        if(c == g) total++;
049                }
050                return total;
051        }
052
053        @Override
054        public double getTransition(ProteinSequence sequence, ATTRIBUTE attribute, TRANSITION transition) throws Exception{
055                Convertor convertor = getConvertor(attribute);
056                String convertedSequence = convertor.convert(sequence);
057                char t1;
058                char t2;
059                switch(transition){
060                case BETWEEN_11: t1 = '1'; t2 = '1'; break;
061                case BETWEEN_22: t1 = '2'; t2 = '2'; break;
062                case BETWEEN_33: t1 = '3'; t2 = '3'; break;
063                case BETWEEN_12: t1 = '1'; t2 = '2'; break;
064                case BETWEEN_13: t1 = '1'; t2 = '3'; break;
065                case BETWEEN_23: t1 = '2'; t2 = '3'; break;
066                default: throw new Exception("Unhandled Case: " + transition);
067                }
068                int total = 0;
069                for(int i = 0; i < convertedSequence.length() - 1; i++){
070                        char s1 = convertedSequence.charAt(i);
071                        char s2 = convertedSequence.charAt(i + 1);
072                        if((t1 == s1 && t2 == s2) || (t2 == s1 && t1 == s2)) total++;
073                }
074                return total / (convertedSequence.length() - 1.0);
075        }
076
077        @Override
078        public double getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute, GROUPING group, DISTRIBUTION distribution) throws Exception{
079                Convertor convertor = getConvertor(attribute);
080                String convertedSequence = convertor.convert(sequence);
081
082                int totalCount = getTotalCount(convertedSequence, group);
083                int countIndex;
084                switch(distribution){
085                case FIRST: countIndex = 1; break;
086                case FIRST25: countIndex = totalCount * 25 / 100; break;
087                case FIRST50: countIndex = totalCount * 50 / 100; break;
088                case FIRST75: countIndex = totalCount * 75 / 100; break;
089                case ALL: countIndex = totalCount; break;
090                default: throw new Exception("Unhandled Case: " + distribution);
091                }
092
093                char g;
094                switch(group){
095                case GROUP1: g = Convertor.group1; break;
096                case GROUP2: g = Convertor.group2; break;
097                case GROUP3: g = Convertor.group3; break;
098                default: throw new Exception("Unhandled Case: " + group);
099                }
100
101                int currentCount = 0;
102                int dist = 0;
103                for(int x = 0; x < convertedSequence.length(); x++){
104                        if(g == convertedSequence.charAt(x)){
105                                currentCount++;
106                                if(currentCount == countIndex){
107                                        dist = x+1;
108                                        break;
109                                }
110                        }
111                }
112                return (dist + 0.0) / convertedSequence.length();
113        }
114
115        private Convertor getConvertor(ATTRIBUTE attribute) throws Exception{
116                switch(attribute){
117                case HYDROPHOBICITY: return new Convert2Hydrophobicity();
118                case VOLUME: return new Convert2NormalizedVanDerWaalsVolume();
119                case POLARITY: return new Convert2Polarity();
120                case POLARIZABILITY: return new Convert2Polarizability();
121                case CHARGE: return new Convert2Charge();
122                case SECONDARYSTRUCTURE: return new Convert2SecondaryStructure();
123                case SOLVENTACCESSIBILITY: return new Convert2SolventAccessibility();
124                default: throw new Exception("Unknown attribute: " + attribute);
125                }
126        }
127
128        @Override
129        public Map<GROUPING, Double> getComposition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
130                Map<GROUPING, Double> grouping2Composition = new HashMap<GROUPING, Double>();
131                for(GROUPING group:GROUPING.values()) grouping2Composition.put(group, getComposition(sequence, attribute, group));
132                return grouping2Composition;
133        }
134
135        @Override
136        public Map<ATTRIBUTE, Map<GROUPING, Double>> getComposition(ProteinSequence sequence) throws Exception {
137                Map<ATTRIBUTE, Map<GROUPING, Double>> attribute2Grouping2Composition = new HashMap<ATTRIBUTE, Map<GROUPING, Double>>();
138                for(ATTRIBUTE attribute:ATTRIBUTE.values()) attribute2Grouping2Composition.put(attribute, getComposition(sequence, attribute));
139                return attribute2Grouping2Composition;
140        }
141
142        @Override
143        public Map<TRANSITION, Double> getTransition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
144                Map<TRANSITION, Double> transition2Double = new HashMap<TRANSITION, Double>();
145                for(TRANSITION transition:TRANSITION.values()) transition2Double.put(transition, getTransition(sequence, attribute, transition));
146                return transition2Double;
147        }
148
149        @Override
150        public Map<ATTRIBUTE, Map<TRANSITION, Double>> getTransition(ProteinSequence sequence) throws Exception {
151                Map<ATTRIBUTE, Map<TRANSITION, Double>> attribute2Transition2Double = new HashMap<ATTRIBUTE, Map<TRANSITION, Double>>();
152                for(ATTRIBUTE attribute:ATTRIBUTE.values()) attribute2Transition2Double.put(attribute, getTransition(sequence, attribute));
153                return attribute2Transition2Double;
154        }
155
156        @Override
157        public Map<DISTRIBUTION, Double> getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute, GROUPING group) throws Exception {
158                Map<DISTRIBUTION, Double> distribution2Double = new HashMap<DISTRIBUTION, Double>();
159                for(DISTRIBUTION distribution:DISTRIBUTION.values())
160                        distribution2Double.put(distribution, getDistributionPosition(sequence, attribute, group, distribution));
161                return distribution2Double;
162        }
163
164        @Override
165        public Map<GROUPING, Map<DISTRIBUTION, Double>> getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
166                Map<GROUPING, Map<DISTRIBUTION, Double>> grouping2Distribution2Double = new HashMap<GROUPING, Map<DISTRIBUTION, Double>>();
167                for(GROUPING group:GROUPING.values())
168                        grouping2Distribution2Double.put(group, getDistributionPosition(sequence, attribute, group));
169                return grouping2Distribution2Double;
170        }
171
172        @Override
173        public Map<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>> getDistributionPosition(ProteinSequence sequence) throws Exception {
174                Map<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>> attribute2Grouping2Distribution2Double =
175                        new HashMap<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>>();
176                for(ATTRIBUTE attribute:ATTRIBUTE.values())
177                        attribute2Grouping2Distribution2Double.put(attribute, getDistributionPosition(sequence, attribute));
178                return attribute2Grouping2Distribution2Double;
179        }
180
181}