# Modify FASTQ file description with a new string using FASTX

**URL:** <https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672>\
**Category:** Biology, Health, and Medicine\
**Tags:** question\
**Created:** [September 15, 2020, 11:20pm UTC](https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672 "2020-09-15T23:20:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopi1616](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@Gopi1616](https://discourse.julialang.org/u/Gopi1616)\
**Post date:** [September 15, 2020, 11:20pm UTC](https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672/1 "2020-09-15T23:20:30Z")

</div>

Hi, I am a python programmer and new to Julia. One of the tasks I am working on needs modification of description lines in Fastq sequence files.  
Doing this in python was pretty time-consuming. So wanted to try the same in Julia with the FASTX package.  
Can anyone recommend a method or provide an example to loop through each description and substitute with a modified line and writing it back to Fastq in an efficient way?

Here is the pseudocode of my python script

for every\_line in fastq:  
if line startswith('@) # description line

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [September 16, 2020, 12:35am UTC](https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672/2 "2020-09-16T00:35:39Z")

</div>

Welcome! This should be pretty straightforward, and may be faster than with python, especially if you don’t actually have to store/process the sequences themselves. Can you share what you’ve tried and what issues you’re running into?

It may also be helpful to take a look a look at [this post](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) which has some tips on how to format stuff if you’re including code.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 16, 2020, 12:42am UTC](https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672/3 "2020-09-16T00:42:06Z")

</div>

Did you read the documentation?  
[https://biojulia.net/FASTX.jl/stable/manual/fastq/](https://biojulia.net/FASTX.jl/stable/manual/fastq/)

What is it you do not understand?

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [September 22, 2020, 8:51am UTC](https://discourse.julialang.org/t/modify-fastq-file-description-with-a-new-string-using-fastx/46672/4 "2020-09-22T08:51:53Z")

</div>

You should check the documentation.

Nonetheless, I couldn’t resist giving an implementation a go. Here’s a simple implementation:

```julia
import FASTX.FASTQ: identifier, description, sequence, quality, Reader, Writer, Record

function modify_descriptions(f, inp::IO, out::IO)
    reader, writer = Reader(inp), Writer(out)
    for rec in reader
        write(writer, Record(identifier(rec), f(rec), sequence(String, rec), quality(rec)))
    end
end

```

You can use it like so:

```julia
f(x) = description(x) * "_with_extra_stuff"
inp = open("/my/input.fastq")
out = open("/tmp/test", "w")
modify_descriptions(f, inp, out)
close(inp)
close(out)

```

It’s not optimized. It does around 65 MB/s on my computer - good enough for most use cases? A fast version would need to have the following changes:

- Iterate over the FASTQ reader by overwriting a single FASTQ record until end of file
- Modify the description of the record in-place, ideally without any heap allocations
- Use a fork of FASTX with commit 18a160b merged

With these changes, it would probably be \> 500 MB/s uncompressed, or at whatever speed your computer can gzip compress with.
