user3781528
user3781528

Reputation: 639

Rename files based on the content of another file

A directory contains multiple bam, corresponding bam.bai files and also a tab delimited matrixkey.txt. The matrixkey includes the barcodes in the first column that are also found in the bam/bai file names.

For example:   Barcode001    Sample1

I would like use barcodes to match files with sample names (located in the second column of the matrixkey), and rename all the files as following: ‘barcode’_’sampleid’.bam/bam.bai Please take a look at my code below and suggest improvements.

The routine runs without errors but files are not being renamed.

#!/usr/bin/perl
use warnings;
use strict;
use File::Copy qw(move);

my $home="/data/";                                                     

my $bam_directory = $home."test_all_runs/Test_Runs";

my $matrix_key = $home."test_all_runs/Test_Runs/matrixkey.txt";

my @matrix_key = ();

open(TXT2, "$matrix_key");
        while (<TXT2>){  
                push (@matrix_key, $_);   
                }
close(TXT2);

my @bam_files = glob "$bam_directory/*.bam";
my @bai_files = glob "$bam_directory/*.bai";

for my $bam_file (@bam_files){

my $matrix_barcode = "";
my $matrix_sample_id = "";
        foreach (@matrix_key){
                chomp($_);
                my @matrix_key = split ("\t", $_);##  
                if (index ($bam_file,$matrix_key[0]) != -1) {
                  $matrix_barcode = $matrix_key[0]; print $matrix_key[0];
                  $matrix_sample_id = $matrix_key[1];
                  chomp $matrix_barcode;
                  chomp $matrix_sample_id;
                  move $bam_file, $bam_directory."/".$matrix_sample_id."_".$matrix_barcode.".bam";
                }       
        }

}

Upvotes: 0

Views: 204

Answers (2)

ssr1012
ssr1012

Reputation: 2589

In my thoughts, that file::copy, rename or move were works with the direct variable's instead the concatenates or methods[::].

 move $bam_file, $bam_directory."/".$matrix_sample_id."_".$matrix_barcode.".bam";

instead

 my $dest_file = "$bam_directory/$matrix_sample_id\_$matrix_barcode\.bam"
 move($bam_file, $dest_file) || die "Couldn't move the files: $!..\n;

Upvotes: 1

Avikd
Avikd

Reputation: 166

You may try a simple awk script as an alternative for generating these paths, e.g

awk -v bam_directory=dir_name -F"\t" '{print "mv " bam_directory "/" $1 ".bam " bam_directory "/" $1 "_" $2 ".bam"}' matrixkey.txt> move_commands

and check the source file paths before the actual file move

cut -f2 -d' ' move_path |xargs  ls -l 

Upvotes: 0

Related Questions