<p>You could do a few command-line operations to answer this question. This assumes the use of <code>hg38</code> assembly.</p>
<p>First, get a list of genes from GENCODE:</p>
<pre><code><span class="math-container">$ wget -qO- ftp://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_28/gencode.v28.annotation.gff3.gz \
    | gunzip --stdout - \
    | awk '$</span>3 == &quot;gene&quot;' - \
    | convert2bed -i gff - \
    &gt; genes.bed
</code></pre>
<p>Then use <code>grep</code> to filter those genes with your list of HGNC symbols:</p>
<pre><code>$ grep -wFf hgnc_symbols.txt genes.bed &gt; filtered_genes.bed
</code></pre>
<p>You can run this through <code>awk</code> to get lengths:</p>
<pre><code><span class="math-container">$ awk -vFS="\t" '{ print $3-$</span>2 }' filtered_genes.bed &gt; filtered_gene_lengths.txt
</code></pre>

CC BY-SA 4.0
2020-08-26T08:21:28.420Z
Thank you for this, this looks great. I should've said I am using hg19, my own fault, but I can figure it out from here so thank you :)
DN1
CC BY-SA 4.0
2020-08-25T20:13:17.977Z
2020-08-25T20:13:17.977Z
Alex Reynolds
<p>I have a list of HGNC gene symbols, I am looking to get the gene length of each gene. Although I also describe these genes with lots of UCSC datasets as features, so I am wondering if there is a dataset in UCSC I can use to also get gene length from?</p>
<p>I've been looking in the data that is downloadable from UCSC table browser (I've been aiming to find start and ends for each gene to subtract to get gene length) but there are a lot of files and I'm not sure which dataset to take from which will also match to my HGNC gene symbols.</p>

<gene><genomics><ucsc>
How to annotate gene length to a list of gene symbols using UCSC data?
# How to annotate gene length to a list of gene symbols using UCSC data?

(Asked by: username_0 on 2020-08-25)

I have a list of HGNC gene symbols, I am looking to get the gene length of each gene. Although I also describe these genes with lots of UCSC datasets as features, so I am wondering if there is a dataset in UCSC I can use to also get gene length from?

I've been looking in the data that is downloadable from UCSC table browser (I've been aiming to find start and ends for each gene to subtract to get gene length) but there are a lot of files and I'm not sure which dataset to take from which will also match to my HGNC gene symbols.

**Tags:** gene, genomics, ucsc

---

## 1 answers

### Answer by username_1 on 2020-08-25. Score: 2 | Accepted answer

You could do a few command-line operations to answer this question. This assumes the use of hg38 assembly.
First, get a list of genes from GENCODE:
$ wget -qO- ftp://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_28/gencode.v28.annotation.gff3.gz \
    | gunzip --stdout - \
    | awk '$3 == "gene"' - \
    | convert2bed -i gff - \
    > genes.bed

Then use grep to filter those genes with your list of HGNC symbols:
$ grep -wFf hgnc_symbols.txt genes.bed > filtered_genes.bed

You can run this through awk to get lengths:
$ awk -vFS="\t" '{ print $3-$2 }' filtered_genes.bed > filtered_gene_lengths.txt

> **Comments:**
> **username_0**: Thank you for this, this looks great. I should've said I am using hg19, my own fault, but I can figure it out from here so thank you :)  

---

