tldr: I want to convert [125, 119, 48, 126, 40]
to output string, }w0~(
To give a real life example, I am working with sequence data in fastq format (Here is a link to the library imported).
cat example.fastq
outputs the following:
@some/random/identifier
ACTAG
+
}w0~(
The julia code below demonstrates reading the fastq file:
import BioSequences.FASTQ
fastq_stream = FASTQ.Reader(open("example.fastq", "r"))
for record in fastq_stream
# Still need to learn, why this offset of 33?
println(
Vector{Int8}(FASTQ.quality(record, :sanger)) .+ 33
)
println(
String(FASTQ.sequence(record))
)
println(
String(FASTQ.identifier(record))
)
break
end
close(fastq_stream)
This code prints the following:
[125, 119, 48, 126, 40]
ACTAG
some/random/identifier
I don't want to have to store this information in a list. I would prefer to convert it to string. So the output I am looking for here is:
}w0~(
ACTAG
some/random/identifier
julia> String(UInt8.([125, 119, 48, 126, 40]))
"}w0~("
in Julia Strings are constructed using a set of bytes. If you are using ASCII only the char-byte mapping is simple and you can directly work on raw data (which is also the fastest way to do that).
Note that since Julia Strings are immutable, when creating String from raw bytes, the initial bytes become unavailable - this also means that no data is copied in the String
creation process. Have a look at the example below:
julia> mybytes = UInt8.([125, 119, 48, 126, 40]);
julia> mystring = String(mybytes)
"}w0~("
julia> mybytes
0-element Array{UInt8,1}
String
s in Julia are not internalized. In analytics scenarios always consider using Symbol
s instead of String
s. In some scenarios using temperature=:hot
instead of temperature="hot"
can mean 3x shorter execution time.
julia> using Random, BenchmarkTools;Random.seed!(0);
bb = rand(33:126,1000);
julia> @btime join(Char.($bb));
31.573 μs (13 allocations: 6.56 KiB)
julia> @btime String(UInt8.($bb));
711.111 ns (2 allocations: 2.13 KiB)
String(UInt8.($bb))
is over 40x faster and uses 1/3 of the memory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With