-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_run.py
executable file
·54 lines (39 loc) · 1.75 KB
/
prep_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
from sys import argv
import json
import os
from uuid import uuid4
from glob import glob
from argparse import ArgumentParser
from shutil import move
parser = ArgumentParser(description="Helper script to create run directory.")
parser.add_argument(dest='json_file', help="master json file")
parser.add_argument('-base_dir', default='.')
parser.add_argument('-fastq_dir', required=True)
args = parser.parse_args()
data = json.load(open(args.json_file))
experiments = data['experiments']
for experiment in experiments :
if 'id' not in experiment :
experiment['id'] = uuid4().hex[:4]
experiment_dir = os.path.join(args.base_dir, experiment['id'])
for sample in experiment['samples'] :
if 'id' not in sample :
sample['id'] = uuid4().hex[:4]
sample_dir = os.path.join(experiment_dir, sample['id'])
for replicate, replicate_accessions in enumerate (sample['accession']) :
replicate_dir = os.path.join(sample_dir, str(replicate))
os.makedirs(replicate_dir, exist_ok=True)
for accession in replicate_accessions :
print (accession)
fastq_files = glob(os.path.join(os.path.abspath(args.fastq_dir), accession + '*.fastq.gz'))
assert len(fastq_files) > 0
for fn in fastq_files:
os.link(fn, os.path.join(replicate_dir, os.path.basename(fn)) )
with open(os.path.join(sample_dir, 'sample.json'), 'w') as f :
json.dump(sample, f)
with open(os.path.join(experiment_dir, 'experiment.json'), 'w') as f :
json.dump(experiment, f)
move(args.json_file, args.json_file+'.backup')
with open(args.json_file, 'w') as f:
json.dump(data, f)