Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Separate bam file

    Hi NGS users,
    anyone knows how I can separate a BAM file in different chromosome?

    Starting BAM file is too large (about 10GB for a whole exome) so I want to divide it in different smaller BAM file for chromosome. But using:

    > samtools view *sorted.bam | awk '$3=="chr1"' > onlychr1.bam.sorted.bam

    returns me an incorrect file.
    Infact, when I run
    > samtools index onlychr1.bam.sorted.bam

    it returns me
    [bam_header_read] EOF marker is absent.
    [bam_header_read] invalid BAM binary header (this is not a BAM file).
    Segmentation fault

    Thanx
    ME

  • #2
    You are trying to index a SAM file. You need to change your command to create BAM as follows. Note that you need headers as well.

    samtools view -h *sorted.bam | awk '$3=="chr1" || /^@/' | samtools view -Sb -> onlychr1.bam.sorted.bam

    Aaron

    Comment


    • #3
      It's perfect!
      Thank you very much Aaron!!

      Comment


      • #4
        A problem!
        I aligned my reads vs hg19.fasta (including sequence like chr9_gl000201_random, chrUn_gl000235....).
        If I want to split bwa in smaller files (with alignment in two or more chromosome), using Aaron advise:

        > samtools view -h *sorted.bam | awk '$3=="chr1" && $3=="chr3" || /^@/' | samtools view -Sb -> onlychr1_3.bam.sorted.bam

        the program returns me this error:
        [sam_read1] reference 'SN:chr18_gl000207_random LN:4262 ' is recognized as '*'. [main_samview] truncated file.


        If I split in single chromosome (only with $3=="chr1"), I have no problem.

        Someone can help me?

        Thanxxx!!!
        ME

        Comment


        • #5
          I am not an 'awk' expert but I suspect that searching for both $3 being chr1 and $3 being chr3 will indeed give a truncated file -- perhaps not zero length but I suspect that it would not contain much useful information.

          E.g., the
          '$3=="chr1" && $3=="chr3"
          Looks bad to me. I would put parenthesis around the parts to group together.

          But caution is due to my non-awk experience.
          Last edited by westerman; 09-21-2010, 11:44 AM.

          Comment


          • #6
            You're right westerman, I'm a beginner with awk. But I can't find a system to define the search for both (or more) chromosomes in bam file...

            Comment


            • #7
              To find chr1 or chr3 do the following

              Code:
              samtools view -h *sorted.bam | awk '$3=="chr1" || $3=="chr3" || /^@/' | samtools view -Sb -> onlychr1_3.bam.sorted.bam
              To find chr1 or chr3 or chr21 do the following

              Code:
              samtools view -h *sorted.bam | awk '$3=="chr1" || $3=="chr3" || $3=="chr21" ||  /^@/' | samtools view -Sb -> onlychr1_3_21.bam.sorted.bam

              Comment


              • #8
                Originally posted by quinlana View Post
                You are trying to index a SAM file. You need to change your command to create BAM as follows. Note that you need headers as well.

                samtools view -h *sorted.bam | awk '$3=="chr1" || /^@/' | samtools view -Sb -> onlychr1.bam.sorted.bam

                Aaron
                I am new to this field but the following worked for me (please let me know if I am wrong):
                samtools sort file.bam file.sorted.bam
                samtools index file.sorted.bam
                samtools view -bh file.sorted.bam chr1 > chr1.file.bam

                Comment

                Latest Articles

                Collapse

                • seqadmin
                  Advancing Precision Medicine for Rare Diseases in Children
                  by seqadmin




                  Many organizations study rare diseases, but few have a mission as impactful as Rady Children’s Institute for Genomic Medicine (RCIGM). “We are all about changing outcomes for children,” explained Dr. Stephen Kingsmore, President and CEO of the group. The institute’s initial goal was to provide rapid diagnoses for critically ill children and shorten their diagnostic odyssey, a term used to describe the long and arduous process it takes patients to obtain an accurate...
                  12-16-2024, 07:57 AM
                • seqadmin
                  Recent Advances in Sequencing Technologies
                  by seqadmin



                  Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

                  Long-Read Sequencing
                  Long-read sequencing has seen remarkable advancements,...
                  12-02-2024, 01:49 PM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by seqadmin, 12-17-2024, 10:28 AM
                0 responses
                33 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 12-13-2024, 08:24 AM
                0 responses
                49 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 12-12-2024, 07:41 AM
                0 responses
                34 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 12-11-2024, 07:45 AM
                0 responses
                46 views
                0 likes
                Last Post seqadmin  
                Working...
                X