Showing posts with label GTF. Show all posts
Showing posts with label GTF. Show all posts

Thursday, August 18, 2011

Converting Cufflinks .GTF predictions to .BED files

Cufflinks writes its predictions as .GTF files. However, this file type is sometimes too large to process. For example, it may be too large to upload to UCSC gene browser (even you compress it). IGV browser also takes more memory resources to process .GTF file than processing BED file. So I wrote a small script (in Python 3) to convert GTF formatted files to BED files. This helps to reduce the file size dramatically. For example, this script converts a 120M GTF file to only 9M BED file, reducing the size by more than 90%!

Of course there are general tools to convert .GTF to .BED. For example, here is one Perl script to do this. However, my script is written specifically for Cufflinks .GTF files: it recognizes "gene_id", "transcript_id" and "FPKM" key word in attribute lists. "transcript_id" is converted as the name field in .BED file, and "FPKM" value is rounded as "score" field in .BED file.

Here is one example of .GTF file predicted by Cufflinks:


chr1    Cufflinks       transcript      934797  935655  324     -       .       gene_id "CUFF.29"; transcript_id "CUFF.29.3"; FPKM "23.5107601622"; frac "0.216036"; conf_lo "20.031437"; conf_hi "26.990083"; cov "84.378021"; full_read_support "yes";
chr1    Cufflinks       exon    934797  934812  324     -       .       gene_id "CUFF.29"; transcript_id "CUFF.29.3"; exon_number "1"; FPKM "23.5107601622"; frac "0.216036"; conf_lo "20.031437"; conf_hi "26.990083"; cov "84.378021";
chr1    Cufflinks       exon    934906  935655  324     -       .       gene_id "CUFF.29"; transcript_id "CUFF.29.3"; exon_number "2"; FPKM "23.5107601622"; frac "0.216036"; conf_lo "20.031437"; conf_hi "26.990083"; cov "84.378021";

The converted .BED file includes only one line as follows:

chr1    934796  935655  CUFF.29.3       24      -       934796  935655  0,0,255 2       16,750  0,109

The record in .BED file doesnot contain information like "frac", "conf_low", "conf_hi", "cov". But it uses only one line instead of 4 lines. Notice that the transcript_id is kept and the FPKM value (23.5) is rounded to 24 in score field of .BED file.

Feel free to comment or ask questions.


Bases in sequence positions

There are two different coordinate base systems in different files: 0-base and 1-base. Different files use different base systems, and sometimes it causes confusions (especially when one tries to calculate the length of the region). Here I show the differences in two systems, and summarize several file formats that use both sytems.

0-base system: the first base is 0. You represent a region as [a, b). This is also called "half-close-half-open", "0-base end exclusive", or "1-base end inclusive". When calculating the length of the region, subtract a from b directly:

L=b-a

1-base system: the first base is 1, and you represent a region as [a,b]. When calculating the length of the region, don't forget to add 1:

L=b-a+1

Here is an example. Suppose you want to represent a region of X in the following sequence:


000XXX0000


In the 0-base system, this is represented as [3,6), and the length is 6-3=3. In the 1-base system, use [4, 6], and the length is 6-4+1=3.


0-base system files: BED, BAM
1-base system files: SAM, GFF, GTF, Wig, PSL