Skip to content

Commit

Permalink
Merge pull request #347 from jodyphelan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jodyphelan authored Apr 29, 2024
2 parents c2dc114 + b8e8a34 commit bf8feb9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 58 deletions.
54 changes: 0 additions & 54 deletions .github/workflows/docs.yml

This file was deleted.

42 changes: 42 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest
defaults:
run:
shell: bash -el {0}
steps:
- uses: actions/checkout@v4
- uses: conda-incubator/setup-miniconda@v3
with:
python-version: 3.8
mamba-version: "*"
channels: conda-forge,bioconda,defaults
channel-priority: true

- name: install dependancies
run: |
mamba create -n test tb-profiler -y
- name: Install dependencies
run: |
conda activate test
pip install --force-reinstall .
- name: Run tests
run: |
cd tests
mamba install pytest -y
pytest -x .
30 changes: 27 additions & 3 deletions tbprofiler/phylo.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,44 @@ def generate_low_dp_mask(bam: str,ref: str,outfile: str,min_dp: int = 10) -> Non
for x in missing_positions:
O.write(f"{x[0]}\t{x[1]}\t{x[1]+1}\n")

def generate_low_dp_mask_vcf(vcf: str,outfile: str,min_dp: int = 10) -> None:
missing_positions = []
vcf_obj = pysam.VariantFile(vcf)
for rec in vcf_obj:
# use AD field if available
if 'AD' in rec.samples[0]:
dp = sum(rec.samples[0]['AD'])
else:
dp = rec.samples[0]['DP']
if dp<min_dp:
missing_positions.append((rec.chrom,rec.pos))

# write missing positions to bed file
with open(outfile,"w") as O:
for x in missing_positions:
O.write(f"{x[0]}\t{x[1]}\t{x[1]+1}\n")

def prepare_usher(treefile: str,vcf_file: str) -> None:
run_cmd(f"usher --tree {treefile} --vcf {vcf_file} --collapse-tree --save-mutation-annotated-tree phylo.pb")

def prepare_sample_consensus(sample: str,input_vcf: str,args: argparse.Namespace) -> str:
s = sample
tmp_vcf = f"{args.files_prefix}.{s}.vcf.gz"
run_cmd(f"bcftools norm -m - {input_vcf} | bcftools view -T ^{args.conf['bedmask']} | bcftools filter --SnpGap 50 | bcftools view -v snps | annotate_maaf.py | bcftools filter -S . -e 'MAAF<0.7' |bcftools filter -S . -e 'FMT/DP<20' | rename_vcf_sample.py --sample-name {s} | bcftools view -v snps -Oz -o {tmp_vcf}")
run_cmd(f"bcftools norm -m - {input_vcf} | bcftools view -T ^{args.conf['bedmask']} | bcftools filter --SnpGap 50 | bcftools view -v snps | annotate_maaf.py | bcftools filter -S . -e 'MAAF<0.7' |bcftools filter -S . -e 'FMT/DP<{args.conf['variant_filters']['depth_soft']}' | rename_vcf_sample.py --sample-name {s} | bcftools view -v snps -Oz -o {tmp_vcf}")
run_cmd(f"bcftools index {tmp_vcf}")

mask_bed = f"{args.files_prefix}.{s}.mask.bed"
if hasattr(args,'supplementary_bam') and args.supplementary_bam:
args.bam = args.supplementary_bam
generate_low_dp_mask(f"{args.bam}",args.conf['ref'],mask_bed)
run_cmd(f"bcftools consensus --sample {s} -m {mask_bed} -M N -f {args.conf['ref']} {tmp_vcf} | sed 's/>/>{s} /' > {args.files_prefix}.{s}.consensus.fa")
if args.bam:
generate_low_dp_mask(f"{args.bam}",args.conf['ref'],mask_bed)
mask_cmd = f"-m {mask_bed} -M N"
elif args.vcf:
generate_low_dp_mask_vcf(args.vcf,mask_bed)
mask_cmd = f"-m {mask_bed} -M N"
else:
mask_cmd = ""
run_cmd(f"bcftools consensus --sample {s} {mask_cmd} -f {args.conf['ref']} {tmp_vcf} | sed 's/>/>{s} /' > {args.files_prefix}.{s}.consensus.fa")
return f"{args.files_prefix}.{s}.consensus.fa"

def get_consensus_vcf(sample: str,input_vcf: str,args: argparse.Namespace) -> str:
Expand Down
3 changes: 2 additions & 1 deletion tests/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
]

db = 'testdb'
branch = 'who'

def test_db():
run_cmd(f"tb-profiler update_tbdb --branch test --prefix {db}")
run_cmd(f"tb-profiler update_tbdb --branch {branch} --prefix {db}")


def check_assertations(filename):
Expand Down

0 comments on commit bf8feb9

Please sign in to comment.