-
Notifications
You must be signed in to change notification settings - Fork 4
Edit a Sequence
NucleotideSequenceBuilder is a class that allows code to programmatically construct an immutable NucleotideSequence in a similar manner to how a Java StringBuilder can build an immutable String. NucleotideSequenceBuilder uses method chaining which allows calls to its methods to get chained together for easier reading.
The build() method will return an immutable instance of the NucleotideSequence interface which will have the same sequence as the current sequence in the builder. The Builder class will choose which implementation to use and how to encode the sequence to improve performance.
Simple example:
//will create a NucleotideSequence which will have the sequence 'ACGT'
NucleotideSequence sequence = new NucleotideSequenceBuilder("ACGT").build();Sequence Builders can be created either via one of its constructors, or through one of the helper methods on the Sequence object.
A String of the input Sequence to start with, use either "-" or "*" to represent gaps.
new NucleotideSequenceBuilder("ACGT")An empty constructor or a constructor taking an int will create a new Builder with an empty sequence but with the initial given buffer capacity. The empty constructor will use the default buffer capacity.
new NucleotideSequenceBuilder()
.append("ACGT")new Builders can be created using an initial sequence based on another Sequence object.
There are actually 2 ways to create a new Builder object from a sequence. Jillion 5 introduced new methods on the Sequence object including toBuilder() which will make a new sequence object
Both of these code blocks below create a new NucleotideSequenceBuilder that is initialized to the complete sequence:
NucleotideSequence other = ...
NucleotideSequenceBuilder builder = other.toBuilder();The Builder objects also have constructors that take Sequences as input.
NucleotideSequence other = ...
NucleotideSequenceBuilder builder = new NucleotideSequenceBuilder(other);Both of these code blocks below create a new NucleotideSequenceBuilder that is initialized to the just a portion of the input sequence as specified by the Range parameter.
NucleotideSequence other = ...
Range range = Range.of(200, 500);
NucleotideSequenceBuilder builder = other.toBuilder(range);The Builder objects also have constructors that take Sequences as input.
NucleotideSequence other = ...
Range range = Range.of(200, 500);
NucleotideSequenceBuilder builder = new NucleotideSequenceBuilder(other, range);This is will produce the same resulting sequence as
NucleotideSequenceBuilder builder = new NucleotideSequenceBuilder(other)
.trim(range);but will be more efficient since the Builder doesn't have to read in the whole sequence, allocate all that memory and then throw most of it away.
NucleotideSequence ungappedSequence = new NucleotideSequenceBuilder("AC-GT-AC-GT")
.ungap()
.build();//ungappedSequence will now contain "ACGTACGT"
NucleotideSequenceBuilders have a method called reverseComplement() which will reverse complement the current sequence stored in the builder. Any future additions to the sequence will not be auto-complemented.
NucleotideSequence complementedSeq = new NucleotideSequenceBuilder("AAAAGGGG")
.reverseComplement()
.build();
//complementedSeq will now contain "CCCCTTTT"NucleotideSequenceBuilders use method chaining to allow users to write more intent revealing code:
NucleotideSequence seq = new NucleotideSequenceBuilder("AA--AAG-GG-G")
.reverseComplement()
.ungap()
.append("NNNN")
.build();
//seq will now contain "CCCCTTTTNNNN"