jpayne@0: #! /usr/bin/env python3.6 jpayne@0: jpayne@0: import argparse jpayne@0: import uuid jpayne@0: import os, sys jpayne@0: from os.path import join as j jpayne@0: from itertools import zip_longest jpayne@0: jpayne@0: def setup(base_dir, names=[], fwds=[], revs=[], extension='fastq', pattern="{name}.{orient}.{ext}"): jpayne@0: if fwds and revs and names and len(fwds) != len(revs) != len(names): jpayne@0: raise ValueError('number of forward reads must equal number of reverse reads and names') jpayne@0: elif len(fwds) != len(names) or not fwds or not names: jpayne@0: raise ValueError('number of forward reads must equal number of names') jpayne@0: with open(j(base_dir, 'snp-unwind.sh'), 'w') as unwind: jpayne@0: #dir = j(base_dir, str(uuid.uuid4())) jpayne@0: for i, (name, fwd, rev) in enumerate(zip_longest(names, fwds, revs)): jpayne@0: dir = j(base_dir, str(i)) jpayne@0: sample_dir = j(dir, name) jpayne@0: os.makedirs(sample_dir) jpayne@0: target_f = j(sample_dir, pattern.format(name=name, orient=1, ext=extension)) jpayne@0: if rev: jpayne@0: target_r = j(sample_dir, pattern.format(name=name, orient=2, ext=extension)) jpayne@0: os.symlink(fwd, target_f) jpayne@0: if rev: jpayne@0: os.symlink(rev, target_r) jpayne@0: print(sample_dir) jpayne@0: if rev: jpayne@0: unwind.write('unlink {}\n'.format(target_r)) jpayne@0: unwind.write('unlink {}\n'.format(target_f)) jpayne@0: unwind.write('rmdir {}\n'.format(sample_dir)) jpayne@0: unwind.write('rmdir {}\n'.format(dir)) jpayne@0: jpayne@0: jpayne@0: jpayne@0: jpayne@0: if __name__ == '__main__': jpayne@0: parser = argparse.ArgumentParser(description="set up fastq symlink directories for snp-pipeline") jpayne@0: parser.add_argument('base_dir') jpayne@0: parser.add_argument('-n', dest='names', type=str, action='append', default=[]) jpayne@0: parser.add_argument('-f', dest='fwds', type=str, action='append', default=[]) jpayne@0: parser.add_argument('-r', dest='revs', type=str, action='append', default=[]) jpayne@0: parser.add_argument('-e', dest='extension', default='fastq') jpayne@0: parser.add_argument('-p', dest='pattern', default='{name}.{orient}.{ext}') jpayne@0: params = parser.parse_args() jpayne@0: setup(**vars(params))