Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • dvanic
    Member
    • Jan 2012
    • 61

    HTSeq-count on bam

    Hi!
    Is there a way to directly run htseq-count on a bam file? I know that htseq supports bam input via HTSeq.BAM_Reader(), but is there a flag to tell the htseq-count script that I'm using a bam?

    Or which file in the source code (and how) do I modify to configure it to work with bams and not sams???

    Thanks in advance!
  • hlwright
    Member
    • Feb 2011
    • 30

    #2
    Hi

    I have EXACTLY the same question. Does htseq take BAM files, or do you have to convert to SAM first (don't want to do this if I don't have to)?

    Thanks

    Comment

    • dpryan
      Devon Ryan
      • Jul 2011
      • 3478

      #3
      If you're using the command line version, you can just use:
      Code:
      samtools view file.bam | htseq-count (options) - GTF > counts.txt
      where GTF is the GTF file and (options) are whatever options are appropriate.

      Comment

      • hlwright
        Member
        • Feb 2011
        • 30

        #4
        dpryan - that worked brilliantly, thanks!

        )

        Comment

        • astazangasta
          Junior Member
          • Jul 2013
          • 1

          #5
          counting reads

          If anyone else is trying this, be advised that HTseq count requires reads to be name-sorted. If you usually keep your bam coordinate-sorted, like I do, and you have paired-end reads, you need to do a bit more work to get it in the right shape, otherwise HTseq will end up skipping huge numbers of reads. I do:

          Code:
          samtools sort -on - output | samtools view -h - | htseq_count etc.

          Comment

          • Him26
            Member
            • Aug 2011
            • 20

            #6
            Code:
            samtools sort -on - output | samtools view -h - | htseq_count etc.
            Could you expand on how you pipe-in to htseq-count?
            I get an error saying I need to provide two arguments to htseq-count.
            I just left it blank on the htseq-count commend assuming that it will know.
            my code is like below:

            Code:
             | htseq-count -m{option}  annotation.gtf >output.count
            Thank you.
            Last edited by Him26; 11-08-2013, 05:50 PM.

            Comment

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

              #7
              Code:
              samtools sort -on file.bam | samtools view - | htseq-count (options) - GTF > counts.txt
              Or something like that. The "-" means read from the pipe/standard input in both samtools and htseq-count.

              Comment

              • Him26
                Member
                • Aug 2011
                • 20

                #8
                Thank you dpryan for the quick reply.
                I tried the following code and I get the following error? any suggestions?

                Code:
                 samtools sort -on -m 300000000 accepted_hits.bam |samtools view -h - |htseq-count -m intersection-strict -  /genome/gencode.v18.annotation.gtf > hits.count
                Usage: samtools sort [options] <in.bam> <out.prefix>

                Options: -n sort by read name
                -f use <out.prefix> as full file name instead of prefix
                -o final output to stdout
                -l INT compression level, from 0 to 9 [-1]
                -@ INT number of sorting and compression threads [1]
                -m INT max memory per thread; suffix K/M/G recognized [768M]

                [bam_header_read] EOF marker is absent. The input is probably truncated.
                [bam_header_read] invalid BAM binary header (this is not a BAM file).
                [main_samview] fail to read the header from "-".
                100000 GFF lines processed.
                200000 GFF lines processed.
                300000 GFF lines processed.
                400000 GFF lines processed.
                500000 GFF lines processed.
                600000 GFF lines processed.
                700000 GFF lines processed.
                800000 GFF lines processed.
                900000 GFF lines processed.
                1000000 GFF lines processed.
                1100000 GFF lines processed.
                1200000 GFF lines processed.
                1300000 GFF lines processed.
                1400000 GFF lines processed.
                1500000 GFF lines processed.
                1600000 GFF lines processed.
                1700000 GFF lines processed.
                1800000 GFF lines processed.
                1900000 GFF lines processed.
                2000000 GFF lines processed.
                2100000 GFF lines processed.
                2200000 GFF lines processed.
                2300000 GFF lines processed.
                2400000 GFF lines processed.
                2500000 GFF lines processed.
                2600000 GFF lines processed.
                2614562 GFF lines processed.
                Error occured when reading first line of sam file.

                [Exception type: StopIteration, raised in count.py:83]
                Last edited by Him26; 11-09-2013, 06:23 AM. Reason: more information

                Comment

                • bruce01
                  Senior Member
                  • Mar 2011
                  • 160

                  #9
                  Originally posted by Him26 View Post
                  [bam_header_read] EOF marker is absent. The input is probably truncated.
                  [bam_header_read] invalid BAM binary header (this is not a BAM file).
                  [main_samview] fail to read the header from "-".
                  So your BAM is not complete, try realigning, or use a different BAM.

                  Comment

                  • dpryan
                    Devon Ryan
                    • Jul 2011
                    • 3478

                    #10
                    You may also just see if the following produces the same result:

                    Code:
                    samtools sort -n -m 300000000 accepted_hits.bam namesorted
                    If so, then bruce01 is correct that your original BAM file is truncated. It's unclear which of the samtools steps is producing the fatal error and this would determine which it is (I too suspect that the original BAM file is just truncated).

                    Comment

                    • flek
                      Junior Member
                      • Mar 2014
                      • 1

                      #11
                      Originally posted by astazangasta View Post
                      If anyone else is trying this, be advised that HTseq count requires reads to be name-sorted. If you usually keep your bam coordinate-sorted, like I do, and you have paired-end reads, you need to do a bit more work to get it in the right shape, otherwise HTseq will end up skipping huge numbers of reads. I do:

                      Code:
                      samtools sort -on - output | samtools view -h - | htseq_count etc.
                      I just came across the same problem and while browsing the htseq documentation I found out that there is a new option in version 0.6.1 that allows to read bam files which are sorted by position.

                      Code:
                      htseq_count -r pos etc.
                      More at their documentation

                      Comment

                      • dstorey
                        Junior Member
                        • Feb 2013
                        • 5

                        #12
                        It unfortunately has a well known bug that causes the memory buffer to overflow sometimes. Named sort appears to be the most consistent method for getting a run completed.

                        Comment

                        • tsalem
                          Junior Member
                          • Nov 2014
                          • 5

                          #13
                          Now, I think htseq can take directly the bam file, we only need to use the -f option as follows


                          htseq-count -f bam <file>.bam <file>.gtf > genes_count.txt


                          is i am Wrong? I tried it and it works

                          Comment

                          • GenoMax
                            Senior Member
                            • Feb 2008
                            • 7142

                            #14
                            Originally posted by tsalem
                            after i run the command
                            htseq-count -f bam <file>.bam <file>.gtf > genes_count.txt

                            i got the following error,


                            Warning: Read UNC13-SN749_48:5:2105:5602:87765/2 claims to have an aligned mate which could not be found in an adjacent line.
                            Error occured when processing SAM input (record #40 in file sorted_genome_alignments.bam):
                            tid -1 out of range 0<=tid<25
                            [Exception type: ValueError, raised in csamfile.pyx:535]

                            is any one have an idea why this error? Thanks in advance
                            When you did QC/trimming of the data was a PE-read aware program used (to keep the pairs of reads in sync in both files) with the two data files at the same time (R1/R2)? It is possible that reads in your files went out of sync and this is resulting in the error above.

                            Comment

                            • tsalem
                              Junior Member
                              • Nov 2014
                              • 5

                              #15
                              Actually, I didn't do any thing with the files, I just got the .gtf file from The ucsc.
                              and the .bam file from my advisor. and i am trying to get genes count from .bam file.

                              is possible to let me know how i can check if the pairs of reads in sync in both files

                              Thanks

                              Comment

                              Latest Articles

                              Collapse

                              • SEQadmin2
                                Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                                by SEQadmin2


                                I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.


                                Here are nine questions we think about, in roughly the order they matter, before...
                                Yesterday, 07:11 AM
                              • SEQadmin2
                                From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                                by SEQadmin2


                                Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                                The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                                ...
                                06-02-2026, 10:05 AM
                              • SEQadmin2
                                Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
                                by SEQadmin2


                                With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


                                Introduction

                                Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
                                05-22-2026, 06:42 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by SEQadmin2, 06-17-2026, 06:09 AM
                              0 responses
                              20 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-09-2026, 11:58 AM
                              0 responses
                              38 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-05-2026, 10:09 AM
                              0 responses
                              44 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 06-04-2026, 08:59 AM
                              0 responses
                              49 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...