Question: Extract the SAM File header section
0
gravatar for mhasa006
7 months ago by
mhasa00630
United States
mhasa00630 wrote:

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 bam java htsjdk • 268 views
ADD COMMENTlink modified 7 months ago by Pierre Lindenbaum117k • written 7 months ago by mhasa00630
2
gravatar for Pierre Lindenbaum
7 months ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum117k wrote:

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 COMMENTlink written 7 months ago by Pierre Lindenbaum117k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1374 users visited in the last hour