Unconfigured Ad
Collapse
X
-
Guest repliedYou can just replace "> ${filename}_chr.bam" with "> /new/path/${filename}_ch.bam".
-
stack in the output part
how to put the filename variable in different path than the original path where bam files located. Hence , keeping the variable $filename as part of the every new bam file's name.
Leave a comment:
-
Guest repliedThe following line works well for me. It will replace the names of chromosome 1 to 22, X, Y and MT (mitochondrial DNA). It will create a new file with the name test_chr.bam instead of test.bam
If you want to do this for a bunch of bam files at once, use a loop. The variable $filename is your file without the extension (assuming there is no period except the one in ".bam").Code:use Samtools samtools view -H test.bam | sed -e 's/SN:\([0-9XY]\)/SN:chr\1/' -e 's/SN:MT/SN:chrM/' | samtools reheader - test.bam > test_chr.bam
Code:for file in *.bam; do filename=`echo $file | cut -d "." -f 1`; samtools view -H $file | sed -e 's/SN:\([0-9XY]\)/SN:chr\1/' -e 's/SN:MT/SN:chrM/' | samtools reheader - $file > ${filename}_chr.bam; doneLast edited by Guest; 01-22-2015, 03:03 PM.
Leave a comment:
-
BAM to BAM plus "chr"
The chromosome names can be changed on the fly. This command takes an input BAM file with chr names '1, 2, 3,..,X, Y, MT' and output a BAM file with names 'chr1, chr2, chr3,..., chrX, chrY, chrM' on both read and header lines. The output BAM doesn't include any non-canonical chromosome that could be present in the input BAM (such as JG*, Un*, etc).
Code:samtools view -h INPUT.bam | awk 'BEGIN{FS=OFS="\t"} (/^@/ && !/@SQ/){print $0} $2~/^SN:[1-9]|^SN:X|^SN:Y|^SN:MT/{print $0} $3~/^[1-9]|X|Y|MT/{$3="chr"$3; print $0} ' | sed 's/SN:/SN:chr/g' | sed 's/chrMT/chrM/g' | samtools view -bS - > OUTPUT.bam
Leave a comment:
-
The reheader command doesn't change the original file (I'm not surprised you thought it would, the name lends itself to that belief and I don't think the documentation mentions this). You'll need to output things to a file rather than to samtools view. Try something like:Originally posted by ddaneels View Postmy header.sam file looks OK. "chr" has been added.
But when I run the samtools reheader command, nothing changes in the original .bam file...
Code:samtools reheader header.sam sample1.bam > sample1.fixed.bam
Leave a comment:
-
my header.sam file looks OK. "chr" has been added.
But when I run the samtools reheader command, nothing changes in the original .bam file...
Code:
samtools reheader header.sam sample1.bam | samtools view -H sample1.bam
Sample1.bam is my original file, so I was hoping that the header in sample1.bam would have changed, but it didn't.
I'm a programming newbie ... so maybe there's a mistake in my code?
Leave a comment:
-
Actually, bam_reheader runs the full bam_header_write using only the new header, so it seems it does both (I haven't bothered looking into the source of bam_header_write, I should note). I decided to run a quick test, since I can't say I've ever actually run the reheader command. For that, I took the header of a sorted alignment (written to a file called header.sam), and changed "chr1" to "chr100".Originally posted by maubp View PostI don't think that would work. Using 'samtools reheader' would only edit the embedded SAM header embedded in a BAM file, it would not IIRC update the separate BAM specific header table containing the list of references (their names and references).
You could turn the BAM file into SAM (e.g. with samtools view -h), do the replacement (e.g. with sed), and then optionally convert back to BAM (again with samtools view). That can be done as one line by piping the output from one tool to the next.
Code:samtools view accepted_hits.bam | head -n 2 HWI-ST143:530:C102UACXX:5:1101:3568:162900 272 chr1 3005607 0 51M * 0 0 CATAAATTCATTTTTTAATAGCTGAGTAGTATTCCATTGTGTAAATGTACC * AS:i:0 XN:i:0 XM:i:0 XO:i:0 XG:i:0 NM:i:0 MD:Z:51 YT:Z:UU NH:i:20 CC:Z:= CP:i:105668244 HI:i:0 HWI-ST143:530:C102UACXX:5:1308:5464:137163 272 chr1 3006556 0 51M * 0 0 TTAGCTCCCTTGTCAAAGATCAGGTGACCATAGGTGTGTGGATTCATCTCT * AS:i:0 XN:i:0 XM:i:0 XO:i:0 XG:i:0 NM:i:0 MD:Z:51 YT:Z:UU NH:i:20 CC:Z:chr15 CP:i:16439024 HI:i:0
So, it seems to work.Code:samtools reheader header.sam accepted_hits.bam | samtools view - | head -n 2 HWI-ST143:530:C102UACXX:5:1101:3568:162900 272 chr100 3005607 0 51M * 0 0 CATAAATTCATTTTTTAATAGCTGAGTAGTATTCCATTGTGTAAATGTACC * AS:i:0 XN:i:0 XM:i:0 XO:i:0 XG:i:0 NM:i:0 MD:Z:51 YT:Z:UU NH:i:20 CC:Z:= CP:i:105668244 HI:i:0 HWI-ST143:530:C102UACXX:5:1308:5464:137163 272 chr100 3006556 0 51M * 0 0 TTAGCTCCCTTGTCAAAGATCAGGTGACCATAGGTGTGTGGATTCATCTCT * AS:i:0 XN:i:0 XM:i:0 XO:i:0 XG:i:0 NM:i:0 MD:Z:51 YT:Z:UU NH:i:20 CC:Z:chr15 CP:i:16439024 HI:i:0
Leave a comment:
-
I don't think that would work. Using 'samtools reheader' would only edit the embedded SAM header embedded in a BAM file, it would not IIRC update the separate BAM specific header table containing the list of references (their names and references).Originally posted by dpryan View PostLook at the samtools reheader command. Presumably you would just "samtools view -H file.bam > header.sam", edit the header, and then use that with reheader.
You could turn the BAM file into SAM (e.g. with samtools view -h), do the replacement (e.g. with sed), and then optionally convert back to BAM (again with samtools view). That can be done as one line by piping the output from one tool to the next.
Leave a comment:
-
Or to be more precise, in BAM the reads just store an integer to say which reference it mapped to (referencing a table at the start of the BAM file, which is separate to any embedded SAM header), but in SAM the reads store the reference sequence's name.Originally posted by ffinkernagel View PostYou are mistaken. The reads only contain a number that tells which entry from the header to pick.
Leave a comment:
-
1, This Big Huge Black 1, is column 3, not 7. It is not a number, but a string.
2, You don't need to change this 1 to chr1, is because programs like BWA and Samtools will read both format; but it doesn't mean this is a number ref to the header lines. Your understanding is more correct.
Leave a comment:
-
Thanks for the info.
Everything will work fine then. I was confused with the "1" in the 7th column of the read-part in the .bam file.
Example:
HWI-ST571_103:4:1302:9610:62449 99 1 604269 254 100M = 60324 152 GGAA...
I thought that the highlighted 1 also had to be changed to chr1.
Leave a comment:
-
In SAM spec v 1.4 document, column 3 RNAME is of String type, to hold Reference sequence NAME of the alignment.Originally posted by ffinkernagel View PostYou are mistaken. The reads only contain a number that tells which entry from the header to pick.
Leave a comment:
-
As was mentioned by ffinkernagel, this is incorrect. I should note that you should avoid swapping the order of chromosomes or any other major edits. Just adding or removing "chr" won't break anything, but changing the order of things in the header or removing chromosomes could cause issues.Originally posted by ddaneels View PostThat would be a way to change the header, but if I'm not mistaking the chromosome numbers are also in the actual read-part of the file. So how to change them there?
Leave a comment:
Latest Articles
Collapse
-
by SEQadmin2
Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?
Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating...-
Channel: Articles
10-14-2026, 09:55 AM -
ad_right_rmr
Collapse
News
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Genome Sequencing Reveals Large, Repeat-Rich DNA in Trapdoor Spider Named for Stephen Colbert
by SEQadmin2
Started by SEQadmin2, Yesterday, 09:57 AM
|
0 responses
19 views
0 reactions
|
Last Post
by SEQadmin2
Yesterday, 09:57 AM
|
||
|
Started by SEQadmin2, Yesterday, 09:51 AM
|
0 responses
11 views
0 reactions
|
Last Post
by SEQadmin2
Yesterday, 09:51 AM
|
||
|
Started by SEQadmin2, 10-22-2026, 11:13 AM
|
0 responses
67 views
0 reactions
|
Last Post
by SEQadmin2
10-22-2026, 11:13 AM
|
||
|
Started by SEQadmin2, 10-22-2026, 10:48 AM
|
0 responses
47 views
0 reactions
|
Last Post
by SEQadmin2
10-22-2026, 10:48 AM
|
Leave a comment: