Write a class DNASequence that has the following: a. Instance Variables 1. Name-
ID: 3770133 • Letter: W
Question
Write a class DNASequence that has the following:
a. Instance Variables 1. Name- the name of the creature the DNA belongs. 2. Sequence-a string which can only have the characters A, C, G, and T for the nucleotide bases.
b. Constructors 1. a default constructor set the name and sequence to some default value. 2. another constructor that takes a name and a sequence (making sure the sequence if valid)
c. Accessors and mutators for each instance variable 1. MAKE SURE TO CHECK FOR VALID VALUES! Remember the sequence can only contain the letters A,C,G, and T
d. Methods 1. a method getDifference which takes in another instance of a DNA sequence and returns a whole number that corresponds to the number of differences between the two sequences. The differences are counted based on letter-by-letter. 2. A static method mesh which takes two instances of DNASequence and returns a new sequence with each letter from sequence 1 and sequence 2 alternating.
Explanation / Answer
import org.apache.log4j.Logger;
import org.biojava3.core.sequence.DNASequence;
import org.biojava3.core.sequence.ProteinSequence;
import org.biojava3.core.sequence.RNASequence;
import java.io.*;
import java.security.InvalidParameterException;
public class SequenceSerializer {
private static final Logger LOGGER = Logger.getLogger(SequenceSerializer.class);
public static void main(String[] args) throws NotSerializableException, IOException {
File f = File.createTempFile("test", ".ser");
DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
RNASequence written = new RNASequence("acugacuacguagcugacugac");
writeRNASequence(written, out);
out.close();
RNASequence read = readRNASequence(new DataInputStream(new FileInputStream(f)));
System.out.println("size: " + f.length());
}
public static void writeDNASequence(DNASequence sequence, DataOutput out) throws IOException {
writeDNASequence(sequence.getSequenceAsString(), out);
}
private static void writeDNASequence(String sequence, DataOutput out) throws IOException {
out.writeInt(sequence.length());
for (int i = 0; i < sequence.length(); i += 4) {
String substring = sequence.substring(i, Math.min(i + 4, sequence.length()));
int value = 0;
for (int j = 0; j < 4; j++) {
value += (j < substring.length() ? getSerializedDNABase(substring.charAt(j)) : 0);
if (j != 3) {
value <<= 2;
}
}
out.writeByte(value);
}
}
public static void writeRNASequence(RNASequence sequence, DataOutput out) throws IOException {
int length = sequence.getLength();
String string = sequence.getSequenceAsString();
out.writeInt(length);
for (int i = 0; i < length; i += 4) {
String substring = string.substring(i, Math.min(i + 4, length));
int value = 0;
for (int j = 0; j < 4; j++) {
value += (j < substring.length() ? getSerializedRNABase(substring.charAt(j)) : 0);
if (j != 3) {
value <<= 2;
}
}
out.writeByte(value);
}
}
public static RNASequence readRNASequence(DataInput in) throws IOException {
int size = in.readInt();
char[] chars = new char[size];
int completed = 0;
while (completed < size) {
int value = in.readUnsignedByte();
chars[completed++] = getUnserializedRNABase((value >> 6));
if (completed < size) {
chars[completed++] = getUnserializedRNABase(((value >> 4) - ((value >> 6) << 2)));
}
if (completed < size) {
chars[completed++] = getUnserializedRNABase(((value >> 2) - ((value >> 4) << 2)));
}
if (completed < size) {
chars[completed++] = getUnserializedRNABase(((value) - ((value >> 2) << 2)));
}
}
return new RNASequence(new String(chars));
}
public static DNASequence readDNASequence(DataInput in) throws IOException {
int size = in.readInt();
char[] chars = new char[size];
int completed = 0;
while (completed < size) {
int value = in.readUnsignedByte();
chars[completed++] = getUnserializedDNABase((value >> 6));
if (completed < size) {
chars[completed++] = getUnserializedDNABase(((value >> 4) - ((value >> 6) << 2)));
}
if (completed < size) {
chars[completed++] = getUnserializedDNABase(((value >> 2) - ((value >> 4) << 2)));
}
if (completed < size) {
chars[completed++] = getUnserializedDNABase(((value) - ((value >> 2) << 2)));
}
}
return new DNASequence(new String(chars));
}
public static void writeProteinSequence(ProteinSequence sequence, DataOutput out) throws IOException {
String string = sequence.getSequenceAsString();
out.writeInt(string.length());
for (int i = 0; i < string.length(); i += 3) {
String substring = string.substring(i, Math.min(i + 3, string.length()));
int value = 0;
for (int j = 0; j < 3; j++) {
value += (j < substring.length() ? getSerializedAminoAcid(substring.charAt(j)) : 0);
if (j != 2) {
value <<= 5;
}
}
out.writeShort(value);
}
}
public static ProteinSequence readProteinSequence(DataInput in) throws IOException {
int size = in.readInt();
char[] chars = new char[size];
int completed = 0;
while (completed < size) {
int value = in.readShort();
chars[completed++] = getUnserializedAminoAcid((short) (value >> 10));
if (completed < size) {
chars[completed++] = getUnserializedAminoAcid((short) ((value >> 5) - ((value >> 10) << 5)));
}
if (completed < size) {
chars[completed++] = getUnserializedAminoAcid((short) ((value) - ((value >> 5) << 5)));
}
}
return new ProteinSequence(new String(chars));
}
private static int getSerializedAminoAcid(char c) throws NotSerializableException {
int index = (short) c;
index -= 64; // shift index A=1, Z=26
if (index < 1 || index > 26) {
switch (c) {
case ' ':
return 27;
case '+':
return 28;
case '-':
return 29;
default:
return 0;
}
}
return index;
}
private static char getUnserializedAminoAcid(int index) throws NotSerializableException {
if (index > 26 || index < 1) {
switch (index) {
case 27:
return ' ';
case 28:
return '+';
case 29:
return '-';
default:
return Character.UNASSIGNED; // should be checked
}
} else {
index += 64; // shift from A=0, Z=25
}
return (char) index;
}
private static int getSerializedDNABase(char c) throws NotSerializableException {
switch (c) {
case 'a':
return 0;
case 'c':
return 1;
case 'g':
return 2;
case 't':
return 3;
}
throw new InvalidParameterException("character is not a, t, c or g (lowercase)");
}
private static char getUnserializedDNABase(int index) throws NotSerializableException {
switch (index) {
case 0:
return 'a';
case 1:
return 'c';
case 2:
return 'g';
case 3:
return 't';
}
throw new InvalidParameterException("character is not a, t, c or g (lowercase)");
}
private static int getSerializedRNABase(char c) throws NotSerializableException {
switch (c) {
case 'a':
return 0;
case 'c':
return 1;
case 'g':
return 2;
case 'u':
return 3;
}
throw new InvalidParameterException("character is not a, t, c or g (lowercase)");
}
private static char getUnserializedRNABase(int index) throws NotSerializableException {
switch (index) {
case 0:
return 'a';
case 1:
return 'c';
case 2:
return 'g';
case 3:
return 'u';
}
throw new InvalidParameterException("character is not a, t, c or g (lowercase)");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.