<p><code>seqkit stats fastq.gz</code>. Get SeqKit from <a href="https://bioinf.shenwei.me/seqkit/usage/" rel="nofollow noreferrer">https://bioinf.shenwei.me/seqkit/usage/</a></p>

CC BY-SA 4.0
2023-10-11T05:37:31.383Z
2023-10-11T05:37:31.383Z
user3479780
<p><strong>Using Perl:</strong><br />
Print the sequence lines (line number 2, 6, 10, etc). Remove the newlines with <code>chomp</code>. Count the bytes (here, bases) using <code>wc -c</code>:</p>
<pre><code>perl -ne 'if ( $. % 4 == 2 ) { chomp; print; }' | wc -c
</code></pre>

CC BY-SA 4.0
2020-09-01T13:11:46.020Z
2020-09-01T13:11:46.020Z
Timur Shtatland
<p>The number of bases in a fastq file can be counted in bash with awk and wc</p>
<pre><code>awk 'NR % 4 == 0' ORS=&quot;&quot; fastqfile|wc -m
</code></pre>
<p>The awk code prints every fourth line (which is actually the quality scores, but that doesn't matter here). wc -m returns the total number of characters.</p>
<p>Edited to add ORS=&quot;&quot; to prevent count of newlines (the double quotes are not strictly required)</p>

CC BY-SA 4.0
2020-09-01T10:08:50.737Z
2020-09-01T12:35:09.083Z
2020-09-01T12:35:09.083Z
Greg
<p>Using a fastq parser in Python (for instance the one provided by <code>mappy</code>), it is rather straightforward to compute the sum of the lengths of the sequences present in a fastq file:</p>
<pre><code><span class="math-container">$ python3 -m pip install mappy  # Only if mappy is not installed yet
$</span> python3 -c 'from mappy import fastx_read; print(sum(len(seq) for (_, seq, *_) in fastx_read(&quot;SRR077487_2.filt.fastq.gz&quot;)))'
2386161200
</code></pre>
<p><code>python3 -c '&lt;insert some python code here&gt;'</code> uses the <code>-c</code> option of <code>python3</code> that tells Python to execute the provided code (instead of starting an interactive interpreter, or running code present in a file).
Here the code has two steps (separated by a <code>;</code>, in a standard script, we could use a new line instead): first making the <code>fastx_read</code> function available (because it is part of an optional module), second displaying the total length of the sequences.</p>
<p><code>fastx_read(&quot;path_to_a_file&quot;)</code> generates (name, sequence, quality) triplets when a fastq file is provided, or (name, sequence) pairs when a fasta file is provided.</p>
<p>The <code>(_, seq, *_)</code> syntax is a form of &quot;tuple unpacking&quot; where we store the second element (the sequence) in a <code>seq</code> variable.
The <code>_</code> in is just a way to ignore the first element in the triplet/pair. The <code>*_</code> is a way to ignore whatever is after the second element in the triplet/pair.</p>
<p><code>len(seq) for (_, seq, *_) in fastx_read(&quot;SRR077487_2.filt.fastq.gz&quot;)</code> is an expression representing the lengths (obtained with the predefined <code>len</code> function) of the sequences present in the file <code>&quot;SRR077487_2.filt.fastq.gz&quot;</code>.</p>
<p>So the code uses the predefined <code>sum</code> function to compute the sum of values that are the lengths of the sequences extracted from the fastq file, and this is displayed using the <code>print</code> function.</p>
<p>This should also work with either fasta or fastq format, and with or without gz compression.</p>
<p>I tested more parsers on a slightly different problem here: <a href="https://bioinformatics.stackexchange.com/a/380/292">https://bioinformatics.stackexchange.com/a/380/292</a> and you can find inspiration with the other approaches proposed in the other answers there.</p>

CC BY-SA 4.0
2020-09-04T07:09:20.300Z
2020-09-04T07:32:54.577Z
2020-09-04T07:32:54.577Z
bli
<p>With bbmap:</p>
<p><code>reformat.sh &lt;/path/to/fastq&gt;</code></p>

CC BY-SA 4.0
2020-09-01T11:54:13.557Z
2020-09-01T11:54:13.557Z
Pallie
<p>I've been using this:</p>
<p><code>cat file.fastq | paste - - - - | cut -f 2 | tr -d '\n' | wc -c </code></p>
<p>Explanation :</p>
<ul>
<li><p><code>paste - - - -</code> : print four consecutive lines in one row (tab delimited), to merge the info for each read</p>
</li>
<li><p><code>cut -f2</code> : print only the second column, to access the sequence after the paste</p>
</li>
<li><p><code>wc -c</code> : count the characters</p>
</li>
<li><p><code>tr -d '\n'</code>: to remove from count the eventual newline characters</p>
</li>
</ul>
<p>(a tip for your googling: try search for &quot;counting number of bases in fastq file&quot;)</p>

CC BY-SA 4.0
2020-09-01T13:17:57.003Z
2020-09-01T13:17:57.003Z
aechchiki
<p>Or Kbps/Gbps. It feels like it should be conceptually very simple, but I can't seem to figure out the right combination of keywords to find it via my search engine. Help would be appreciated!</p>
<p>I have BBMAP, SRAtoolkit and MEGAHIT already installed, and also use bash. I'd be very happy if this can be answered with software that I already have, but if not that's perfectly fine.</p>

<fastq><bash>
How to measure the total size of a fastq file in base pairs?
# How to measure the total size of a fastq file in base pairs?

(Asked by: username_0 on 2020-09-01)

Or Kbps/Gbps. It feels like it should be conceptually very simple, but I can't seem to figure out the right combination of keywords to find it via my search engine. Help would be appreciated!

I have BBMAP, SRAtoolkit and MEGAHIT already installed, and also use bash. I'd be very happy if this can be answered with software that I already have, but if not that's perfectly fine.

**Tags:** fastq, bash

---

## 6 answers

### Answer by username_6 on 2020-09-01. Score: 7 | Accepted answer

I've been using this:

`cat file.fastq | paste - - - - | cut -f 2 | tr -d '\n' | wc -c`

Explanation :

* `paste - - - -` : print four consecutive lines in one row (tab delimited), to merge the info for each read
* `cut -f2` : print only the second column, to access the sequence after the paste
* `wc -c` : count the characters
* `tr -d '\n'`: to remove from count the eventual newline characters

(a tip for your googling: try search for "counting number of bases in fastq file")


---

### Answer by username_1 on 2023-10-11. Score: 1

`seqkit stats fastq.gz`. Get SeqKit from <https://bioinf.shenwei.me/seqkit/usage/>


---

### Answer by username_2 on 2020-09-01. Score: 1

Using Perl:
Print the sequence lines (line number 2, 6, 10, etc). Remove the newlines with chomp. Count the bytes (here, bases) using wc -c:
perl -ne 'if ( $. % 4 == 2 ) { chomp; print; }' | wc -c


---

### Answer by username_3 on 2020-09-01. Score: 1

The number of bases in a fastq file can be counted in bash with awk and wc
awk 'NR % 4 == 0' ORS="" fastqfile|wc -m

The awk code prints every fourth line (which is actually the quality scores, but that doesn't matter here). wc -m returns the total number of characters.
Edited to add ORS="" to prevent count of newlines (the double quotes are not strictly required)


---

### Answer by username_4 on 2020-09-04. Score: 1

Using a fastq parser in Python (for instance the one provided by mappy), it is rather straightforward to compute the sum of the lengths of the sequences present in a fastq file:
$ python3 -m pip install mappy  # Only if mappy is not installed yet
$ python3 -c 'from mappy import fastx_read; print(sum(len(seq) for (_, seq, *_) in fastx_read("SRR077487_2.filt.fastq.gz")))'
2386161200

python3 -c '' uses the -c option of python3 that tells Python to execute the provided code (instead of starting an interactive interpreter, or running code present in a file).
Here the code has two steps (separated by a ;, in a standard script, we could use a new line instead): first making the fastx_read function available (because it is part of an optional module), second displaying the total length of the sequences.
fastx_read("path_to_a_file") generates (name, sequence, quality) triplets when a fastq file is provided, or (name, sequence) pairs when a fasta file is provided.
The (_, seq, *_) syntax is a form of "tuple unpacking" where we store the second element (the sequence) in a seq variable.
The _ in is just a way to ignore the first element in the triplet/pair. The *_ is a way to ignore whatever is after the second element in the triplet/pair.
len(seq) for (_, seq, *_) in fastx_read("SRR077487_2.filt.fastq.gz") is an expression representing the lengths (obtained with the predefined len function) of the sequences present in the file "SRR077487_2.filt.fastq.gz".
So the code uses the predefined sum function to compute the sum of values that are the lengths of the sequences extracted from the fastq file, and this is displayed using the print function.
This should also work with either fasta or fastq format, and with or without gz compression.
I tested more parsers on a slightly different problem here: https://bioinformatics.stackexchange.com/a/380/292 and you can find inspiration with the other approaches proposed in the other answers there.


---

### Answer by username_5 on 2020-09-01. Score: 0

With bbmap:

`reformat.sh`


---

