001/*
002
003    ngs-align  Sequence alignment.
004    Copyright (c) 2014-2015 National Marrow Donor Program (NMDP)
005
006    This library is free software; you can redistribute it and/or modify it
007    under the terms of the GNU Lesser General Public License as published
008    by the Free Software Foundation; either version 3 of the License, or (at
009    your option) any later version.
010
011    This library is distributed in the hope that it will be useful, but WITHOUT
012    ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
013    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
014    License for more details.
015
016    You should have received a copy of the GNU Lesser General Public License
017    along with this library;  if not, write to the Free Software Foundation,
018    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
019
020    > http://www.gnu.org/licenses/lgpl.html
021
022*/
023package org.nmdp.ngs.align;
024
025import static com.google.common.base.Preconditions.checkNotNull;
026
027import java.util.List;
028
029import javax.annotation.concurrent.Immutable;
030
031import com.google.common.base.Splitter;
032
033/**
034 * High-scoring segment pair (HSP).
035 */
036@Immutable
037public final class HighScoringPair {
038    private final String source;
039    private final String target;
040    private final double percentIdentity;
041    private final long alignmentLength;
042    private final int mismatches;
043    private final int gapOpens;
044    private final long sourceStart;
045    private final long sourceEnd;
046    private final long targetStart;
047    private final long targetEnd;
048    private final double evalue;
049    private final double bitScore;
050
051
052    /**
053     * Create a new high-scoring segment pair (HSP).
054     *
055     * @param source source
056     * @param target target
057     * @param percentIdentity percent identity
058     * @param alignmentLength alignment length
059     * @param mismatches mismatches
060     * @param gapOpens gap opens
061     * @param sourceStart source start
062     * @param sourceEnd source end
063     * @param targetStart target start
064     * @param targetEnd target end
065     * @param evalue evalue
066     * @param bitScore bit score
067     */
068    HighScoringPair(final String source,
069                    final String target,
070                    final double percentIdentity,
071                    final long alignmentLength,
072                    final int mismatches,
073                    final int gapOpens,
074                    final long sourceStart,
075                    final long sourceEnd,
076                    final long targetStart,
077                    final long targetEnd,
078                    final double evalue,
079                    final double bitScore) {
080
081        this.source = source;
082        this.target = target;
083        this.percentIdentity = percentIdentity;
084        this.alignmentLength = alignmentLength;
085        this.mismatches = mismatches;
086        this.gapOpens = gapOpens;
087        this.sourceStart = sourceStart;
088        this.sourceEnd = sourceEnd;
089        this.targetStart = targetStart;
090        this.targetEnd = targetEnd;
091        this.evalue = evalue;
092        this.bitScore = bitScore;
093    }
094
095
096    /**
097     * Return the source for this high-scoring segment pair (HSP).
098     *
099     * @return the source for this high-scoring segment pair (HSP)
100     */
101    public String source() {
102        return source;
103    }
104
105    /**
106     * Return the target for this high-scoring segment pair (HSP).
107     *
108     * @return the target for this high-scoring segment pair (HSP)
109     */
110    public String target() {
111        return target;
112    }
113
114    /**
115     * Return the percent identity for this high-scoring segment pair (HSP).
116     *
117     * @return the percent identity for this high-scoring segment pair (HSP)
118     */
119    public double percentIdentity() {
120        return percentIdentity;
121    }
122
123    /**
124     * Return the alignment length for this high-scoring segment pair (HSP).
125     *
126     * @return the alignment length for this high-scoring segment pair (HSP)
127     */
128    public long alignmentLength() {
129        return alignmentLength;
130    }
131
132    /**
133     * Return the mismatches for this high-scoring segment pair (HSP).
134     *
135     * @return the mismatches for this high-scoring segment pair (HSP)
136     */
137    public int mismatches() {
138        return mismatches;
139    }
140
141    /**
142     * Return the gap opens for this high-scoring segment pair (HSP).
143     *
144     * @return the gap opens for this high-scoring segment pair (HSP)
145     */
146    public int gapOpens() {
147        return gapOpens;
148    }
149
150    /**
151     * Return the source start for this high-scoring segment pair (HSP).
152     *
153     * @return the source start for this high-scoring segment pair (HSP)
154     */
155    public long sourceStart() {
156        return sourceStart;
157    }
158
159    /**
160     * Return the source end for this high-scoring segment pair (HSP).
161     *
162     * @return the source end for this high-scoring segment pair (HSP)
163     */
164    public long sourceEnd() {
165        return sourceEnd;
166    }
167
168    /**
169     * Return the target start for this high-scoring segment pair (HSP).
170     *
171     * @return the target start for this high-scoring segment pair (HSP)
172     */
173    public long targetStart() {
174        return targetStart;
175    }
176
177    /**
178     * Return the target end for this high-scoring segment pair (HSP).
179     *
180     * @return the target end for this high-scoring segment pair (HSP)
181     */
182    public long targetEnd() {
183        return targetEnd;
184    }
185
186    /**
187     * Return the evalue for this high-scoring segment pair (HSP).
188     *
189     * @return the evalue for this high-scoring segment pair (HSP)
190     */
191    public double evalue() {
192        return evalue;
193    }
194
195    /**
196     * Return the bit score for this high-scoring segment pair (HSP).
197     *
198     * @return the bit score for this high-scoring segment pair (HSP)
199     */
200    public double bitScore() {
201        return bitScore;
202    }
203
204    @Override
205    public String toString() {
206        StringBuilder sb = new StringBuilder();
207        sb.append(source);
208        sb.append("\t");
209        sb.append(target);
210        sb.append("\t");
211        sb.append(percentIdentity);
212        sb.append("\t");
213        sb.append(alignmentLength);
214        sb.append("\t");
215        sb.append(mismatches);
216        sb.append("\t");
217        sb.append(gapOpens);
218        sb.append("\t");
219        sb.append(sourceStart);
220        sb.append("\t");
221        sb.append(sourceEnd);
222        sb.append("\t");
223        sb.append(targetStart);
224        sb.append("\t");
225        sb.append(targetEnd);
226        sb.append("\t");
227        sb.append(evalue);
228        sb.append("\t");
229        sb.append(bitScore);
230        return sb.toString();
231    }
232
233    /**
234     * Return a new high scoring pair parsed from the specified value.
235     *
236     * @param value value to parse, must not be null
237     * @return a new high scoring pair parsed from the specified value
238     * @throws IllegalArgumentException if the value is not valid high scoring pair format
239     * @throws NumberFormatException if a number valued field cannot be parsed as a number
240     */
241    public static HighScoringPair valueOf(final String value) {
242        checkNotNull(value);
243        List<String> tokens = Splitter.on("\t").trimResults().splitToList(value);
244        if (tokens.size() != 12) {
245            throw new IllegalArgumentException("value must have twelve fields");
246        }
247        String source = tokens.get(0).trim();
248        String target = tokens.get(1).trim();
249        double percentIdentity = Double.parseDouble(tokens.get(2).trim());
250        long alignmentLength = Long.parseLong(tokens.get(3).trim());
251        int mismatches = Integer.parseInt(tokens.get(4).trim());
252        int gapOpens = Integer.parseInt(tokens.get(5).trim());
253        long sourceStart = Long.parseLong(tokens.get(6).trim());
254        long sourceEnd = Long.parseLong(tokens.get(7).trim());
255        long targetStart = Long.parseLong(tokens.get(8).trim());
256        long targetEnd = Long.parseLong(tokens.get(9).trim());
257        double evalue = Double.parseDouble(tokens.get(10).trim());
258        double bitScore = Double.parseDouble(tokens.get(11).trim());
259
260        return new HighScoringPair(source, target, percentIdentity, alignmentLength, mismatches, gapOpens, sourceStart, sourceEnd, targetStart, targetEnd, evalue, bitScore);
261    }
262}
263