-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathprocess_gtf.py
More file actions
113 lines (95 loc) · 3.53 KB
/
process_gtf.py
File metadata and controls
113 lines (95 loc) · 3.53 KB
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
# Copyright 2024 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to process GTF into feather file."""
import os
import tempfile
from urllib import parse
from urllib import request
from absl import app
from absl import flags
from absl import logging
from alphagenome.data import transcript as transcript_utils
import pandas as pd
import pyranges
_GTF_PATH = flags.DEFINE_string(
'gtf_path', None, 'Path to GTF file.', required=True
)
_OUTPUT_PATH = flags.DEFINE_string(
'output_path', None, 'Path to GTF output feather file.', required=True
)
_SPLICE_SITES_OUTPUT_PATH = flags.DEFINE_string(
'splice_sites_output_path',
None,
'Path to splice_sites output feather file.',
)
def generate_splice_sites(
gtf: pd.DataFrame,
) -> tuple[pd.DataFrame, pd.DataFrame]:
"""Generates tissue-agnostic splice start/end sites from GTF."""
transcripts = {
k: transcript_utils.Transcript.from_gtf_df(df)
for k, df in gtf.groupby('transcript_id')
}
junctions = []
for _, transcript in transcripts.items():
junctions.extend(
[x.chromosome, x.start, x.end, x.strand] for x in transcript.introns
)
df = pd.DataFrame(
junctions, columns=['Chromosome', 'Start', 'End', 'Strand']
).sort_values(by=['Chromosome', 'Start'])
df = df.drop_duplicates().reset_index(drop=True)
return (
df[['Chromosome', 'Start', 'Strand']],
df[['Chromosome', 'End', 'Strand']],
)
def generate_gtf(path: str) -> pd.DataFrame:
"""Generates GTF from a GTF path."""
url = parse.urlparse(path)
if all([url.scheme, url.netloc]):
with tempfile.TemporaryDirectory() as d:
path, _ = request.urlretrieve(
path,
filename=os.path.join(d, os.path.basename(path)),
)
logging.info('Downloaded GTF to %s', path)
gtf = pyranges.read_gtf(path, as_df=True, duplicate_attr=True)
else:
gtf = pyranges.read_gtf(path, as_df=True, duplicate_attr=True)
gtf['gene_id_nopatch'] = gtf['gene_id'].str.split('.', expand=True)[0]
return gtf
def main(_) -> None:
if not _OUTPUT_PATH.value.endswith('.feather'):
raise ValueError('Output path must end with .feather')
logging.info('Reading GTF from %s', _GTF_PATH.value)
gtf = generate_gtf(_GTF_PATH.value)
logging.info('Writing GTF to %s', _OUTPUT_PATH.value)
gtf.to_feather(_OUTPUT_PATH.value)
if _SPLICE_SITES_OUTPUT_PATH.value is not None:
logging.info('Generating splice sites from GTF')
splice_sites_starts, splice_sites_ends = generate_splice_sites(gtf)
splice_sites_start_path = (
_SPLICE_SITES_OUTPUT_PATH.value.removesuffix('.feather')
+ '_starts.feather'
)
splice_sites_end_path = (
_SPLICE_SITES_OUTPUT_PATH.value.removesuffix('.feather')
+ '_ends.feather'
)
logging.info('Writing start splice sites to %s', splice_sites_start_path)
splice_sites_starts.to_feather(splice_sites_start_path)
logging.info('Writing end splice sites to %s', splice_sites_end_path)
splice_sites_ends.to_feather(splice_sites_end_path)
if __name__ == '__main__':
app.run(main)