i have a file of 35k microarray rpboes aligned to a reference genome using STAR. It made a bam file containnig all the results. I want to know the gene_id on which the probes have aligned, how can i do that using bash or R
Assistant
User
i have a file of 35k microarray rpboes aligned to a reference genome using STAR. It made a bam file containnig all the results. I want to know the gene_id on which the probes have aligned, how can i do that using bash or R, i don't have bed file
Assistant
User
how to load makeblastdb and blastn modules
Assistant
makeblastdb and blastn are both part of the BLAST+ suite, so you load one package, not two modules.
On an HPC cluster (environment modules)
bash
module avail blast # list available versions (names vary: blast, blast+, ncbi-blast+)
module load blast+ # or e.g. module load ncbi-blast+/2.14.0
User
how to blast now that
i've installed dierectly without root
export PATH=PWD/ncbi−blast−2.17.0/bin:PATH
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You need a gene annotation (GTF/GFF) for the same genome build that the probes were aligned to, then you intersect the alignment coordinates with the gene coordinates. Here are three ways to do it.
-R CORE writes a per-read file (probes.bam.featureCounts) with the columns: probe name, status, number of targets, gene_id(s).
-M counts multi-mapping probes and -O allows assignment to overlapping genes.
-s 0 ignores strand. Use -s 1 or -s 2 if you care about probe orientation.
Add -t gene if you also want hits in introns, since probes can sit in introns or UTRs.
Add --fracOverlap 0.5 or similar if you want to require a minimum overlap.
Option 2: bedtools
bash
# keep only gene features
awk '$3=="gene"' annotation.gtf > genes.gtf
# BAM -> BED (use -split if alignments are spliced)
bedtools bamtobed -i probes.bam > probes.bed
# intersect
bedtools intersect -a probes.bed -b genes.gtf -wa -wb > probe_gene.txt
# extract probe ID and gene_id
awk -F'\t' '{match($15,/gene_id "[^"]+"/); g=substr($15,RSTART+9,RLENGTH-10); print $4"\t"g}' probe_gene.txt \
| sort -u > probe2gene.tsv
Add -s to the intersect if you want same-strand matches only. Add -f 0.9 to require 90% of the probe to overlap the gene.
Option 3: R (GenomicAlignments)
r
library(GenomicAlignments)
library(rtracklayer)
aln <- readGAlignments("probes.bam", use.names = TRUE,
param = ScanBamParam(tag = "NH"))
gr <- granges(aln)
gr$probe <- names(aln)
gr$NH <- mcols(aln)$NH # number of loci each probe aligned to
gtf <- import("annotation.gtf")
genes <- gtf[gtf$type == "gene"]
hits <- findOverlaps(gr, genes, ignore.strand = TRUE)
res <- data.frame(
probe = gr$probe[queryHits(hits)],
NH = gr$NH[queryHits(hits)],
gene_id = genes$gene_id[subjectHits(hits)],
gene_name = genes$gene_name[subjectHits(hits)] # if present in your GTF
)
# probes that hit no gene
unassigned <- setdiff(unique(gr$probe), res$probe)
Things to watch for
Chromosome names must match between the BAM and GTF (chr1 vs 1). Otherwise you'll get no overlaps.
STAR's defaults are tuned for long reads. Probes are 25–60 nt, so check that you relaxed --outFilterMatchNminOverLread, --outFilterScoreMinOverLread and --outFilterMultimapNmax. Otherwise many probes will be unmapped. Check with samtools flagstat probes.bam.
Multimappers: a probe can align to several loci, so filter on NH == 1 if you only want unique hits.
Mismatches: probes with several mismatches may still align, so consider filtering on the NM tag.
Annotation version: use the same GTF version (e.g. the GENCODE/Ensembl release) that the STAR index was built with.
If you tell me which organism and annotation you're using, I can adapt the commands, for example to map gene_id to gene symbols.
You don't need a BED file. The BAM already holds the probe coordinates, and bedtools or R can read it directly. What you need is a gene annotation file (GTF/GFF3) for the same genome build you aligned to.
Step 1: Get the GTF
If you built the STAR index with a GTF, it's probably still around. Check the Log.out of the genome generation, or genomeDir/genomeParameters.txt (look for sjdbGTFfile).
Otherwise, download the one matching your genome. For example, for human:
bash
# Ensembl (chromosomes named 1, 2, X ...)
wget https://ftp.ensembl.org/pub/release-110/gtf/homo_sapiens/Homo_sapiens.GRCh38.110.gtf.gz
# or GENCODE (chromosomes named chr1, chr2, chrX ...)
wget https://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_44/gencode.v44.annotation.gtf.gz
gunzip *.gtf.gz
The chromosome naming and the assembly version must match the reference you aligned to. Check with:
bash
samtools view -H probes.bam | grep '^@SQ' | head
grep -v '^#' annotation.gtf | cut -f1 | uniq | head
No overlaps at all: the chromosome names probably differ (chr1 vs 1), or the assembly versions differ.
Many probes unmapped: STAR's default filters are made for long reads. Rerun with relaxed settings such as --outFilterMatchNminOverLread 0.6 --outFilterScoreMinOverLread 0.6 --outFilterMultimapNmax 20.
Probes hitting several genes: these are multimappers or overlapping genes. Filter on the NH:i:1 tag if you want unique hits only.
If you tell me the organism and which genome files you used, I can give the exact download links and commands.
If module avail shows nothing, try a case-insensitive search:
bash
module -i avail blast
module spider blast # on Lmod systems
Check that it worked:
bash
which blastn makeblastdb
blastn -version
If your cluster uses Lmod and the module needs a compiler or other dependency first, module spider blast/<version> will tell you what to load beforehand.
Use -task blastn-short for probes under about 50 nt, because the default word size is too large for short sequences.
If you BLAST against a transcriptome (cDNA FASTA), the subject IDs are already transcript or gene IDs, so you don't need a GTF. This is often the easiest way to map microarray probes to genes.
Filter the output to full-length, near-perfect hits, e.g. length >= 0.9 * probe_length and mismatch <= 2.
On a cluster, run it through the scheduler (sbatch/qsub) rather than on the login node.
If module avail blast gives output, paste it here and I can tell you which one to load.
1. Check that it works
bash
blastn -version
makeblastdb -version
If you get "command not found", the PATH wasn't set properly. $PWD is wherever you were when you ran the command, so use the absolute path instead, and make it permanent:
The transcript ID in column 2 of the BLAST output should match the first field of tx2gene.tsv exactly, including the version suffix (.2). If the join gives blanks, check for this.
A probe that hits several genes appears on several lines. Count them with cut -f1 probe2gene.tsv | sort | uniq -c | awk '$1>1'. These are potentially non-specific probes.
If you need to see the best hit only, sort by bitscore and keep the first row per probe.
Tell me which organism and which files you're using, and I can adjust the header parsing.