-2

I am trying recreate python script from my perl script to find all files with common name model1_r.pdb and them move it to a new folder with newname of their previous existing folder. This is the python code I wrote;

import os, shutil, glob
# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

source = input("enter filepath: ")

# build the command to look for model1_r.pdb
for file_path in glob.glob(os.path.join(source, '*model1_r.pdb')):
        new_dir = file_path.rsplit('/', 1)[1]
        find = os.system("find -type f -name \"model1_r.pdb\" ")
        print(filepath)
        destination = "/gs/gsfs0/users/kdhusia/dir_all_pdb/"
        #Move the files to new folder based with filepath folder name
        shutil.move(find, ios.path.join(destination, os.path.basename(filepath)))

Here is my perl script that works fine, but is slow

use strict;
use warnings;
use Data::Dumper;
use File::Basename;

# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

my $dir = $ARGV[0] || undef;
die "a directory is required\n" unless $dir;

chomp $dir;

# build the command to look for model1_r.pdb
my $cmd = "find $dir -type f -name \"model1_r.pdb\" "; 
#my $cmd = "find $dir -type f -name \"*.pdb\" "; 
print $cmd, "\n";

my @output = `$cmd`;

my $DEST = "/gs/gsfs0/users/kdhusia/dcomplex_all";
for my $line (@output) {
   # remove CR from $line
   chomp $line;
   #print $line;
   # get pair-name from line
   my @pairs = split("\/", $line);
   #print Dumper(\@pairs);

   my $new_name = $pairs[2].".pdb";
   #print "new_name = $new_name\n";

   # copy model1_r.pdb to dinal destination
   # change the name of pdb to "pair-name.pdb"
   my $cmd = "cp $line $DEST/$new_name"; 
   # my $cmd = "./dcomplex $pdbfile A B "
   print "$cmd\n";

   # do the copy
   system($cmd);
}

I did the conversion using previous discussions at stack(Python: Moving files to folder based on filenames)

I am missing out something during the storing the filepath. Any comments would be highly appreciated.

Kay
  • 90
  • 8

1 Answers1

0

Here is the reciprocal python3 code:

import os
import shutil
import glob
import argparse

# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

parser = argparse.ArgumentParser()
parser.add_argument("file", help="filename is required", type=str)
args = parser.parse_args()

print (args.file)

dest = "/gs/gsfs0/users/.../dir_all_pdb/"
# build the command to look for model1_r.pdb
#for f in os.path.listdir(source):
for root, dirs, files in os.walk(args.file):
    for file in files:
        if file.endswith("l1_r.pdb"):
            #print (file)
            print ("moving ...")
            print(os.path.join(root, file)) # printing full pathname
            # extratc the name ofthe pair. position 6 identifies tha name of the pair 
            pair_name =  os.path.join(root, file).split("/")
            name = pair_name[6]

            # buildong final destination ful path name 
            f_name = dest + name+'.pdb'
            print ("to ...", f_name)           

            src = os.path.join(root, file)
            shutil.copy( src, f_name)

This takes path of directory to look for files name and then store it to the desination folder with the names of folder in which the file was found.

Kay
  • 90
  • 8