Skip to content

Commit cc84728

Browse files
Merge pull request carlobaldassi#15 from carlobaldassi/check_desc
Add check_description flag
2 parents 41292c4 + 6315667 commit cc84728

File tree

6 files changed

+85
-14
lines changed

6 files changed

+85
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ jobs:
4343
- uses: julia-actions/julia-buildpkg@v1
4444
- uses: julia-actions/julia-runtest@v1
4545
- uses: julia-actions/julia-processcoverage@v1
46-
- uses: codecov/codecov-action@v3
46+
- uses: codecov/codecov-action@v4
4747
with:
4848
files: lcov.info

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
codecov:
2+
token: b60713dd-5429-4e88-8a1e-d3a1f45a34f6

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ The FASTA format which is assumed by this module is as follows:
7272
at the beginning or end of the description
7373
5. Empty lines are ignored (note however that lines containing whitespace will still trigger an error)
7474

75-
When writing, description lines longer than 80 characters will trigger a warning message; sequence data is
76-
formatted in lines of 80 characters each; extra whitespace is silently discarded.
75+
When writing, description lines longer than 80 characters will trigger a warning message (this can be optionally
76+
disabled); sequence data is formatted in lines of 80 characters each; extra whitespace is silently discarded.
7777
No other restriction is put on the content of the sequence data, except that the `>` character is
7878
forbidden.
7979

src/FastaIO.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,15 @@ mutable struct FastaWriter
307307
entry::Int
308308
own_f::Bool
309309
at_start::Bool
310+
check_description::Bool
310311
function FastaWriter(io::IO)
311-
fw = new(io, false, 0, 0, false, 0, 1, false, true)
312+
fw = new(io, false, 0, 0, false, 0, 1, false, true, true)
312313
finalizer(close, fw)
313314
return fw
314315
end
315316
function FastaWriter(filename::AbstractString, mode::AbstractString = "w")
316317
fopen = endswith(filename, ".gz") ? gzopen : open
317-
fw = new(fopen(filename, mode), false, 0, 0, false, 0, 1, true, true)
318+
fw = new(fopen(filename, mode), false, 0, 0, false, 0, 1, true, true, true)
318319
finalizer(close, fw)
319320
return fw
320321
end
@@ -351,6 +352,9 @@ file.
351352
352353
The `FastaWriter` object has an `entry::Int` field which stores the number of the entry which is
353354
currently being written.
355+
356+
After creating the object, you can set the `check_description` field to `false` to disable the warning
357+
given when description lines are too long.
354358
"""
355359
function FastaWriter(f::Function, args...)
356360
fw = FastaWriter(args...)
@@ -442,7 +446,7 @@ function write(fw::FastaWriter, c)
442446
error("character '>' not allowed in sequence data (entry $(fw.entry) of FASTA input)")
443447
end
444448
if fw.pos == 80
445-
if !fw.in_seq
449+
if !fw.in_seq && fw.check_description
446450
@warn("description line longer than 80 characters (entry $(fw.entry) of FASTA input)")
447451
else
448452
write(fw.f, '\n')
@@ -553,11 +557,13 @@ function writefastaseq(io::IO, seq, entry::Int, nl::Bool = true)
553557
end
554558

555559
"""
556-
writefasta([io::IO = stdout], data)
560+
writefasta([io::IO = stdout], data; check_description=true)
557561
558562
This version of the function writes to an already opened `IO` stream, defaulting to `stdout`.
563+
564+
Set the keyword `check_description=false` to disable the warning message given when description lines are too long.
559565
"""
560-
function writefasta(io::IO, data)
566+
function writefasta(io::IO, data; check_description::Bool=true)
561567
entry = 0
562568
for (desc, seq) in data
563569
entry += 1
@@ -567,18 +573,18 @@ function writefasta(io::IO, data)
567573
if findfirst(==('\n'), desc) nothing
568574
error("newlines are not allowed within description (entry $entry of FASTA input)")
569575
end
570-
if length(desc) > 79
576+
if length(desc) > 79 && check_description
571577
@warn("description line longer than 80 characters (entry $entry of FASTA input)")
572578
end
573579
println(io, ">", desc)
574580
entry_chars = writefastaseq(io, seq, entry)
575581
entry_chars > 0 || error("empty sequence data (entry $entry of FASTA input)")
576582
end
577583
end
578-
writefasta(data) = writefasta(stdout, data)
584+
writefasta(data; kw...) = writefasta(stdout, data; kw...)
579585

580586
"""
581-
writefasta(filename::String, data, [mode::String = "w"])
587+
writefasta(filename::String, data, [mode::String = "w"]; check_description=true)
582588
583589
This function dumps data to a FASTA file, auto-formatting it so to follow the specifications detailed in
584590
the section titled [The FASTA format](@ref). The `data` can be anything which is iterable and which produces
@@ -597,11 +603,13 @@ If the `filename` ends with `.gz`, the result will be a gzip-compressed file.
597603
598604
The `mode` flag determines how the `filename` is open; use `"a"` to append the data to an existing
599605
file.
606+
607+
Set the keyword `check_description=false` to disable the warning message given when description lines are too long.
600608
"""
601-
function writefasta(filename::AbstractString, data, mode::AbstractString = "w")
609+
function writefasta(filename::AbstractString, data, mode::AbstractString = "w"; check_description=true)
602610
fopen = endswith(filename, ".gz") ? gzopen : open
603611
fopen(filename, mode) do f
604-
writefasta(f, data)
612+
writefasta(f, data; check_description)
605613
end
606614
end
607615

test/Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[deps]
22
GZip = "92fee26a-97fe-5a0c-ad85-20a5f3185b63"
3-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
4+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module FastaTests
33
using FastaIO
44
using GZip
55
using Test
6+
using Logging
67

78
const fastadata_ascii = Any[
89
("A0ADS9_STRAM/3-104",
@@ -202,4 +203,63 @@ end
202203
end
203204
end
204205

206+
outfile = joinpath(@__DIR__, "long_desc_test_out.fasta.gz")
207+
208+
@testset "desc length checks" begin
209+
longdesc = ">" * "X"^100
210+
data = "DATA"
211+
212+
FastaWriter(devnull) do fw
213+
b = IOBuffer()
214+
with_logger(SimpleLogger(b, Logging.Debug)) do
215+
write(fw, longdesc)
216+
write(fw, data)
217+
end
218+
s = String(take!(b))
219+
@test !isempty(s)
220+
221+
fw.check_description = false
222+
b = IOBuffer()
223+
with_logger(SimpleLogger(b, Logging.Debug)) do
224+
write(fw, longdesc)
225+
write(fw, data)
226+
end
227+
s = String(take!(b))
228+
@test isempty(s)
229+
end
230+
231+
b = IOBuffer()
232+
with_logger(SimpleLogger(b, Logging.Debug)) do
233+
writefasta(devnull, [(longdesc, data)])
234+
end
235+
s = String(take!(b))
236+
@test !isempty(s)
237+
238+
b = IOBuffer()
239+
with_logger(SimpleLogger(b, Logging.Debug)) do
240+
writefasta(devnull, [(longdesc, data)], check_description=false)
241+
end
242+
s = String(take!(b))
243+
@test isempty(s)
244+
245+
try
246+
b = IOBuffer()
247+
with_logger(SimpleLogger(b, Logging.Debug)) do
248+
writefasta(outfile, [(longdesc, data)])
249+
end
250+
s = String(take!(b))
251+
@test !isempty(s)
252+
253+
b = IOBuffer()
254+
with_logger(SimpleLogger(b, Logging.Debug)) do
255+
writefasta(outfile, [(longdesc, data)], check_description=false)
256+
end
257+
s = String(take!(b))
258+
@test isempty(s)
259+
finally
260+
isfile(outfile) && rm(outfile)
261+
end
262+
205263
end
264+
265+
end # module FastaTests

0 commit comments

Comments
 (0)