6
$\begingroup$

I want to find the length of a list of genes, of Homo sapiens, that is reported in the GEO database. I have gathered the ensembl id's of those genes.

I understand this information can be parsed from the start and end position given in GTF file available in ensembl.org.

I would like to ask for advice on how to programmatically download the gtf files with ensembl id's.

On the ensembl webpage a couple of options are listed, API and ftp access. I would like to know if sample scripts are available.

Update: I found a script here, it shows how to parse the data from gtf file. Could someone help in understanding how the code can be modified to obtain gene length from start and end position?

Edit 1: By start and end position, I refer to the start and end values given in the "fields" section here and the file from which I want to parse the values is the second last file available in this link.
For instance, the end - start value of the gene with ensembl id ENSG00000131482, gene :G6PC

Edit 2: I get the gene names and the expression values from the SOFT files documented in GEO, link

The following is a sample output from SOFT file. It gives me the probe_set(ID_REF) , Gene name and expression values from samples (GSMXXXXXX). Here we see fro G6PC, values are reported from two probe_sets. Can we get the transcript id's from the probe_sets?

Obtaining the transcript id will help me in finding the length of the specific transcript.

> Table(gds)[2449,0:5]
           ID_REF IDENTIFIER GSM388749 GSM388750 GSM388753
2449 1555612_s_at       G6PC   4.39472   4.52235   4.26869
> Table(gds)[16399,0:5]
         ID_REF IDENTIFIER GSM388749 GSM388750 GSM388753
16399 206952_at       G6PC   2.76693   3.45107    2.9815
$\endgroup$
10
  • 1
    $\begingroup$ What do you mean by gene length? Is that the distance from the start of the first CDS to the end of the last CDS in the gene, the length of the longest transcript, or something else? $\endgroup$
    – gringer
    Aug 26, 2018 at 6:06
  • 1
    $\begingroup$ The start and the end position given in gtf files(I'm trying to obtain thi length), does it include both the coding domain and the non-coding domain? I'm a beginner in this field, excuse me for the naive questions $\endgroup$
    – Natasha
    Aug 26, 2018 at 6:19
  • $\begingroup$ That would then be coding, non-coding and intronic regions. What do you need these values for? $\endgroup$
    – Devon Ryan
    Aug 26, 2018 at 11:07
  • $\begingroup$ Can you give us an example? Show us a few lines of the GTF file and the output you expect to see from them. $\endgroup$
    – terdon
    Aug 26, 2018 at 11:55
  • 1
    $\begingroup$ @DevonRyan Realized that after doing the whole task. Thanks anyway:) $\endgroup$
    – Natasha
    Sep 11, 2018 at 14:46

4 Answers 4

3
$\begingroup$

If you really just want the start and end positions of all genes in a list of genes from Homo_sapiens.GRCh38.93.gtf.gz, you could do this in awk. For example, if your list of genes is:

$ cat genes
ENSG00000131482
ENSG00000223972
ENSG00000228794
ENSG00000187961

You can do:

$ awk '{ 
        if(NR==FNR){
            wantedGenes[$1]++ 
        } 
        else{
            if($3=="gene"){
                gsub(/";*/,"",$10); 
                if($10 in wantedGenes){
                    print $10,$4,$5
                } 
            }
        }
      }' genes Homo_sapiens.GRCh38.93.gtf 
ENSG00000223972 11869 14409
ENSG00000228794 825138 859446
ENSG00000187961 960587 965715
ENSG00000131482 42900797 42913369

Explanation:

  • if(NR==FNR){ wantedGenes[$1]++ } : NR is the input line number and FNR is the current line number of this input file. The two are only equal while reading the first file. So, if this is the first file, save the first field of each line (the gene name) in the array wantedGenes.
  • if($3=="gene"){ : if this line is a 'gene' entry.
  • gsub(/";*/,"",$10); : the gene names in the GTF file are quoted and have an ; after the quote. For example: "ENSG00000187961";. Since we don't have this in the list, we need to remove it so we can match the gene name. This gsub command will remove all " followed by 0 or more ; (the ;* means 0 or more occurrences of ;) from the 10th field of each line. So, it will replace "ENSG00000187961"; with ENSG00000187961.
  • if($10 in wantedGenes){print $10,$4,$5} : if the 10th field (the modified gene name from above) is in the wantedGenes array, then print the 10th field (the gene name), the 4th (start) and the 5th field (end).

You can easily modify this to print the length instead of the start and end positions:

$ awk '{ 
        if(NR==FNR){
            wantedGenes[$1]++ 
        } 
        else{
            if($3=="gene"){
                gsub(/";*/,"",$10); 
                if($10 in wantedGenes){
                    print $10,$5-$4
                } 
            }
        }
      }' genes Homo_sapiens.GRCh38.93.gtf 
ENSG00000223972 2540
ENSG00000228794 34308
ENSG00000187961 5128
ENSG00000131482 12572

As requested in chat, here's a python version of the same thing:

#!/usr/bin/env python3
import re
import sys

wantedGenes = []
with open(sys.argv[1], mode='r') as f:
    wantedGenes = f.readlines()

wantedGenes = [ x.rstrip() for x in wantedGenes]

print('# Gene\tstart\tend\tlength')
with open(sys.argv[2], mode='r') as f:
    for line in f:
        if not line.startswith('#'): 
            fields = line.split('\t')
            annotations = fields[8].split(' ')
            geneName = re.sub('[";]', '', annotations[1])
            if geneName in wantedGenes and fields[2] == 'gene':
                print('%s\t%s\t%s\t%d' %
                      (geneName, fields[3],fields[4],
                       int( int(fields[4]) - int(fields[3]) )
                      ))

Save the file as foo.py and run with the file names as arguments:

python foo.py geneList.txt Homo_sapiens.GRCh38.93.gtf
$\endgroup$
3
$\begingroup$

You could also do this in R with the GenomicFeatures library.

library(GenomicFeatures)

## make TxDb from GTF file 
txdb <- makeTxDbFromGFF('Homo_sapiens.GRCh38.93.gtf')

## get gene information
all.genes <- genes(txdb)

## import your list of gene names
my.genes <- c('ENSG00000141510','ENSG00000184571','ENSG00000011007')

## get the length of each of those genes
my.genes.lengths <- width(all.genes[my.genes])
## put the names back on the lengths
names(my.genes.lengths) <- my.genes

## print lengths
print(my.genes.lengths)

ENSG00000141510 ENSG00000184571 ENSG00000011007
      25772           55687           18905
$\endgroup$
0
$\begingroup$

You can do this directly on BioMart site from EBI as well. Using a list of ENSG ids as input, you can get the actual length of each individual transcript, as well as the length of the genomic region coding said transcripts. These are the attributes I used:

Gene stable ID
Transcript stable ID
Transcript length (including UTRs and CDS)
Gene start (bp)
Gene end (bp)

The output is a table with the relevant data:

Gene ID Transcript ID   Transcript length (including UTRs and CDS)  Gene start (bp) Gene end (bp)
ENSG00000146648 ENST00000455089 3844    55019021    55211628
ENSG00000146648 ENST00000342916 2239    55019021    55211628

You can change the build as well if you don't want to use GRCh38.

$\endgroup$
0
$\begingroup$

If you are an R user, then you can easily do so with biomaRt

library("biomaRt")
library("dplyr")

genes <- read.delim("genes.file.txt", header=T)

head(genes)
          gene_id
1 ENSG00000000003
2 ENSG00000000005
3 ENSG00000000419
4 ENSG00000000457
5 ENSG00000000460
6 ENSG00000000938

## Build a biomart query 
# In the example below, I use the human gene annotation from Ensembl release 82 located on "sep2015.archive.ensembl.org", More about the ensembl_build can be found on "http://www.ensembl.org/info/website/archives/index.html"

dataset="hsapiens_gene_ensembl"
mart = biomaRt::useMart("ENSEMBL_MART_ENSEMBL", dataset = dataset, 
host = paste0("sep2015", ".archive.ensembl.org"), path = "/biomart/martservice", archive = FALSE)

annotations <- biomaRt::getBM(mart = mart, attributes=c("ensembl_gene_id", "external_gene_name", "start_position", "end_position"))
annotations <- dplyr::transmute(annotations, ensembl_gene_id, external_gene_name, gene_length = end_position - start_position)

# Filter and re-order gene.annotations to match the order in your input genes list
final.genes <- annotations %>% dplyr::filter(ensembl_gene_id %in% genes$gene_id)
final.genes <- final.genes[order(match(final.genes$ensembl_gene_id, genes$gene_id)),]; rownames(final.genes) <-NULL

head(final.genes)
  ensembl_gene_id external_gene_name gene_length
1 ENSG00000000003             TSPAN6       12882
2 ENSG00000000005               TNMD       15083
3 ENSG00000000419               DPM1       23688
4 ENSG00000000457              SCYL3       44636
5 ENSG00000000460           C1orf112      192073
6 ENSG00000000938                FGR       23213

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.