Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • Guest
    Guest replied
    You can just replace "> ${filename}_chr.bam" with "> /new/path/${filename}_ch.bam".

    Leave a comment:


  • a_l_25
    replied
    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
    Guest replied
    The 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

    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
    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:
    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; done
    Last edited by Guest; 01-22-2015, 03:03 PM.

    Leave a comment:


  • neurongs
    replied
    thank you mra for this useful tool!

    Leave a comment:


  • mra
    replied
    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:


  • dpryan
    replied
    Originally posted by ddaneels View Post
    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...
    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:
    Code:
    samtools reheader header.sam sample1.bam > sample1.fixed.bam

    Leave a comment:


  • ddaneels
    replied
    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:


  • maubp
    replied
    OK - I may have been worrying over nothing then.

    Leave a comment:


  • dpryan
    replied
    Originally posted by maubp View Post
    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).

    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.
    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".
    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
    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
    So, it seems to work.

    Leave a comment:


  • maubp
    replied
    Originally posted by dpryan View Post
    Look 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.
    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).

    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:


  • maubp
    replied
    Originally posted by ffinkernagel View Post
    You are mistaken. The reads only contain a number that tells which entry from the header to pick.
    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.

    Leave a comment:


  • xied75
    replied
    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:


  • ddaneels
    replied
    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:


  • xied75
    replied
    Originally posted by ffinkernagel View Post
    You are mistaken. The reads only contain a number that tells which entry from the header to pick.
    In SAM spec v 1.4 document, column 3 RNAME is of String type, to hold Reference sequence NAME of the alignment.
    Last edited by xied75; 08-14-2012, 08:14 AM. Reason: My mistake.

    Leave a comment:


  • dpryan
    replied
    Originally posted by ddaneels View Post
    That 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?
    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.

    Leave a comment:

Latest Articles

Collapse

  • SEQadmin2
    16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
    by SEQadmin2




    NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification, detection...
    Today, 06:46 AM
  • SEQadmin2
    Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
    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
    ...
    10-14-2026, 09:55 AM

ad_right_rmr

Collapse

News

Collapse

Topics Statistics Last Post
Started by SEQadmin2, 10-28-2026, 09:57 AM
0 responses
22 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 10-28-2026, 09:51 AM
0 responses
14 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 10-22-2026, 11:13 AM
0 responses
67 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 10-22-2026, 10:48 AM
0 responses
47 views
0 reactions
Last Post SEQadmin2  
Working...