# FASTX.FASTA.Reader, no method matching error

**URL:** <https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240>\
**Category:** Biology, Health, and Medicine\
**Tags:** question, package\
**Created:** [November 29, 2021, 3:27pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240 "2021-11-29T15:27:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lamma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamma/32/31006_2.png) [@Lamma](https://discourse.julialang.org/u/Lamma)\
**Post date:** [November 29, 2021, 3:27pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/1 "2021-11-29T15:27:38Z")

</div>

I am using the ´FASTX` package to stream over a large fasta file but am getting a no method matching error:

```julia
ERROR: LoadError: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:334 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads ./threadingconstructs.jl:38
 [3] macro expansion
   @ ./threadingconstructs.jl:97 [inlined]
 [4] top-level scope
   @ /home/projects/ku_00014/people/robmur/scripts/metagenome/resampling/resampling.jl:38

    nested task error: MethodError: no method matching length(::FASTX.FASTA.Reader{TranscodingStreams.NoopStream{IOStream}})
    Closest candidates are:
      length(::Union{Base.KeySet, Base.ValueIterator}) at /services/tools/julia/1.7.0-rc3/share/julia/base/abstractdict.jl:58
      length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}) at /services/tools/julia/1.7.0-rc3/share/julia/stdlib/v1.7/LinearAlgebra/src/adjtrans.jl:171
      length(::Union{DataStructures.OrderedRobinDict, DataStructures.RobinDict}) at ~/.julia/packages/DataStructures/nBjdy/src/ordered_robin_dict.jl:86
      ...
    Stacktrace:
     [1] (::var"#2#threadsfor_fun#11"{FASTX.FASTA.Writer{TranscodingStreams.NoopStream{IOStream}}, Vector{String15}, FASTX.FASTA.Reader{TranscodingStreams.NoopStream{IOStream}}})(onethread::Bool)
       @ Main ./threadingconstructs.jl:53
     [2] (::var"#2#threadsfor_fun#11"{FASTX.FASTA.Writer{TranscodingStreams.NoopStream{IOStream}}, Vector{String15}, FASTX.FASTA.Reader{TranscodingStreams.NoopStream{IOStream}}})()
       @ Main ./threadingconstructs.jl:52
in expression starting at /home/projects/ku_00014/people/robmur/scripts/metagenome/resampling/resampling.jl:14

```

This started to occur after after I switched from a gzipped to an ungizziped fasta but I am unsure f that is the cause of the error. I am running the package as follows:

```julia
reader = FASTA.Reader(open("some/file/path/megahit_final_assembly_500bp_filterd.fasta"))

    writer = open(FASTA.Writer, "some/file/path/500bp_filter_samples/"*sample)

    println("writing to file")

    @threads for record in reader

        if FASTA.identifier(record) in passContig

            write(writer, record)

        end

    end

    close(reader)

```

EDIT: I just tried with a gzip and I get the same error.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 29, 2021, 6:50pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/2 "2021-11-29T18:50:42Z")

</div>

Have you gotten `@threads` to work before? It looks like the issue is that the reader doesn’t have a length method defined for it, which the threading code uses to divide the work up.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 29, 2021, 7:01pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/3 "2021-11-29T19:01:53Z")

</div>

You might have to do some preprocessing to prepare for multithreaded processing of your file.

Another issue though… I don’t think reading and writing is thread-safe, so multithreading this will likely cause your output to be jumbled up.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 29, 2021, 7:04pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/4 "2021-11-29T19:04:24Z")

</div>

Ah, `open` has a `lock` argument you can use to enable safe multithreaded access.

---

<div class="post-metadata">

**Author:** ![Lamma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamma/32/31006_2.png) [@Lamma](https://discourse.julialang.org/u/Lamma)\
**Post date:** [November 30, 2021, 8:31am UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/5 "2021-11-30T08:31:45Z")

</div>

I will try the `lock`argument. I was hoping it would just run multiple iterations of the for loop on different threads but whatever speeds things up is good 🙂 Looking more into it though, would multi-processing be more optimal for this then?

> Have you gotten `@threads` to work before? It looks like the issue is that the reader doesn’t have a length method defined for it, which the threading code uses to divide the work up.

No I have not used it before. I am wanting the nthreads to equal the available number.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 30, 2021, 1:14pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/6 "2021-11-30T13:14:49Z")

</div>

Yeah, it would be nice if `@threads` could work with a collection of unknown length. I think you would have the same, or worse, issue with multiprocessing.

Also your example code isn’t doing that much processing it looks like, is that right? If so, you’ll be IO-bound and parallel processing may not be much help anyway.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 30, 2021, 1:19pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/7 "2021-11-30T13:19:12Z")

</div>

Or you could do something like load a reasonably big chunk, process it in threads, load another chunk, etc.

---

<div class="post-metadata">

**Author:** ![Lamma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lamma/32/31006_2.png) [@Lamma](https://discourse.julialang.org/u/Lamma)\
**Post date:** [November 30, 2021, 1:21pm UTC](https://discourse.julialang.org/t/fastx-fasta-reader-no-method-matching-error/72240/8 "2021-11-30T13:21:34Z")

</div>

The object being iterated through is huge. It is a co-assembly from 22 metagenomic samples. So I assume that is where the long run time comes in. I am not sure of how I can speed that up natively though?
