Skip to content

Instantly share code, notes, and snippets.

@bluegenes
Last active July 1, 2019 02:47
Show Gist options
  • Save bluegenes/075e96f68e141451452b0ea741c8e6d2 to your computer and use it in GitHub Desktop.
Save bluegenes/075e96f68e141451452b0ea741c8e6d2 to your computer and use it in GitHub Desktop.
given a list of sample names and a source location, copy relevant files somewhere else
import os
import sys
import argparse
import glob
from shutil import copyfile
# requires python >= 3.6
# run: python copy_samplefiles.py haptophyta.txt --source ../../pep/ --destination ../../haptophyta_pep
def copy_assemblyfiles(samplelist, source, destination):
if not os.path.exists(destination):
try:
os.mkdir(destination)
except Exception as e:
sys.stderr.write(f"\n\tError: cannot make {destination} destination folder. Please fix.\n\n")
sys.exit(-1)
with open(samplelist, 'r') as f:
for line in f:
sample = line.strip()
samplefile = glob.glob(os.path.join(source, sample + '*'))
if len(samplefile) > 1:
sys.stderr.write(f"\n\tError: only expecting a single match per sample name. Fix samplenames (this sample: {sample}) or edit this script :) \n\n")
sys.exit(-1)
sampleF = samplefile[0]
dest = os.path.join(destination, os.path.basename(sampleF))
# orthofinder needs files to end in ".fa" or ".fasta"
if dest.endswith('.pep'):
#dest = dest.split('.pep')[0] + '.fasta'
dest = dest + '.fasta'
copyfile(sampleF, dest)
if __name__ == '__main__':
p = argparse.ArgumentParser()
p.add_argument('sample_list')
p.add_argument('--source', default = os.getcwd())
p.add_argument('--destination', required=True)
args = p.parse_args()
sys.exit(copy_assemblyfiles(args.sample_list, args.source, args.destination))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment