-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDNASequence.java
More file actions
73 lines (67 loc) · 2.19 KB
/
DNASequence.java
File metadata and controls
73 lines (67 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Objects;
/**
* Represent one labeled DNA sequence in the database.
* @author Dr. Jody Paul
* @version 2026-03-10a
*/
public class DNASequence {
private final String label;
private final String sequence;
/**
* Create a labeled DNA sequence.
*
* @param label the label associated with the sequence
* @param sequence the DNA sequence text
* @return a new DNASequence object
*/
public DNASequence(String label, String sequence) {
this.label = Objects.requireNonNull(label, "label must not be null");
this.sequence = normalizeAndValidate(sequence);
}
/**
* Return the label for this sequence.
* @return the label
*/
public String getLabel() {
return label;
}
/**
* Return the normalized DNA sequence.
* @return the DNA sequence
*/
public String getSequence() {
return sequence;
}
/**
* Normalize the input sequence and check that it contains only valid DNA bases.
*
* @param sequence the sequence to normalize and validate
* @return the normalized uppercase sequence
* @throws NullPointerException if sequence is null
* @throws IllegalArgumentException if sequence is empty or contains invalid characters
*/
public static String normalizeAndValidate(String sequence) {
Objects.requireNonNull(sequence, "sequence must not be null");
String normalized = sequence.trim().toUpperCase();
if (normalized.isEmpty()) {
throw new IllegalArgumentException("sequence must not be empty");
}
for (int i = 0; i < normalized.length(); i++) {
char base = normalized.charAt(i);
if (base != 'A' && base != 'C' && base != 'G' && base != 'T') {
throw new IllegalArgumentException(
"Invalid DNA base '" + base + "' in sequence: " + normalized
);
}
}
return normalized;
}
/**
* Return a readable string representation of this object.
* @return a string containing the label and sequence
*/
@Override
public String toString() {
return label + ": " + sequence;
}
}