0

I have 96 bam files, How do I output the txt file with the unique sample IDs? I am looping through the bam files, but need to assign unique output files. For example: SC845414.txt

#Typical Bam Files:
SC845414-CTGATCGT-GCGCATAT_Aligned.sortedByCoord.out.bam
SC845425-TGTGACTG-AGCCTATC_Aligned.sortedByCoord.out.bam

#!/bin/bash
#SBATCH --mem=110g
#SBATCH --cpus-per-task=12
#SBATCH --time=10-00:00:00

module load python

DIR=/PATH/*

for d in $DIR; do
    python -m HTSeq.scripts.count -s yes -f bam "$d" /PATH1/gencode.v35.annotation.gtf > /PATH3/HTseq/SC845414.txt
done

2 Answers 2

1

It depends highly on what exactly you mean by "sample ID".

Based on your example, if you mean "the part of the filename before the first dash", then you could do this:

for d in $DIR; do
    id=$(basename "$d" | cut -f 1 -d -)
    python -m HTSeq.scripts.count -s yes -f bam "$d" /PATH1/gencode.v35.annotation.gtf > "/PATH3/HTseq/$id.txt"
done
Sign up to request clarification or add additional context in comments.

9 Comments

Why is thre "" around this? "/PATH3/HTseq/$id.txt" and not just "id"?
You need the $ to make it a variable, so it needs to be $id, not id. And it's a good idea to quote anything with a variable in it, for several reasons (see here). Well, unless you're relying on features that don't work in quotes (like your use of $DIR where you expect the * to be expanded).
This is what I am getting:
DIR=/P/A/T/T/SC915966-GCGTCATT-CAGACGTT_Aligned.sortedByCoord.out.bam
id=$(printf '%s' "$DIR" | cut -f 1 -d -)
|
0

same; but using builtin variable interpolation instead of calling basename and cut

for d in $DIR; do
    fname=${d##*/}
    python -m HTSeq.scripts.count -s yes -f bam "$d" /PATH1/gencode.v35.annotation.gtf > "/PATH3/HTseq/${fname%%-*}.txt"
done

(edited to strip any leading path as well)

unfortunately stripping both the leading and trailing parts of a variable at once is beyond me (at the moment).

seems it should be do-able see: https://www.thegeekstuff.com/2010/07/bash-string-manipulation/

(no affiliation or endorsement; just first relevant web search)

2 Comments

Is this sh compatible or just bash?
bash as far as know, tcsh had some other convention I have since forgotten

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.