baracus37
baracus37

Reputation: 33

Change output filename when running a command in a loop

I am using the Tracer software package (https://github.com/Teichlab/tracer). The program is invoked as followed:

tracer assemble [options] <file_1> [<file_2>] <cell_name> <output_directory>

The program runs on a single dataset and the output goes to /<output_directory>/<cell_name>

What I want to do now is run this program on multiple files. To do so this is what I do:

for filename in /home/tobias/tracer/datasets/test/*.fastq
do
echo "Processing $filename file..."
python tracer assemble --single_end --fragment_length 62 --fragment_sd 1 $filename Tcell_test output; 
done

This works in priciple, but as cell_name is static, every iteration overwrites the output from the previous iteration. How do I need to change my script in order to give the output folder the name of the input file?

For example: Input filename is tcell1.fastq. For this cell_name should be tcell1. Next file is tcell2.fastq and cell_name should be tcell2, and so on...

Upvotes: 3

Views: 516

Answers (2)

cxw
cxw

Reputation: 17041

I think this will do it, in bash, if I understand correctly -

for filename in /home/tobias/tracer/datasets/test/*.fastq
do
  echo "Processing $filename file..."
  basefilename="${filename##*/}"   #<--- 
  python tracer assemble --single_end --fragment_length 62 --fragment_sd 1 "$filename" "${basefilename%.fastq}" output;
  #                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^
done

${filename##*/} removes the part up to the last /, and ${basefilename%.fastq} removes the .fastq at the end.

Upvotes: 1

Yasser Hussain
Yasser Hussain

Reputation: 854

From what I understood the library you use writes output to predefined (non configurable) directory

Let's call it output_dir. At each iteration you should rename the output directory.

So your code should be something like this (pseudo code)

for filename in /home/tobias/tracer/datasets/test/*.fastq
   do
     echo "Processing $filename file..."
     python tracer assemble --single_end --fragment_length 62 --fragment_sd 1 $filename Tcell_test output; 
     rename  output_dir , each_file + "_output_dir"
   done

Upvotes: 0

Related Questions