Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • cmccabe
    Senior Member
    • Jul 2012
    • 355

    multiple bam files one command

    I have multiple .bam files in a directory that I would like to run the following command on:

    Code:
    samtools view -H Input.bam | sed '/^@PG/d' | samtools reheader - Input.bam > Input_newheader.bam
    The command works great for one file, but I am trying to use that command on all .bam file in a directory (/home/cmccabe/Desktop/NGS).

    to do multiple? or is there a better way? Thank you .

    Code:
    find *bam | parallel 'samtools view -H Input.bam | sed '/^@PG/d' | samtools reheader - Input.bam > Input_newheader.bam'
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #2
    Code:
    for f in *.bam
    do
    prefix=${f%%.bam}
    samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > ${prefix}_newheader.bam
    done
    This is going to quickly become IO bound, so you're unlikely to see much benefit from parallel. BTW, to do this with parallel, the simplest method is to just write a shell script that takes a single file as input and use that with parallel.

    Comment

    • cmccabe
      Senior Member
      • Jul 2012
      • 355

      #3
      If all the bam files are stored on a separate drive (/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215), and the output gets re-directed to (/home/cmccabe/Desktop/NGS/pool_I_090215)will the below work?

      Code:
      cd "/home/cmccabe/Desktop/NGS" -- path to samtools
      
      for f in *.bam
      do
      prefix=${f%%.bam}
      samtools view -H /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f | sed '/^@PG/d' | samtools reheader - /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
      done
      Thank you

      Comment

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #4
        "*.bam" is looking in the current working directory, so no, that won't work. If you said "/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam" then note that you would need to do something like:
        Code:
        bname=`basename $f`
        pref=${bname%%.bam}
        That would strip the path to the file appropriately. You would also then just use $f instead of /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/$f. For why that's the case, run:

        Code:
        for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
        do
        echo $f
        done

        Comment

        • cmccabe
          Senior Member
          • Jul 2012
          • 355

          #5
          Makes sense, so I tried: the bold is the output so why is it looking for those files? Thank you for your help.

          Code:
          cmccabe@HPZ640:~/Desktop/NGS$ for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam; do prefix=${f%%.bam}; samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215${prefix}_newheader.bam; done
          [B]bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_008_150902_newheader.bam: No such file or directory
          bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_015_rawlib_newheader.bam: No such file or directory
          bash: /home/cmccabe/Desktop/NGS/pool_I_090215/media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_016_150902_newheader.bam: No such file or directory[/B]

          Comment

          • dpryan
            Devon Ryan
            • Jul 2011
            • 3478

            #6
            You forgot the basename line.

            Comment

            • cmccabe
              Senior Member
              • Jul 2012
              • 355

              #7
              I thought I got it but I am a bit confused:

              Code:
              bname=`basename $f`
              pref=${bname%%.bam}
              for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
              do
              prefix=${f%%.bam}
              samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
              done
              gives the below error:

              Code:
              cmccabe@HPZ640:~$ cd "/home/cmccabe/Desktop/NGS"
              cmccabe@HPZ640:~/Desktop/NGS$ bname=`basename $f`
              basename: missing operand
              Try 'basename --help' for more information.
              cmccabe@HPZ640:~/Desktop/NGS$ pref=${bname%%.bam}
              cmccabe@HPZ640:~/Desktop/NGS$ for f in /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam
              > do
              > prefix=${f%%.bam}
              > samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${prefix}_newheader.bam
              > done
              bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_008_150902_newheader.bam: No such file or directory
              bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_015_rawlib_newheader.bam: No such file or directory
              bash: /home/cmccabe/Desktop/NGS/pool_I_090215//media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/IonXpress_016_150902_newheader.bam: No such file or directory
              Thank you .

              Comment

              • dpryan
                Devon Ryan
                • Jul 2011
                • 3478

                #8
                When you run basename, $f hasn't yet been defined...

                I'm actually not going to explicitly tell you the solution to this, you should be able to figure it out given a bit of playing around and noting the error message.

                Comment

                • cmccabe
                  Senior Member
                  • Jul 2012
                  • 355

                  #9
                  So here is the command:

                  Code:
                  /media/cmccabe/C2F8EFBFF8EFAFB9/pool_I_090215/*.bam ; do     bname=`basename $f`;     pref=${bname%%.bam};     samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${pref}_newheader.bam; done
                  This seems to work great, thank you for your help .

                  Comment

                  Latest Articles

                  Collapse

                  • SEQadmin2
                    Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                    by SEQadmin2



                    Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                    ...
                    07-09-2026, 11:10 AM
                  • SEQadmin2
                    Cancer Drug Resistance: The Lingering Barrier to Rising Survival
                    by SEQadmin2



                    Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

                    There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
                    07-08-2026, 05:17 AM
                  • GATTACAT
                    Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
                    by GATTACAT
                    Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
                    07-01-2026, 11:43 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 07-13-2026, 10:26 AM
                  0 responses
                  24 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-09-2026, 10:04 AM
                  0 responses
                  34 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-08-2026, 10:08 AM
                  0 responses
                  21 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 07-07-2026, 11:05 AM
                  0 responses
                  34 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...