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.
\r
34 * @see LimitsManager
\r
38 * @version 1.0 January 2010
\r
41 * the type of an executable for which this limit is defined.
\r
44 @XmlAccessorType(XmlAccessType.FIELD)
\r
45 public class Limit<T> {
\r
47 private String preset;
\r
48 private int seqNumber;
\r
49 private int seqLength;
\r
55 // JAXB default constructor
\r
58 public Limit(int seqNumber, int seqLength, String preset) {
\r
59 if (seqNumber <= 0) {
\r
60 throw new IllegalArgumentException(
\r
61 "seqNumber - a maximum number of sequences to align must be greater than 0. Value given:"
\r
64 if (seqLength <= 0) {
\r
65 throw new IllegalArgumentException(
\r
66 "seqLength - an average sequence length must be greater than 0. Value given:"
\r
69 this.seqNumber = seqNumber;
\r
70 this.seqLength = seqLength;
\r
71 this.preset = preset;
\r
72 this.isDefault = false;
\r
75 public Limit(int seqNumber, int seqLength, String preset, boolean isDefault) {
\r
76 this(seqNumber, seqNumber, preset);
\r
77 this.isDefault = isDefault;
\r
80 public String getPreset() {
\r
84 public int getAvgSeqLength() {
\r
88 public int getSeqNumber() {
\r
92 public boolean isDefault() {
\r
97 public int hashCode() {
\r
98 final int prime = 31;
\r
100 result = prime * result + ((preset == null) ? 0 : preset.hashCode());
\r
101 result = prime * result + seqLength;
\r
102 result = prime * result + seqNumber;
\r
107 public boolean equals(Object obj) {
\r
112 if (getClass() != obj.getClass())
\r
114 Limit other = (Limit) obj;
\r
115 if (preset == null) {
\r
116 if (other.preset != null)
\r
118 } else if (!preset.equals(other.preset))
\r
120 if (seqLength != other.seqLength)
\r
122 if (seqNumber != other.seqNumber)
\r
128 public String toString() {
\r
131 value = "Default Limit" + SysPrefs.newlinechar;
\r
133 value = "Limit for Preset '" + preset + "'" + SysPrefs.newlinechar;
\r
135 value += "Maximum sequence number=" + seqNumber + SysPrefs.newlinechar;
\r
136 value += "Average sequence length=" + seqLength + SysPrefs.newlinechar;
\r
137 value += SysPrefs.newlinechar;
\r
141 long numberOfLetters() {
\r
142 return this.seqNumber * this.seqLength;
\r
146 * Checks if the number of sequences or their average length in the dataset
\r
147 * exceeds limits the values defined by this Limit
\r
150 * @return true if a limit is exceeded (what is the dataset is larger then
\r
151 * the limit), false otherwise.
\r
153 public boolean isExceeded(List<FastaSequence> data) {
\r
154 if (data == null) {
\r
155 throw new NullPointerException(
\r
156 "List of fasta sequences is expected!");
\r
158 if (data.size() > this.seqNumber) {
\r
161 if (this.seqLength != 0) {
\r
162 if ((long) getAvgSeqLength() * data.size() > numberOfLetters()) {
\r
170 * Calculates an average sequence length of the dataset
\r
173 * @return an average sequence length in the input dataset
\r
175 public static int getAvgSequenceLength(List<FastaSequence> data) {
\r
177 for (FastaSequence seq : data) {
\r
178 length += seq.getLength();
\r
180 return (int) (length / data.size());
\r
184 if (this.seqNumber < 1) {
\r
185 throw new AssertionError(
\r
186 "Maximum number of sequences must be defined and be positive! Set value is: "
\r
189 if (this.seqLength != 0 && this.seqLength < 1) {
\r
190 throw new AssertionError(
\r
191 "Average sequence length must be positive! Set value is: "
\r