Next version of JABA
[jabaws.git] / datamodel / compbio / metadata / Limit.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.metadata;\r
20 \r
21 import java.util.List;\r
22 \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
26 \r
27 import compbio.data.sequence.FastaSequence;\r
28 import compbio.util.SysPrefs;\r
29 \r
30 /**\r
31  * A value object containing a maximum number of sequences and a maximum average\r
32  * sequence length for a preset.\r
33  * \r
34  * @see LimitsManager\r
35  * \r
36  * @author pvtroshin\r
37  * \r
38  *         Date January 2010\r
39  * \r
40  * @param <T>\r
41  *            the type of an executable for which this limit is defined.\r
42  * \r
43  */\r
44 @XmlAccessorType(XmlAccessType.FIELD)\r
45 public class Limit<T> {\r
46 \r
47     private String preset;\r
48     private int seqNumber;\r
49     private int seqLength;\r
50 \r
51     @XmlAttribute\r
52     boolean isDefault;\r
53 \r
54     private Limit() {\r
55         // JAXB default constructor\r
56     }\r
57 \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
62                             + seqNumber);\r
63         }\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
67                             + seqLength);\r
68         }\r
69         this.seqNumber = seqNumber;\r
70         this.seqLength = seqLength;\r
71         this.preset = preset;\r
72         this.isDefault = false;\r
73     }\r
74 \r
75     public Limit(int seqNumber, int seqLength, String preset, boolean isDefault) {\r
76         this(seqNumber, seqNumber, preset);\r
77         this.isDefault = isDefault;\r
78     }\r
79 \r
80     public String getPreset() {\r
81         return preset;\r
82     }\r
83 \r
84     public int getAvgSeqLength() {\r
85         return seqLength;\r
86     }\r
87 \r
88     public int getSeqNumber() {\r
89         return seqNumber;\r
90     }\r
91 \r
92     public boolean isDefault() {\r
93         return isDefault;\r
94     }\r
95 \r
96     @Override\r
97     public int hashCode() {\r
98         final int prime = 31;\r
99         int result = 1;\r
100         result = prime * result + ((preset == null) ? 0 : preset.hashCode());\r
101         result = prime * result + seqLength;\r
102         result = prime * result + seqNumber;\r
103         return result;\r
104     }\r
105 \r
106     @Override\r
107     public boolean equals(Object obj) {\r
108         if (this == obj)\r
109             return true;\r
110         if (obj == null)\r
111             return false;\r
112         if (getClass() != obj.getClass())\r
113             return false;\r
114         Limit other = (Limit) obj;\r
115         if (preset == null) {\r
116             if (other.preset != null)\r
117                 return false;\r
118         } else if (!preset.equals(other.preset))\r
119             return false;\r
120         if (seqLength != other.seqLength)\r
121             return false;\r
122         if (seqNumber != other.seqNumber)\r
123             return false;\r
124         return true;\r
125     }\r
126 \r
127     @Override\r
128     public String toString() {\r
129         String value = "";\r
130         if (isDefault) {\r
131             value = "Default Limit" + SysPrefs.newlinechar;\r
132         } else {\r
133             value = "Limit for Preset '" + preset + "'" + SysPrefs.newlinechar;\r
134         }\r
135         value += "Maximum sequence number=" + seqNumber + SysPrefs.newlinechar;\r
136         value += "Average sequence length=" + seqLength + SysPrefs.newlinechar;\r
137         value += SysPrefs.newlinechar;\r
138         return value;\r
139     }\r
140 \r
141     long numberOfLetters() {\r
142         return this.seqNumber * this.seqLength;\r
143     }\r
144 \r
145     /**\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
148      * \r
149      * @param data\r
150      * @return true if a limit is exceeded (what is the dataset is larger then\r
151      *         the limit), false otherwise.\r
152      */\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
157         }\r
158         if (data.size() > this.seqNumber) {\r
159             return true;\r
160         }\r
161         if (this.seqLength != 0) {\r
162             if ((long) getAvgSeqLength() * data.size() > numberOfLetters()) {\r
163                 return true;\r
164             }\r
165         }\r
166         return false;\r
167     }\r
168 \r
169     /**\r
170      * Calculates an average sequence length of the dataset\r
171      * \r
172      * @param data\r
173      * @return an average sequence length in the input dataset\r
174      */\r
175     public static int getAvgSequenceLength(List<FastaSequence> data) {\r
176         long length = 0;\r
177         for (FastaSequence seq : data) {\r
178             length += seq.getLength();\r
179         }\r
180         return (int) (length / data.size());\r
181     }\r
182 \r
183     void validate() {\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
187                             + this.seqNumber);\r
188         }\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
192                             + this.seqLength);\r
193         }\r
194     }\r
195 }\r