Skip to content

Instantly share code, notes, and snippets.

@SJShaw
Created June 15, 2023 08:10
Show Gist options
  • Save SJShaw/c72db4371f711fcb28caa9c829704caa to your computer and use it in GitHub Desktop.
Save SJShaw/c72db4371f711fcb28caa9c829704caa to your computer and use it in GitHub Desktop.
Extract an HMM profile from a set
#!/usr/bin/env python3
import argparse
import os
import sys
from typing import IO
def _main(accession: str, source: IO, destination: IO) -> int:
if not accession:
print("error: accession cannot be empty", file=sys.stderr)
return 2
profile = []
correct_profile = False
for line in source:
profile.append(line)
if line.startswith("//"):
if correct_profile:
break
profile.clear()
if (line.startswith("NAME ") or line.startswith("ACC ")) and line.split()[-1] == accession:
correct_profile = True
if not profile:
print(f"error: no profile found with accession or name of: {accession}", file=sys.stderr)
return 3
if not profile[-1].startswith("//"):
print("error: input ended abruptly", file=sys.stderr)
return 4
for line in profile:
destination.write(line)
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("identifier", type=str, help="The name/accession of the profile to extract")
parser.add_argument("source", type=argparse.FileType('r'), default=sys.stdin,
help="The full set of HMMer profiles")
parser.add_argument("destination", nargs="?", type=argparse.FileType('w'), default=sys.stdout,
help="The output location (default: stdout)")
args = parser.parse_args()
sys.exit(_main(args.identifier, args.source, args.destination))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment