Extract the SAM File header section
1
0
Entering edit mode
5.8 years ago
mhasa006 ▴ 70

How can I extract the header section of the SAM File with htsjdk package? Below is an example of a SAM File

@HD     VN:1.3  SO:coordinate
@SQ     SN:chr1 LN:248956422
@SQ     SN:chr10        LN:133797422
@SQ     SN:chr11        LN:135086622
XXX:111:312:1312     99      chr1    10349   1       56M44S  =       10349   56      CCCTAACCTAACCCTAACCCTAA EEEEEEEEEEEEEEEEEEEEEEEE
XXX:111:312:1312     99      chr1    10349   1       56M44S  =       10349   56      CCCTAACCTAACCCTAACCCTAA EEEEEEEEEEEEEEEEEEEEEEEE

I want to extract only

    @HD     VN:1.3  SO:coordinate
    @SQ     SN:chr1 LN:248956422
    @SQ     SN:chr10        LN:133797422
    @SQ     SN:chr11        LN:135086622

How to do that with htsjdk?

SAM samtools htsjdk bam java • 1.8k views
ADD COMMENT
2
Entering edit mode
5.8 years ago

a minimal code. Don't do this in production (static method, streams are not closed,...)

import htsjdk.samtools.*;
import java.io.*;

class Biostar327241 {
public static void main(final String args[]) throws Exception {
 new SAMTextHeaderCodec().encode(
    new PrintWriter(System.out),
    SamReaderFactory.makeDefault().open(new File(args[0])).getFileHeader(),
    true
    );
  }
}

compile:

javac -cp "/path/to/com/github/samtools/htsjdk/2.15.0/htsjdk-2.15.0.jar:/path/to/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/path/to/gov/nih/nlm/ncbi/ngs-java/1.2.4/ngs-java-1.2.4.jar:/path/to/org/apache/commons/commons-compress/1.4.1/commons-compress-1.4.1.jar:/path/to/org/apache/commons/commons-jexl/2.1.1/commons-jexl-2.1.1.jar:/path/to/org/tukaani/xz/1.5/xz-1.5.jar:/path/to/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar"  Biostar327241.java

execute (don't forget the '.' alone at the end of the classpath):

java -cp "/path/to/com/github/samtools/htsjdk/2.15.0/htsjdk-2.15.0.jar:/path/to/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/path/to/gov/nih/nlm/ncbi/ngs-java/1.2.4/ngs-java-1.2.4.jar:/path/to/org/apache/commons/commons-compress/1.4.1/commons-compress-1.4.1.jar:/path/to/org/apache/commons/commons-jexl/2.1.1/commons-jexl-2.1.1.jar:/path/to/org/tukaani/xz/1.5/xz-1.5.jar:/path/to/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:."  Biostar327241 S1.bam


@HD VN:1.3  SO:coordinate
@SQ SN:RF01 LN:3302
@SQ SN:RF02 LN:2687
@SQ SN:RF03 LN:2592
@SQ SN:RF04 LN:2362
@SQ SN:RF05 LN:1579
@SQ SN:RF06 LN:1356
@SQ SN:RF07 LN:1074
@SQ SN:RF08 LN:1059
@SQ SN:RF09 LN:1062
@SQ SN:RF10 LN:751
@SQ SN:RF11 LN:666
@RG ID:S1   SM:S1   LB:L1   CN:Nantes
ADD COMMENT

Login before adding your answer.

Traffic: 2680 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6