Skip to content

Document how to get hg38 12-column BED file from UCSC #15

Description

@sreichl
  • consider generating it from GFF/GTF file or similar that was already used in an upstream module e.g. rnaseq-pipeline or atacseq_pipline ie something simpler and potentially more correct as it comes from the same source/version as the read mapping ( or that can be easily retrieved.). (Generate 12-column BED file based on GFF from RNA pipeline. )

Instructions to get hg38 12-column BED file from UCSC

Okay, here are the complete step-by-step instructions to download the NCBI RefSeq annotations for the hg38 genome assembly from UCSC and generate a 12-column, gzipped BED file (.bed.gz) where the 4th column contains the gene symbol.

This process involves downloading the data in genePred format, rearranging columns using awk so the gene symbol is first, and then converting to BED format using the genePredToBed utility.

  • Prerequisites:*
  • Web browser
  • Command-line terminal (like Terminal on macOS/Linux, or WSL/Git Bash/Cygwin on Windows)
  • Command-line tools: gunzip, gzip, awk (usually pre-installed on Linux/macOS)
  • The genePredToBed utility from UCSC (you'll download this if you don't have it).
  • Instructions:*
  1. Go to the UCSC Table Browser:
    Open your web browser and navigate to: https://genome.ucsc.edu/cgi-bin/hgTables

  2. Set Genome and Assembly:

    • clade: Mammal
    • genome: Human
    • assembly: Dec. 2013 (GRCh38/hg38)
  3. Select Track and Table:

    • group: Genes and Gene Predictions
    • track: NCBI RefSeq
    • table: ncbiRefSeq
  4. Choose Output Format:

    • output format: Select all fields from selected table (This will output data in a format compatible with genePred).
  5. Specify Output File:

    • In the output file field, enter a filename. Let's use: hg38_refseq_raw.gp.gz
    • Output field separator: tsv (tab-separated)
    • Make sure the gzip compressed checkbox is checked.
  6. Download the Data:

    • Click the get output button. The file hg38_refseq_raw.gp.gz will be downloaded.
  7. Open Your Command-Line Terminal:

    • Navigate to the directory where you downloaded the file.
  8. Unzip the Downloaded File:

    gunzip hg38_refseq_raw.gp.gz
    • You should now have a file named hg38_refseq_raw.gp.
  9. Verify Column Order (Important!):

    • The ncbiRefSeq table usually has the transcript ID (e.g., NM_...) in column 1 and the gene symbol (e.g., TP53) in column 12. Let's check the first line to confirm:
      head -n 1 hg38_refseq_raw.gp | awk '{print "Col2:", $2, " Col13:", $13}'
    • Confirm that Column 2 looks like a transcript ID and Column 13 looks like a gene symbol. If they are in different columns, adjust the column numbers ($2, $13) in the next step accordingly.
  10. Swap Columns to Put Gene Symbol First and remove header:

    • Use awk to create a new file where the original 13th column (gene symbol) is moved to the 2nd column, and the original 2nd column (transcript ID) is moved to the 13th column.

awk 'BEGIN{FS=OFS="\t"} /^#/ {next} {print $13, $3, $4, $5, $6, $7, $8, $9, $10, $11}' hg38_refseq_raw.gp > hg38_refseq_genePred_for_conversion.gp
```
* This reads hg38_refseq_raw.gp, swaps columns 2 and 13 for each line using a tab (`\t`) as the separator, and writes the result to `hg38_refseq_swapped_noheader.gp`.

  1. Get the genePredToBed Utility:
    • If you don't already have the UCSC Kent command-line utilities installed:
      • Go to: http://hgdownload.soe.ucsc.edu/admin/exe/
      • Navigate into the directory corresponding to your operating system (e.g., linux.x86_64/, macOSX.x86_64/).
      • Download the genePredToBed executable file.
      • Make the file executable: chmod +x genePredToBed
      • You can either place this file in your current directory (and run it using ./genePredToBed) or move it to a directory in your system's PATH.

or using conda

  • conda create -n genepredtobed
  • conda install bioconda::ucsc-genepredtobed
  1. Create the Correctly Formatted Input for genePredToBed:
    • Run this awk command. It selects the necessary columns from hg38_refseq_raw.gp and outputs them in the order genePredToBed expects, with the gene symbol ($2 from the original file) as the first column.
    awk 'BEGIN{FS=OFS="\t"} /^#/ {next} {print $13, $3, $4, $5, $6, $7, $8, $9, $10, $11}' hg38_refseq_raw.gp > hg38_refseq_genePred_for_conversion.gp
    • Explanation:
      • BEGIN{FS=OFS="\t"}: Use tabs as separators.
      • /^#/ {next}: Skip the header line.
      • {print $13, $3, $4, $5, $6, $7, $8, $9, $10, $11}: Print the original columns:
        • $13 (name2 -> gene symbol) -> becomes input col 1 (for BED name)
        • $3 (chrom) -> becomes input col 2 (for BED chrom)
        • $4 (strand) -> becomes input col 3 (for BED strand)
        • $5 (txStart) -> becomes input col 4 (for BED chromStart)
        • $6 (txEnd) -> becomes input col 5 (for BED chromEnd)
        • $7 (cdsStart) -> becomes input col 6 (for BED thickStart)
        • $8 (cdsEnd) -> becomes input col 7 (for BED thickEnd)
        • $9 (exonCount) -> becomes input col 8 (for BED blockCount)
        • $10 (exonStarts) -> becomes input col 9 (for BED blockStarts calculation)
        • $11 (exonEnds) -> becomes input col 10 (for BED blockSizes calculation)
    • This creates a new file hg38_refseq_genePred_for_conversion.gp specifically formatted for genePredToBed.

Context: genePredToBed expects its input file to have a specific 10-column structure derived from the standard genePred format:

  1. name (This will become the 4th column / name field in the output BED file)
  2. chrom
  3. strand
  4. txStart
  5. txEnd
  6. cdsStart
  7. cdsEnd
  8. exonCount
  9. exonStarts (comma-separated list)
  10. exonEnds (comma-separated list)

Your current hg38_refseq_swapped_noheader.gp file starts with 0 XM_011541469.2 chr1 - ..., which doesn't match this required input structure. The 0 is the bin field, and the XM_... is the transcript ID. Crucially, the first column is not the gene symbol, which is why the previous attempt failed and why genePredToBed is likely encountering unexpected data (like the - for strand in a position where it might expect a coordinate if the columns were misinterpreted).

  1. Run genePredToBed:

    • Now, run the conversion tool on this new, correctly formatted file:
    ./genePredToBed hg38_refseq_genePred_for_conversion.gp hg38_refseq_geneSymbol.bed
    • This should now correctly interpret the columns and generate the BED12 file. The first column of the input ($13 from the original file, the gene symbol) will be placed into the 4th column (name field) of hg38_refseq_geneSymbol.bed.
  2. Compress the Final BED File:

    gzip hg38_refseq_geneSymbol.bed

Result:*

You will now have a file named hg38_refseq_geneSymbol.bed.gz. This is the 12-column BED annotation file for hg38 NCBI RefSeq, compressed with gzip, and with the gene symbol correctly placed in the 4th column.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentation

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions