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                total = (int)convertedSeq.chars().filter(c ->(char) c == g) .count();
048                return total;
049        }
050
051        @Override
052        public double getTransition(ProteinSequence sequence, ATTRIBUTE attribute, TRANSITION transition) throws Exception{
053                Convertor convertor = getConvertor(attribute);
054                String convertedSequence = convertor.convert(sequence);
055                char t1;
056                char t2;
057                switch(transition){
058                case BETWEEN_11: t1 = '1'; t2 = '1'; break;
059                case BETWEEN_22: t1 = '2'; t2 = '2'; break;
060                case BETWEEN_33: t1 = '3'; t2 = '3'; break;
061                case BETWEEN_12: t1 = '1'; t2 = '2'; break;
062                case BETWEEN_13: t1 = '1'; t2 = '3'; break;
063                case BETWEEN_23: t1 = '2'; t2 = '3'; break;
064                default: throw new Exception("Unhandled Case: " + transition);
065                }
066                int total = 0;
067                for(int i = 0; i < convertedSequence.length() - 1; i++){
068                        char s1 = convertedSequence.charAt(i);
069                        char s2 = convertedSequence.charAt(i + 1);
070                        if((t1 == s1 && t2 == s2) || (t2 == s1 && t1 == s2)) total++;
071                }
072                return total / (convertedSequence.length() - 1.0);
073        }
074
075        @Override
076        public double getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute, GROUPING group, DISTRIBUTION distribution) throws Exception{
077                Convertor convertor = getConvertor(attribute);
078                String convertedSequence = convertor.convert(sequence);
079
080                int totalCount = getTotalCount(convertedSequence, group);
081                int countIndex;
082                switch(distribution){
083                case FIRST: countIndex = 1; break;
084                case FIRST25: countIndex = totalCount * 25 / 100; break;
085                case FIRST50: countIndex = totalCount * 50 / 100; break;
086                case FIRST75: countIndex = totalCount * 75 / 100; break;
087                case ALL: countIndex = totalCount; break;
088                default: throw new Exception("Unhandled Case: " + distribution);
089                }
090
091                char g;
092                switch(group){
093                case GROUP1: g = Convertor.group1; break;
094                case GROUP2: g = Convertor.group2; break;
095                case GROUP3: g = Convertor.group3; break;
096                default: throw new Exception("Unhandled Case: " + group);
097                }
098
099                int currentCount = 0;
100                int dist = 0;
101                for(int x = 0; x < convertedSequence.length(); x++){
102                        if(g == convertedSequence.charAt(x)){
103                                currentCount++;
104                                if(currentCount == countIndex){
105                                        dist = x+1;
106                                        break;
107                                }
108                        }
109                }
110                return (dist + 0.0) / convertedSequence.length();
111        }
112
113        private Convertor getConvertor(ATTRIBUTE attribute) throws Exception{
114                switch(attribute){
115                case HYDROPHOBICITY: return new Convert2Hydrophobicity();
116                case VOLUME: return new Convert2NormalizedVanDerWaalsVolume();
117                case POLARITY: return new Convert2Polarity();
118                case POLARIZABILITY: return new Convert2Polarizability();
119                case CHARGE: return new Convert2Charge();
120                case SECONDARYSTRUCTURE: return new Convert2SecondaryStructure();
121                case SOLVENTACCESSIBILITY: return new Convert2SolventAccessibility();
122                default: throw new Exception("Unknown attribute: " + attribute);
123                }
124        }
125
126        @Override
127        public Map<GROUPING, Double> getComposition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
128                Map<GROUPING, Double> grouping2Composition = new HashMap<GROUPING, Double>();
129                for(GROUPING group:GROUPING.values()) grouping2Composition.put(group, getComposition(sequence, attribute, group));
130                return grouping2Composition;
131        }
132
133        @Override
134        public Map<ATTRIBUTE, Map<GROUPING, Double>> getComposition(ProteinSequence sequence) throws Exception {
135                Map<ATTRIBUTE, Map<GROUPING, Double>> attribute2Grouping2Composition = new HashMap<ATTRIBUTE, Map<GROUPING, Double>>();
136                for(ATTRIBUTE attribute:ATTRIBUTE.values()) attribute2Grouping2Composition.put(attribute, getComposition(sequence, attribute));
137                return attribute2Grouping2Composition;
138        }
139
140        @Override
141        public Map<TRANSITION, Double> getTransition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
142                Map<TRANSITION, Double> transition2Double = new HashMap<TRANSITION, Double>();
143                for(TRANSITION transition:TRANSITION.values()) transition2Double.put(transition, getTransition(sequence, attribute, transition));
144                return transition2Double;
145        }
146
147        @Override
148        public Map<ATTRIBUTE, Map<TRANSITION, Double>> getTransition(ProteinSequence sequence) throws Exception {
149                Map<ATTRIBUTE, Map<TRANSITION, Double>> attribute2Transition2Double = new HashMap<ATTRIBUTE, Map<TRANSITION, Double>>();
150                for(ATTRIBUTE attribute:ATTRIBUTE.values()) attribute2Transition2Double.put(attribute, getTransition(sequence, attribute));
151                return attribute2Transition2Double;
152        }
153
154        @Override
155        public Map<DISTRIBUTION, Double> getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute, GROUPING group) throws Exception {
156                Map<DISTRIBUTION, Double> distribution2Double = new HashMap<DISTRIBUTION, Double>();
157                for(DISTRIBUTION distribution:DISTRIBUTION.values())
158                        distribution2Double.put(distribution, getDistributionPosition(sequence, attribute, group, distribution));
159                return distribution2Double;
160        }
161
162        @Override
163        public Map<GROUPING, Map<DISTRIBUTION, Double>> getDistributionPosition(ProteinSequence sequence, ATTRIBUTE attribute) throws Exception {
164                Map<GROUPING, Map<DISTRIBUTION, Double>> grouping2Distribution2Double = new HashMap<GROUPING, Map<DISTRIBUTION, Double>>();
165                for(GROUPING group:GROUPING.values())
166                        grouping2Distribution2Double.put(group, getDistributionPosition(sequence, attribute, group));
167                return grouping2Distribution2Double;
168        }
169
170        @Override
171        public Map<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>> getDistributionPosition(ProteinSequence sequence) throws Exception {
172                Map<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>> attribute2Grouping2Distribution2Double =
173                        new HashMap<ATTRIBUTE, Map<GROUPING, Map<DISTRIBUTION, Double>>>();
174                for(ATTRIBUTE attribute:ATTRIBUTE.values())
175                        attribute2Grouping2Distribution2Double.put(attribute, getDistributionPosition(sequence, attribute));
176                return attribute2Grouping2Distribution2Double;
177        }
178
179}