-
Notifications
You must be signed in to change notification settings - Fork 8
/
create_cross_species_motifs_rankings_db.py
executable file
·214 lines (180 loc) · 8.52 KB
/
create_cross_species_motifs_rankings_db.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""
Purpose : Create cisTarget cross-species motif rankings databases.
Copyright (C): 2020-2021 - Gert Hulselmans
"""
import argparse
import glob
import os
import re
import sys
import time
from typing import List
from cistarget_db import CisTargetDatabase, DatabaseTypes
ct_rankings_db_motifs_vs_regions_or_genes_species_pattern = re.compile(
r"""^(?P<db_prefix_minimal>.*?)(\.(?P<species>(?!(part[^.]+[0-9]+)|cross_?species)[^.]+))\.(?P<db_type>(?P<motifs_or_tracks_type>motifs)_vs_(?P<regions_or_genes_type>regions|genes)\.rankings\.feather)$"""
)
def create_cross_species_motifs_rankings_db(
cross_species_db_prefix: str,
ct_rankings_db_motifs_vs_regions_or_genes_species_filenames: List[str],
):
cross_species_db_prefix = (
cross_species_db_prefix
if cross_species_db_prefix.endswith(".cross_species")
else cross_species_db_prefix + ".cross_species"
)
start_creating_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time = (
time.monotonic()
)
print(
'Create cisTarget cross-species motifs rankings db from:\n - "'
+ '"\n - "'.join(ct_rankings_db_motifs_vs_regions_or_genes_species_filenames)
+ '"\n'
)
ct_cross_species_rankings_db_motifs_vs_regions_or_genes = CisTargetDatabase.create_cross_species_rankings_db(
species_rankings_db_filenames=ct_rankings_db_motifs_vs_regions_or_genes_species_filenames
)
elapsed_creating_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time = (
time.monotonic()
- start_creating_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time
)
print(
f"Creating cisTarget cross-species "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind} vs "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind} rankings db took: "
f"{elapsed_creating_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time:.06f} seconds"
)
start_writing_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time = (
time.monotonic()
)
ct_cross_species_rankings_db_motifs_vs_regions_or_genes_filename = ct_cross_species_rankings_db_motifs_vs_regions_or_genes.create_db_filename_from_db_prefix(
db_prefix=cross_species_db_prefix, extension="feather"
)
print(
f"Writing cisTarget cross-species "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind} vs "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind} rankings db: "
f'"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes_filename}"'
)
ct_cross_species_rankings_db_motifs_vs_regions_or_genes.write_db(
db_prefix=cross_species_db_prefix,
version=2,
)
elapsed_writing_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time = (
time.monotonic()
- start_writing_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time
)
print(
f"Writing cisTarget cross-species "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind} "
f"vs {ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind} rankings db "
f"took: {elapsed_writing_ct_cross_species_rankings_db_motifs_vs_regions_or_genes_time:.06f} seconds\n"
)
start_writing_ct_cross_species_rankings_db_regions_or_genes_vs_motifs_time = (
time.monotonic()
)
ct_cross_species_rankings_db_regions_or_genes_vs_motifs_filename = DatabaseTypes.from_strings(
scores_or_rankings="rankings",
column_kind=ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind,
row_kind=ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind,
).create_db_filename(
db_prefix=cross_species_db_prefix, extension="feather"
)
print(
f"Writing cisTarget cross-species "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind} vs "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind} rankings db: "
f'"{ct_cross_species_rankings_db_regions_or_genes_vs_motifs_filename}"'
)
ct_cross_species_rankings_db_motifs_vs_regions_or_genes.transpose().write_db(
db_prefix=cross_species_db_prefix,
version=2,
)
elapsed_writing_ct_cross_species_rankings_db_regions_or_genes_vs_motifs_time = (
time.monotonic()
- start_writing_ct_cross_species_rankings_db_regions_or_genes_vs_motifs_time
)
print(
f"Writing cisTarget cross-species "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.row_kind} vs "
f"{ct_cross_species_rankings_db_motifs_vs_regions_or_genes.db_type.column_kind} rankings db "
f"took: {elapsed_writing_ct_cross_species_rankings_db_regions_or_genes_vs_motifs_time:.06f} seconds\n"
)
def main():
parser = argparse.ArgumentParser(
description="Create cisTarget cross-species motifs rankings databases."
)
parser.add_argument(
"-i",
"--input",
dest="input",
action="store",
type=str,
required=True,
help="Input directory or database prefix with cisTarget motifs vs regions or genes rankings databases per "
"species.",
)
parser.add_argument(
"-o",
"--output",
dest="output_dir",
action="store",
type=str,
required=True,
help="Output directory to which the cisTarget cross-species motifs rankings database files will be written.",
)
args = parser.parse_args()
# Construct glob string to find "*.*.motifs_vs_*.rankings.feather" cisTarget rankings database per species.
ct_rankings_db_motifs_vs_regions_or_genes_species_glob_str = (
os.path.join(args.input, "*.*.motifs_vs_*.rankings.feather")
if os.path.isdir(args.input)
else args.input + "*.*.motifs_vs_*.rankings.feather"
)
# Get all "*.*.motifs_vs_*.rankings.feather" cisTarget databases in the input directory or the ones that start with
# the requested prefix.
ct_rankings_db_motifs_vs_regions_or_genes_species_filenames = sorted(
glob.glob(ct_rankings_db_motifs_vs_regions_or_genes_species_glob_str)
)
if len(ct_rankings_db_motifs_vs_regions_or_genes_species_filenames) == 0:
print(
f"Error: No cisTarget motifs vs regions or genes rankings databases per species found matching "
f'glob: "{ct_rankings_db_motifs_vs_regions_or_genes_species_glob_str}".',
file=sys.stderr,
)
sys.exit(1)
ct_dbs_hierarchical_dict = dict()
for (
ct_rankings_db_motifs_vs_regions_or_genes_species_filename
) in ct_rankings_db_motifs_vs_regions_or_genes_species_filenames:
# Parse cisTarget database filename, so databases made from the same regions/genes, but lifted over
# to different species can be grouped together.
match = ct_rankings_db_motifs_vs_regions_or_genes_species_pattern.match(
ct_rankings_db_motifs_vs_regions_or_genes_species_filename
)
if match:
db_prefix_minimal = match.group("db_prefix_minimal")
db_type = match.group("db_type")
species = match.group("species")
ct_dbs_hierarchical_dict.setdefault(db_prefix_minimal, dict())
ct_dbs_hierarchical_dict[db_prefix_minimal].setdefault(db_type, dict())
ct_dbs_hierarchical_dict[db_prefix_minimal][db_type].setdefault(
species, dict()
)
ct_dbs_hierarchical_dict[db_prefix_minimal][db_type][
species
] = ct_rankings_db_motifs_vs_regions_or_genes_species_filename
for db_prefix_minimal in ct_dbs_hierarchical_dict:
for db_type in ct_dbs_hierarchical_dict[db_prefix_minimal]:
# Get all cisTarget motifs vs regions or genes species rankings databases for the same regions/genes.
ct_rankings_db_motifs_vs_regions_or_genes_species_filenames = [
ct_dbs_hierarchical_dict[db_prefix_minimal][db_type][species]
for species in sorted(
ct_dbs_hierarchical_dict[db_prefix_minimal][db_type]
)
]
create_cross_species_motifs_rankings_db(
cross_species_db_prefix=db_prefix_minimal,
ct_rankings_db_motifs_vs_regions_or_genes_species_filenames=ct_rankings_db_motifs_vs_regions_or_genes_species_filenames,
)
if __name__ == "__main__":
main()