Skip to content

Instantly share code, notes, and snippets.

@philiptzou
Last active October 27, 2023 16:27
Show Gist options
  • Save philiptzou/8d6c7c61d2242f730a1a2f87ba9a2a72 to your computer and use it in GitHub Desktop.
Save philiptzou/8d6c7c61d2242f730a1a2f87ba9a2a72 to your computer and use it in GitHub Desktop.
A Python implementation of LANL Hypermut2
# https://www.hiv.lanl.gov/content/sequence/HYPERMUT/hypermut.html
#! /usr/bin/env python
"""
MIT License
Copyright (c) 2023 Stanford University HIVDB Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import re
import sys
from scipy.stats import fisher_exact
inf = float('inf')
IUPAC_CODES = {
'R': r'[RAG]',
'Y': r'[YCT]',
'B': r'[BYSKCGT]',
'D': r'[DRWKAGT]',
'H': r'[HYWMACT]',
'V': r'[VRSMAGC]',
'N': r'[NRYBDHVWSKMACGT]',
'W': r'[WAT]',
'S': r'[SCG]',
'K': r'[KGT]',
'M': r'[MAC]',
}
VALID_NA_PATTERN = re.compile(r'[NRYBDHVWSKMACGT]')
def expand_iupac(pattern):
result = []
for char in pattern:
if char in IUPAC_CODES:
result.append(IUPAC_CODES[char])
else:
result.append(char)
return ''.join(result)
DEFAULT_PATTERNS = {
'hypermut_from': re.compile(expand_iupac(r'^G(?=RD)')),
'hypermut_to': re.compile(expand_iupac(r'^A(?=RD)')),
'control_from': re.compile(expand_iupac(r'^G(?=YN|RC)')),
'control_to': re.compile(expand_iupac(r'^A(?=YN|RC)')),
}
def fasta_reader(filename):
with open(filename) as fp:
header = None
seq = []
for line in fp:
if line.startswith('#'):
continue
elif line.startswith('>'):
if seq:
yield header, ''.join(seq).upper()
header = line[1:].strip()
seq = []
else:
seq.append(line.strip())
if seq:
yield header, ''.join(seq).upper()
def find_sites(seq, pattern, site_range):
sites = []
for offset in site_range:
match = pattern.search(seq[offset:])
if match:
sites.append(offset)
return sites
def get_comparable_sites(refseq, naseq):
sites = []
for offset, (ref, na) in enumerate(zip(refseq, naseq)):
if not VALID_NA_PATTERN.match(ref):
continue
if not VALID_NA_PATTERN.match(na):
continue
sites.append(offset)
return sites
def hypermut(refseq, naseq, patterns=DEFAULT_PATTERNS):
comparable_sites = get_comparable_sites(refseq, naseq)
potential_muts = find_sites(
refseq, patterns['hypermut_from'], comparable_sites)
potential_ctrls = find_sites(
refseq, patterns['control_from'], comparable_sites)
matched_muts = find_sites(
naseq, patterns['hypermut_to'], potential_muts)
matched_ctrls = find_sites(
naseq, patterns['control_to'], potential_ctrls)
num_potential_muts = len(potential_muts)
num_matched_muts = len(matched_muts)
num_potential_ctrls = len(potential_ctrls)
num_matched_ctrls = len(matched_ctrls)
try:
oddsratio = (
(num_matched_muts / num_potential_muts) /
(num_matched_ctrls / num_potential_ctrls)
)
except ZeroDivisionError:
oddsratio = inf
_, p = fisher_exact([
[num_matched_muts, num_potential_muts - num_matched_muts],
[num_matched_ctrls, num_potential_ctrls - num_matched_ctrls]
], 'greater')
return (
num_matched_muts,
num_potential_muts,
num_matched_ctrls,
num_potential_ctrls,
oddsratio,
p)
def main():
if len(sys.argv) != 2:
print('Usage: {} <FASTA_FILE>'.format(sys.argv[0]),
file=sys.stderr)
exit(1)
fasta_filename = sys.argv[1]
sequences = list(fasta_reader(fasta_filename))
_, refseq = sequences.pop(0)
for _, naseq in sequences:
print(hypermut(refseq, naseq, DEFAULT_PATTERNS))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment