Skip to content

Commit

Permalink
Implement copy! for records
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed May 2, 2023
1 parent 4e2db69 commit 149d3a7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.1.0]
### Additions
* Implement `Base.copy!` for `FASTQRecord` and `FASTARecord`

## [2.0.1]
### Bugfix
* Fix `Base.read!(::FASTQReader, ::FASTQRecord)` (issue #95)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FASTX"
uuid = "c2308a5c-f048-11e8-3e8a-31650f418d12"
authors = ["Sabrina J. Ward <[email protected]>", "Jakob N. Nissen <[email protected]>"]
version = "2.0.1"
version = "2.1.0"

[weakdeps]
BioSequences = "7e6ae17a-c86d-528c-b3b9-7f778a29fe59"
Expand Down
10 changes: 10 additions & 0 deletions src/fasta/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ function Base.copy(record::Record)
)
end

function Base.copy!(dst::Record, src::Record)
n = filled(src)
length(dst.data) < n && resize!(dst.data, n)
unsafe_copyto!(dst.data, 1, src.data, 1, n)
dst.identifier_len = src.identifier_len
dst.description_len = src.description_len
dst.sequence_len = src.sequence_len
dst
end

function Base.write(io::IO, record::Record)
data = record.data
write(io, UInt8('>'))
Expand Down
10 changes: 10 additions & 0 deletions src/fastq/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ function Base.copy(record::Record)
)
end

function Base.copy!(dst::Record, src::Record)
n = filled(src)
length(dst.data) < n && resize!(dst.data, n)
unsafe_copyto!(dst.data, 1, src.data, 1, n)
dst.identifier_len = src.identifier_len
dst.description_len = src.description_len
dst.has_description_seq_len = src.has_description_seq_len
dst
end

function Base.write(io::IO, record::Record)
data = record.data
len = UInt(seqsize(record))
Expand Down
24 changes: 19 additions & 5 deletions test/fasta/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
empty!(record)
@test record == record2

# Copying
cp = copy(record)
@test record == cp
@test record.data !== cp.data

# Components of empty records
@test identifier(record) isa AbstractString
@test isempty(identifier(record))
Expand Down Expand Up @@ -129,6 +124,25 @@ end
@test sequence(String, cp) == "OOJMQQ"
end

@testset "Copying" begin
record = parse(Record, ">some_identifier \tmy_description | text\nAAT\nTA\nCCG")
resize!(record.data, 1000)
cp = copy(record)

@test record !== cp
@test record == cp
@test sequence(record) == sequence(cp)
@test identifier(record) == identifier(cp)
@test description(record) == description(cp)
@test record.data !== cp.data

record = parse(Record, ">another record\r\nUAGWMPK\nKKLLAAM")
@test record != cp
copy!(record, cp)
@test record !== cp
@test record == cp
end

# Get sequence as String/StringView
@testset "Get sequence" begin
record = Record(codeunits("ab cAACCAAGGTTKKKMMMM"), 2, 4, 10)
Expand Down
29 changes: 22 additions & 7 deletions test/fastq/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
empty!(record)
@test record == record2

# Copying
resize!(record.data, 1000)
cp = copy(record)
empty!(record.data)
@test record == cp
@test record.data !== cp.data

# Components of empty records
@test identifier(record) == ""
@test description(record) == ""
Expand Down Expand Up @@ -122,6 +115,28 @@ end
end
end

@testset "Copying" begin
record = parse(Record, "@A\nTGGAA\n+\nJJJAA")
resize!(record.data, 1000)
cp = copy(record)

@test record !== cp
@test record == cp
@test sequence(record) == sequence(cp)
@test identifier(record) == identifier(cp)
@test description(record) == description(cp)
@test quality(record) == quality(cp)
@test record.data !== cp.data
# Test that we don't unnecessarily copy noncoding data
@test length(record.data) > length(cp.data)

record = parse(Record, "@some other record\r\nTAGA\r\n+\r\nJJJK")
@test record != cp
copy!(record, cp)
@test record !== cp
@test record == cp
end

@testset "Get sequence as String/StringView" begin
records = map(i -> parse(Record, i), TEST_RECORD_STRINGS)

Expand Down

2 comments on commit 149d3a7

@jakobnissen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82686

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.1.0 -m "<description of version>" 149d3a79bb62a22401f9da151e083b9faa467447
git push origin v2.1.0

Please sign in to comment.