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
                  Best Practices for Single-Cell Sequencing Analysis
                  by seqadmin



                  While isolating and preparing single cells for sequencing was historically the bottleneck, recent technological advancements have shifted the challenge to data analysis. This highlights the rapidly evolving nature of single-cell sequencing. The inherent complexity of single-cell analysis has intensified with the surge in data volume and the incorporation of diverse and more complex datasets. This article explores the challenges in analysis, examines common pitfalls, offers...
                  06-06-2024, 07:15 AM
                • seqadmin
                  Latest Developments in Precision Medicine
                  by seqadmin



                  Technological advances have led to drastic improvements in the field of precision medicine, enabling more personalized approaches to treatment. This article explores four leading groups that are overcoming many of the challenges of genomic profiling and precision medicine through their innovative platforms and technologies.

                  Somatic Genomics
                  “We have such a tremendous amount of genetic diversity that exists within each of us, and not just between us as individuals,”...
                  05-24-2024, 01:16 PM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by seqadmin, Yesterday, 07:49 AM
                0 responses
                14 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 06-20-2024, 07:23 AM
                0 responses
                14 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 06-17-2024, 06:54 AM
                0 responses
                16 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 06-14-2024, 07:24 AM
                0 responses
                25 views
                0 likes
                Last Post seqadmin  
                Working...
                X