1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.metadata;
\r
21 import java.util.List;
\r
23 import javax.xml.bind.annotation.XmlAccessType;
\r
24 import javax.xml.bind.annotation.XmlAccessorType;
\r
25 import javax.xml.bind.annotation.XmlAttribute;
\r
27 import compbio.data.sequence.FastaSequence;
\r
28 import compbio.util.SysPrefs;
\r
31 * A value object containing a maximum number of sequences and a maximum average
\r
32 * sequence length for a preset. Also contains static method for determining the
\r
33 * number of sequence and their average length in the List<FastaSequence>
\r
38 * @version 1.0 January 2010
\r
41 * the type of an executable for which this limit is defined.
\r
43 * @see LimitsManager
\r
45 @XmlAccessorType(XmlAccessType.FIELD)
\r
46 public class Limit<T> {
\r
48 // Allowed to be null
\r
49 private String preset;
\r
50 // Cannot be 0 or below
\r
51 private int seqNumber;
\r
52 // Can be 0 - i.e. undefined
\r
53 private int seqLength;
\r
59 // JAXB default constructor
\r
63 * Instantiate the limit
\r
66 * the maximum number of sequences allowed for calculation.
\r
69 * the average length of the sequence, optional
\r
71 * the name of preset if any, optional
\r
72 * @throws IllegalArgumentException
\r
73 * if the seqNumber is not supplied or the seqLength is negative
\r
75 public Limit(int seqNumber, int seqLength, String preset) {
\r
76 if (seqNumber <= 0) {
\r
77 throw new IllegalArgumentException(
\r
78 "seqNumber - a maximum number of sequences to align must be greater than 0. Value given:"
\r
81 if (seqLength < 0) {
\r
82 throw new IllegalArgumentException(
\r
83 "seqLength - an average sequence length must be greater than 0. Value given:"
\r
86 this.seqNumber = seqNumber;
\r
87 this.seqLength = seqLength;
\r
88 this.preset = preset;
\r
89 this.isDefault = false;
\r
92 public Limit(int seqNumber, int seqLength, String preset, boolean isDefault) {
\r
93 this(seqNumber, seqNumber, preset);
\r
94 this.isDefault = isDefault;
\r
97 public String getPreset() {
\r
103 * @return the allowed average sequence length
\r
105 public int getAvgSeqLength() {
\r
111 * @return the maximum number of sequences allowed
\r
113 public int getSeqNumber() {
\r
119 * @return true is this is a default limit to be used, false otherwise
\r
121 public boolean isDefault() {
\r
126 public int hashCode() {
\r
127 final int prime = 31;
\r
129 result = prime * result + ((preset == null) ? 0 : preset.hashCode());
\r
130 result = prime * result + seqLength;
\r
131 result = prime * result + seqNumber;
\r
136 public boolean equals(Object obj) {
\r
141 if (getClass() != obj.getClass())
\r
143 Limit other = (Limit) obj;
\r
144 if (preset == null) {
\r
145 if (other.preset != null)
\r
147 } else if (!preset.equals(other.preset))
\r
149 if (seqLength != other.seqLength)
\r
151 if (seqNumber != other.seqNumber)
\r
157 public String toString() {
\r
160 value = "Default Limits:" + SysPrefs.newlinechar;
\r
162 value = "Limits for Preset '" + preset + "'" + SysPrefs.newlinechar;
\r
164 value += "Maximum sequence number=" + seqNumber + SysPrefs.newlinechar;
\r
165 value += "Average sequence length=" + seqLength + SysPrefs.newlinechar;
\r
170 * Calculates total number of letters allowed
\r
172 long numberOfLetters() {
\r
173 return this.seqNumber * this.seqLength;
\r
177 * Checks if the number of sequences or their average length in the dataset
\r
178 * exceeds this limit.
\r
181 * the dataset to measure
\r
182 * @return true if a limit is exceeded (what is the dataset is larger then
\r
183 * the limit), false otherwise. First check the number of sequences
\r
184 * in the dataset and if it exceeds the limit return true
\r
185 * irrespective of the average length. If the number of sequences in
\r
186 * the dataset is less than the limit and average length is defined,
\r
187 * then check whether the total number of letters (number of
\r
188 * sequence multiplied by the average sequence length) is greater
\r
189 * then the total number of letters in the dataset. Returns true if
\r
190 * the total number of letters in the dataset is greater than the
\r
191 * limit, false otherwise.
\r
193 public boolean isExceeded(List<FastaSequence> data) {
\r
194 if (data == null) {
\r
195 throw new NullPointerException(
\r
196 "List of fasta sequences is expected!");
\r
198 if (data.size() > this.seqNumber) {
\r
201 if (this.seqLength != 0 && data.size() > 0) {
\r
202 if ((long) getAvgSequenceLength(data) * data.size() > numberOfLetters()) {
\r
210 * Calculates an average sequence length of the dataset
\r
213 * @return an average sequence length in the input dataset
\r
215 public static int getAvgSequenceLength(List<FastaSequence> data) {
\r
217 for (FastaSequence seq : data) {
\r
218 length += seq.getLength();
\r
220 return (int) (length / data.size());
\r
224 if (this.seqNumber < 1) {
\r
225 throw new AssertionError(
\r
226 "Maximum number of sequences must be defined and be positive! Set value is: "
\r
229 if (this.seqLength != 0 && this.seqLength < 1) {
\r
230 throw new AssertionError(
\r
231 "Average sequence length must be positive! Set value is: "
\r