# Filter Sequences by Length BEFORE Downloading with get-ncbi-data

**URL:** <https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112>\
**Category:** Library Support\
**Tags:** rescript\
**Created:** [May 18, 2022, 5:43pm UTC](https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112 "2022-05-18T17:43:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexkrohn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/alexkrohn/32/11149_2.png) [@alexkrohn](https://forum.qiime2.org/u/alexkrohn)\
**Post date:** [May 18, 2022, 5:43pm UTC](https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112/1 "2022-05-18T17:43:23Z")

</div>

Hi there,

I've been using and loving `rescript` for my eDNA metabarcoding projects. I was wondering if it was possible to filter sequences returned from NCBI before downloading them.

For example, I'm doing a metabarcoding project using the 12S gene targeting fishes. One of the target species _Percina brevicauda_ (txid163812) has its entire mitochondrial genome sequenced, but does not have individual entries for just the 12S gene. If I were to use this search, I would not get any results:

```auto
qiime rescript get-ncbi-data --p-query '(12S[Title] OR 12 rRNA[Title]) AND txid163812[ORGN]' --o-sequences 12s-p-brevicauda-seqs.qza --o-taxonomy 12s-p-brevicauda-taxonomy.qza --p-n-jobs 20

```

But, loosening to include any entries with 12S anywhere in the entry will yield the two complete mitochondrial genomes.

```auto
qiime rescript get-ncbi-data --p-query '12S AND txid163812[ORGN]' --o-sequences 12s-p-brevicauda-seqs.qza --o-taxonomy 12s-p-brevicauda-taxonomy.qza --p-n-jobs 20

```

This strategy works for a single species, but I would like to download 12S sequences from all vertebrates (`--p-query '12S AND txid7742[ORGN]') for my metabarcoding work. Running that query yields 195k sequences, including 708 whole genomes or whole chromosomes. That will take a very very long time to download, and will likely take a ton of space.

Importantly, some of those larger sequences, especially the whole mitochondria, are useful (e.g. _P. brevicauda_), but many of the whole genomes or whole chromosomes are not. It would be wonderful to only download sequences shorter than, for example, 100kb.

I know I can use `qiime rescript filter-seqs-length` afterwards, but is there a way to do this before the sequences get downloaded? Or is there a better way to ensure you capture all of the 12S sequences on GenBank, regardless of whether they're in a whole genome, whole mitochondrion, or single 12S sequence?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![alexkrohn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/alexkrohn/32/11149_2.png) [@alexkrohn](https://forum.qiime2.org/u/alexkrohn)\
**Post date:** [May 18, 2022, 5:47pm UTC](https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112/2 "2022-05-18T17:47:33Z")

</div>

As another example, _Micropterus dolomieu_ has a whole genome, a whole mitochondrion, and partial 12S sequences present on GenBank. The only way I can think to filter these sequences to include the mitochondrion, the partial sequences and NOT the whole genome, would be to filter by sequence length. Is there a better way?

---

<div class="post-metadata">

**Author:** ![Nicholas\_Bokulich](https://forum.qiime2.org/user_avatar/forum.qiime2.org/nicholas_bokulich/32/19937_2.png) [@Nicholas\_Bokulich](https://forum.qiime2.org/u/Nicholas_Bokulich)\
**Post date:** [May 18, 2022, 6:04pm UTC](https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112/3 "2022-05-18T18:04:20Z")

</div>

Hi @alexkrohn ,  
Technically any valid entrez query should work. [According to the docs here](https://www.ncbi.nlm.nih.gov/books/NBK49540/), you can use the [SLEN] keyword to select a sequence length range like so:  
`100:1000[SLEN]`

I have never tried this... want to give it a spin and let us know what you find? (tip: you can search with this query on Genbank first to confirm that you eliminate unwanted sequences, before trying to download with RESCRIPt)

---

<div class="post-metadata">

**Author:** ![alexkrohn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/alexkrohn/32/11149_2.png) [@alexkrohn](https://forum.qiime2.org/u/alexkrohn)\
**Post date:** [May 19, 2022, 4:42pm UTC](https://forum.qiime2.org/t/filter-sequences-by-length-before-downloading-with-get-ncbi-data/23112/7 "2022-05-19T16:42:48Z")

</div>

Hi @Nicholas_Bokulich!

Thanks for tracking down `[SLEN]`, that's exactly what I was looking for. I confirmed it worked:

```auto
# This returns nothing
qiime rescript get-ncbi-data --p-query '12S AND 1:10000[SLEN] AND txid163812[ORGN]' --o-sequences 12s-p-brevicauda-seqs.qza --o-taxonomy 12s-p-brevicauda-taxonomy.qza --p-n-jobs 20

# Changing SLEN to 1:20kb will retrieve the two 16kb mtDNA genomes
qiime rescript get-ncbi-data --p-query '12S AND 1:20000[SLEN] AND txid163812[ORGN]' --o-sequences 12s-p-brevicauda-seqs.qza --o-taxonomy 12s-p-brevicauda-taxonomy.qza --p-n-jobs 20

```

Much appreciated!
