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).
-
Go to the UCSC Table Browser:
Open your web browser and navigate to: https://genome.ucsc.edu/cgi-bin/hgTables
-
Set Genome and Assembly:
- clade: Mammal
- genome: Human
- assembly: Dec. 2013 (GRCh38/hg38)
-
Select Track and Table:
- group: Genes and Gene Predictions
- track: NCBI RefSeq
- table:
ncbiRefSeq
-
Choose Output Format:
- output format: Select
all fields from selected table (This will output data in a format compatible with genePred).
-
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.
-
Download the Data:
- Click the get output button. The file
hg38_refseq_raw.gp.gz will be downloaded.
-
Open Your Command-Line Terminal:
- Navigate to the directory where you downloaded the file.
-
Unzip the Downloaded File:
gunzip hg38_refseq_raw.gp.gz
- You should now have a file named
hg38_refseq_raw.gp.
-
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.
-
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`.
- 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
- 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:
name (This will become the 4th column / name field in the output BED file)
chrom
strand
txStart
txEnd
cdsStart
cdsEnd
exonCount
exonStarts (comma-separated list)
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).
-
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.
-
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.
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
genePredformat, rearranging columns usingawkso the gene symbol is first, and then converting to BED format using thegenePredToBedutility.gunzip,gzip,awk(usually pre-installed on Linux/macOS)genePredToBedutility from UCSC (you'll download this if you don't have it).Go to the UCSC Table Browser:
Open your web browser and navigate to: https://genome.ucsc.edu/cgi-bin/hgTables
Set Genome and Assembly:
Select Track and Table:
ncbiRefSeqChoose Output Format:
all fields from selected table(This will output data in a format compatible withgenePred).Specify Output File:
hg38_refseq_raw.gp.gzDownload the Data:
hg38_refseq_raw.gp.gzwill be downloaded.Open Your Command-Line Terminal:
Unzip the Downloaded File:
hg38_refseq_raw.gp.Verify Column Order (Important!):
ncbiRefSeqtable 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:$2,$13) in the next step accordingly.Swap Columns to Put Gene Symbol First and remove header:
awkto 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`.genePredToBedUtility:linux.x86_64/,macOSX.x86_64/).genePredToBedexecutable file.chmod +x genePredToBed./genePredToBed) or move it to a directory in your system's PATH.or using conda
genePredToBed:awkcommand. It selects the necessary columns fromhg38_refseq_raw.gpand outputs them in the ordergenePredToBedexpects, with the gene symbol ($2from the original file) as the first column.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)hg38_refseq_genePred_for_conversion.gpspecifically formatted forgenePredToBed.Context:
genePredToBedexpects its input file to have a specific 10-column structure derived from the standardgenePredformat:name(This will become the 4th column / name field in the output BED file)chromstrandtxStarttxEndcdsStartcdsEndexonCountexonStarts(comma-separated list)exonEnds(comma-separated list)Your current
hg38_refseq_swapped_noheader.gpfile starts with0 XM_011541469.2 chr1 - ..., which doesn't match this required input structure. The0is thebinfield, and theXM_...is the transcript ID. Crucially, the first column is not the gene symbol, which is why the previous attempt failed and whygenePredToBedis likely encountering unexpected data (like the-for strand in a position where it might expect a coordinate if the columns were misinterpreted).Run
genePredToBed:$13from the original file, the gene symbol) will be placed into the 4th column (name field) ofhg38_refseq_geneSymbol.bed.Compress the Final BED File:
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.