-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_deliverable.py
128 lines (97 loc) · 4 KB
/
make_deliverable.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
import os
import sys
import argparse
import textwrap
import subprocess
# TODO: Moved out of its initial folder, so some stuf (e.g. paths) may not work
__version__ = '1.0'
def eval_projects(projects):
try:
# Get the part of the project name that should be common
proj = ['_'.join(p.split('_')[2:6]) for p in projects]
# Count how many times the 1st element occurs.
# If equal to the list length, then all elements
# are the same (as should)
return proj.count(proj[0]) == len(proj)
except IndexError:
print("Error! Make sure the project name(s) you gave are valid.")
exit(1)
def make_deliverable(args):
target_dir = 'deliverables'
if os.path.exists(target_dir):
os.chdir(target_dir)
else:
print("'{0}' directory does not exist. Exiting...".format(target_dir))
exit(1)
if not eval_projects(args.project):
print("Error! Make sure the projects are \
part of a bigger biological project.")
print("Use --help for more information.")
exit(1)
if args.output is None:
args.output = '_'.join(args.project[0].split('_')[2:7])
# Create the deliverable directory for the project, and move to it
try:
os.mkdir(args.output)
except OSError as e:
print(e)
exit(1)
finally:
os.chdir(args.output)
print(os.getcwd())
# Create 'fastq_files' directory if fastq argument was set
if args.fastq:
args.fastq = 'fastq_files'
os.mkdir(args.fastq)
# Get a dictionary of listed (sample) directories in each project
#sample_dirs = {}
projects_dir = '../../projects/'
cmd = 'rsync -r --progress {0} {1}'
for project in args.project:
# Form the dir to get the data from
source_dir = os.path.join(projects_dir, project + '/', 'counts/')
# print(source_dir)
p = subprocess.Popen(cmd.format(source_dir + '*', '.'),
shell=True).wait()
# Copy the fastq files if the fastq argument was set
if args.fastq:
source_dir = os.path.join(projects_dir, project + '/', 'fastqs/')
p = subprocess.Popen(cmd.format(source_dir + '*', args.fastq),
shell=True).wait()
def main():
# Move the working directory to the local root folder
# NOTE: Use when you call this script by running a link (softlink).
# os.chdir(os.path.dirname(sys.path[0]))
# Create a text wrapper for the help output text
w = textwrap.TextWrapper(width=90,
break_long_words=False,
replace_whitespace=False)
help_descr = "This script is used to create deliverables for the given projects"
# Initialize a parser
parser = argparse.ArgumentParser(
description=help_descr,
formatter_class=argparse.RawDescriptionHelpFormatter)
# -- Add the appropriate arguments -- #
parser.add_argument(
'-v', '--version',
action='version',
version='%(prog)s {0}'.format(__version__))
parser.add_argument(
'-f', '--fastq', action='store_const', const=True,
help='If set, it will also include the fastqs in the deliverable.')
parser.add_argument(
'-p', '--project', nargs='*', required=True,
help='Names of the projects you want to include in the deliverable.\
You can give multiple projects by simply separete them with space.\
Note that the projects should be part of a bigger biological project.\
In other words, this part of the name: 10X_YY_NNN_## should be common.')
parser.add_argument(
'-o', '--output',
help='(Optional) The name of the deliverable to be created.')
# Parse the given arguments
args = parser.parse_args()
# Call the function make_deliverable() to create the desired output
make_deliverable(args)
if __name__ == '__main__':
main()