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
                  Recent Advances in Sequencing Analysis Tools
                  by seqadmin


                  The sequencing world is rapidly changing due to declining costs, enhanced accuracies, and the advent of newer, cutting-edge instruments. Equally important to these developments are improvements in sequencing analysis, a process that converts vast amounts of raw data into a comprehensible and meaningful form. This complex task requires expertise and the right analysis tools. In this article, we highlight the progress and innovation in sequencing analysis by reviewing several of the...
                  05-06-2024, 07:48 AM
                • seqadmin
                  Essential Discoveries and Tools in Epitranscriptomics
                  by seqadmin




                  The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
                  04-22-2024, 07:01 AM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by seqadmin, 05-07-2024, 06:57 AM
                0 responses
                12 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 05-06-2024, 07:17 AM
                0 responses
                16 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 05-02-2024, 08:06 AM
                0 responses
                22 views
                0 likes
                Last Post seqadmin  
                Started by seqadmin, 04-30-2024, 12:17 PM
                0 responses
                24 views
                0 likes
                Last Post seqadmin  
                Working...
                X