├── .gitignore ├── README.md ├── gencode_utr_fix ├── __init__.py └── main.py ├── setup.py └── tests └── test_gencode_utr_fix.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 98 | __pypackages__/ 99 | 100 | # Celery stuff 101 | celerybeat-schedule 102 | celerybeat.pid 103 | 104 | # SageMath parsed files 105 | *.sage.py 106 | 107 | # Environments 108 | .env 109 | .venv 110 | env/ 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | 116 | # Spyder project settings 117 | .spyderproject 118 | .spyproject 119 | 120 | # Rope project settings 121 | .ropeproject 122 | 123 | # mkdocs documentation 124 | /site 125 | 126 | # mypy 127 | .mypy_cache/ 128 | .dmypy.json 129 | dmypy.json 130 | 131 | # Pyre type checker 132 | .pyre/ 133 | 134 | # pytype static type analyzer 135 | .pytype/ 136 | 137 | # Cython debug symbols 138 | cython_debug/ 139 | 140 | data/ 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gencode UTR fix 2 | 3 | Gencode GTF does not differentiate UTR as 5' and 3' UTR but annotates all of them as `UTR` unlike Ensembl GTF which annotates UTR as `five_prime_utr` and `three_prime_utr`. Thus, gencode annotation creates difficulty while studying UTR type-specific processes such as alternative polyadenylation. 4 | 5 | This package fixes UTR features in the third columns of Gencode GTF by converting `UTR` annotation into `five_prime_utr` and `three_prime_utr` similar to Ensembl. Package compares the location of UTR with CDS in GTF and annotates UTRs as `five_prime_utr` if UTR is located before CDS and `three_prime_utr` if UTR is located after CDS. 6 | 7 | ## Setup 8 | 9 | 10 | ```shell 11 | pip install cython 12 | pip install -e git+https://github.com/MuhammedHasan/gencode_utr_fix.git#egg=gencode_utr_fix 13 | ``` 14 | 15 | 16 | ## Run 17 | 18 | ```shell 19 | gencode_utr_fix --input_gtf gencode.v29.annotation.gtf --output_gtf gencode.v29.annotation_utr.gtf 20 | ``` 21 | 22 | 23 | ## Test 24 | ``` 25 | pytest tests/ 26 | ``` 27 | 28 | ## Cite 29 | This package is based on [pyranges](https://github.com/biocore-ntnu/pyranges) and designed for [lapa](https://github.com/mortazavilab/lapa) so cite the PyRanges and LAPA if you are using this package for research: 30 | 31 | PyRanges: http://dx.doi.org/10.1093/bioinformatics/btz615 32 | 33 | LAPA: https://www.biorxiv.org/content/10.1101/2022.11.08.515683v1 34 | -------------------------------------------------------------------------------- /gencode_utr_fix/__init__.py: -------------------------------------------------------------------------------- 1 | from gencode_utr_fix.main import gencode_utr_fix 2 | -------------------------------------------------------------------------------- /gencode_utr_fix/main.py: -------------------------------------------------------------------------------- 1 | import click 2 | from tqdm import tqdm 3 | import pyranges as pr 4 | 5 | 6 | class UtrUpdater: 7 | 8 | def __init__(self, gr: pr.PyRanges): 9 | self.starts, self.ends = self.cds_start_end(gr) 10 | 11 | @staticmethod 12 | def cds_start_end(gr: pr.PyRanges): 13 | print('Calculating CDS locations...') 14 | 15 | starts = dict() 16 | ends = dict() 17 | 18 | for i, df in tqdm(gr.df.groupby('transcript_id')): 19 | 20 | df = df[df['Feature'] == 'CDS'] 21 | 22 | if df.shape[0] == 0: 23 | continue 24 | 25 | starts[i] = df['Start'].min() 26 | ends[i] = df['End'].max() 27 | 28 | return starts, ends 29 | 30 | def __call__(self, row): 31 | if row['Feature'] == 'UTR': 32 | cds_start = self.starts[row['transcript_id']] 33 | cds_end = self.ends[row['transcript_id']] 34 | 35 | if row['Start'] < cds_start: 36 | if row['Strand'] == '+': 37 | row['Feature'] = 'five_prime_utr' 38 | elif row['Strand'] == '-': 39 | row['Feature'] = 'three_prime_utr' 40 | else: 41 | raise ValueError('UTR type cannot determined') 42 | 43 | elif cds_end < row['End']: 44 | if row['Strand'] == '+': 45 | row['Feature'] = 'three_prime_utr' 46 | elif row['Strand'] == '-': 47 | row['Feature'] = 'five_prime_utr' 48 | else: 49 | raise ValueError('UTR type cannot determined') 50 | 51 | else: 52 | raise ValueError('UTR type cannot determined') 53 | return row 54 | 55 | 56 | def gencode_utr_fix(gr): 57 | """ 58 | Update gtf pyranges objects UTRs. 59 | 60 | Args: 61 | gr: (pyranges.PyRanges) pyrange for GTF. 62 | """ 63 | update_utr_row = UtrUpdater(gr) 64 | print('Calculating UTR side...') 65 | return gr.apply(lambda df: df.apply(update_utr_row, axis=1)) 66 | 67 | 68 | @click.command() 69 | @click.option('--input_gtf', help='Input gencode gtf file.') 70 | @click.option('--output_gtf', help='Output gtf file.') 71 | def cli(input_gtf, output_gtf): 72 | print('Reading GTF...') 73 | gr = pr.read_gtf(input_gtf) 74 | gr_utr = gencode_utr_fix(gr) 75 | gr_utr.to_gtf(output_gtf) 76 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | 4 | requirements = [ 5 | 'setuptools', 6 | 'tqdm', 7 | 'click', 8 | 'pyranges' 9 | ] 10 | 11 | setup_requirements = ['pytest-runner', ] 12 | 13 | test_requirements = ['pytest'] 14 | 15 | setup( 16 | author="M. Hasan Çelik", 17 | author_email='muhammedhasancelik@gmail.com', 18 | classifiers=[ 19 | 'Natural Language :: English', 20 | 'Programming Language :: Python :: 3.6', 21 | ], 22 | description="Fix UTR of Gencode", 23 | install_requires=requirements, 24 | license="MIT license", 25 | keywords=['Gencode', 'UTR'], 26 | name='gencode_utr_fix', 27 | entry_points=''' 28 | [console_scripts] 29 | gencode_utr_fix=gencode_utr_fix.main:cli 30 | ''', 31 | packages=find_packages(include=['gencode_utr_fix']), 32 | setup_requires=setup_requirements, 33 | test_suite='tests', 34 | tests_require=test_requirements, 35 | url='https://github.com/MuhammedHasan/gencode_utr_fix', 36 | version='1.0.0', 37 | ) 38 | -------------------------------------------------------------------------------- /tests/test_gencode_utr_fix.py: -------------------------------------------------------------------------------- 1 | import pyranges as pr 2 | from gencode_utr_fix import gencode_utr_fix 3 | 4 | 5 | def test_gencode_utr_fix(): 6 | gr = pr.data.gencode_gtf() 7 | gr_utr = gencode_utr_fix(gr) 8 | 9 | assert gr.df.shape == gr_utr.df.shape 10 | 11 | for i, df in gr_utr.df.groupby('transcript_id'): 12 | strand = df[df['Feature'] == 'transcript'].iloc[0]['Strand'] 13 | 14 | for utr5 in df[df['Feature'] == 'five_prime_utr']['End']: 15 | for utr3 in df[df['Feature'] == 'three_prime_utr']['Start']: 16 | 17 | if strand == '+': 18 | assert utr3 > utr5 19 | else: 20 | assert utr3 < utr5 21 | --------------------------------------------------------------------------------