annotate vsnp_add_zero_coverage.py @ 2:6fdfdee72f8a draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 10077c740e7cbe6a6563a1c632d711691753e46d"
author iuc
date Mon, 06 Dec 2021 18:28:42 +0000
parents ec6e02f4eab7
children 4535ad8b74f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
2
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
3 import argparse
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
4 import os
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
5 import re
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
6 import shutil
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
7
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
8 import pandas
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
9 import pysam
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
10 from Bio import SeqIO
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
11
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
12
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
13 def get_sample_name(file_path):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
14 base_file_name = os.path.basename(file_path)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
15 if base_file_name.find(".") > 0:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
16 # Eliminate the extension.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
17 return os.path.splitext(base_file_name)[0]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
18 return base_file_name
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
19
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
20
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
21 def get_coverage_df(bam_file):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
22 # Create a coverage dictionary.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
23 coverage_dict = {}
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
24 coverage_list = pysam.depth(bam_file, split_lines=True)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
25 for line in coverage_list:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
26 chrom, position, depth = line.split('\t')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
27 coverage_dict["%s-%s" % (chrom, position)] = depth
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
28 # Convert it to a data frame.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
29 coverage_df = pandas.DataFrame.from_dict(coverage_dict, orient='index', columns=["depth"])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
30 return coverage_df
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
31
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
32
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
33 def get_zero_df(reference):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
34 # Create a zero coverage dictionary.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
35 zero_dict = {}
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
36 for record in SeqIO.parse(reference, "fasta"):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
37 chrom = record.id
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
38 total_len = len(record.seq)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
39 for pos in list(range(1, total_len + 1)):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
40 zero_dict["%s-%s" % (str(chrom), str(pos))] = 0
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
41 # Convert it to a data frame with depth_x
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
42 # and depth_y columns - index is NaN.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
43 zero_df = pandas.DataFrame.from_dict(zero_dict, orient='index', columns=["depth"])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
44 return zero_df
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
45
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
46
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
47 def output_zc_vcf_file(base_file_name, vcf_file, zero_df, total_zero_coverage, output_vcf):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
48 column_names = ["CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO", "FORMAT", "Sample"]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
49 vcf_df = pandas.read_csv(vcf_file, sep='\t', header=None, names=column_names, comment='#')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
50 good_snp_count = len(vcf_df[(vcf_df['ALT'].str.len() == 1) & (vcf_df['REF'].str.len() == 1) & (vcf_df['QUAL'] > 150)])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
51 if total_zero_coverage > 0:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
52 header_file = "%s_header.csv" % base_file_name
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
53 with open(header_file, 'w') as outfile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
54 with open(vcf_file) as infile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
55 for line in infile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
56 if re.search('^#', line):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
57 outfile.write("%s" % line)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
58 vcf_df_snp = vcf_df[vcf_df['REF'].str.len() == 1]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
59 vcf_df_snp = vcf_df_snp[vcf_df_snp['ALT'].str.len() == 1]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
60 vcf_df_snp['ABS_VALUE'] = vcf_df_snp['CHROM'].map(str) + "-" + vcf_df_snp['POS'].map(str)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
61 vcf_df_snp = vcf_df_snp.set_index('ABS_VALUE')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
62 cat_df = pandas.concat([vcf_df_snp, zero_df], axis=1, sort=False)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
63 cat_df = cat_df.drop(columns=['CHROM', 'POS', 'depth'])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
64 cat_df[['ID', 'ALT', 'QUAL', 'FILTER', 'INFO']] = cat_df[['ID', 'ALT', 'QUAL', 'FILTER', 'INFO']].fillna('.')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
65 cat_df['REF'] = cat_df['REF'].fillna('N')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
66 cat_df['FORMAT'] = cat_df['FORMAT'].fillna('GT')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
67 cat_df['Sample'] = cat_df['Sample'].fillna('./.')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
68 cat_df['temp'] = cat_df.index.str.rsplit('-', n=1)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
69 cat_df[['CHROM', 'POS']] = pandas.DataFrame(cat_df.temp.values.tolist(), index=cat_df.index)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
70 cat_df = cat_df[['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO', 'FORMAT', 'Sample']]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
71 cat_df['POS'] = cat_df['POS'].astype(int)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
72 cat_df = cat_df.sort_values(['CHROM', 'POS'])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
73 body_file = "%s_body.csv" % base_file_name
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
74 cat_df.to_csv(body_file, sep='\t', header=False, index=False)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
75 with open(output_vcf, "w") as outfile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
76 for cf in [header_file, body_file]:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
77 with open(cf, "r") as infile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
78 for line in infile:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
79 outfile.write("%s" % line)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
80 else:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
81 shutil.move(vcf_file, output_vcf)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
82 return good_snp_count
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
83
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
84
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
85 def output_metrics_file(base_file_name, average_coverage, genome_coverage, good_snp_count, output_metrics):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
86 bam_metrics = [base_file_name, "", "%4f" % average_coverage, genome_coverage]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
87 vcf_metrics = [base_file_name, str(good_snp_count), "", ""]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
88 metrics_columns = ["File", "Number of Good SNPs", "Average Coverage", "Genome Coverage"]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
89 with open(output_metrics, "w") as fh:
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
90 fh.write("# %s\n" % "\t".join(metrics_columns))
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
91 fh.write("%s\n" % "\t".join(bam_metrics))
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
92 fh.write("%s\n" % "\t".join(vcf_metrics))
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
93
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
94
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
95 def output_files(vcf_file, total_zero_coverage, zero_df, output_vcf, average_coverage, genome_coverage, output_metrics):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
96 base_file_name = get_sample_name(vcf_file)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
97 good_snp_count = output_zc_vcf_file(base_file_name, vcf_file, zero_df, total_zero_coverage, output_vcf)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
98 output_metrics_file(base_file_name, average_coverage, genome_coverage, good_snp_count, output_metrics)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
99
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
100
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
101 def get_coverage_and_snp_count(bam_file, vcf_file, reference, output_metrics, output_vcf):
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
102 coverage_df = get_coverage_df(bam_file)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
103 zero_df = get_zero_df(reference)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
104 coverage_df = zero_df.merge(coverage_df, left_index=True, right_index=True, how='outer')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
105 # depth_x "0" column no longer needed.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
106 coverage_df = coverage_df.drop(columns=['depth_x'])
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
107 coverage_df = coverage_df.rename(columns={'depth_y': 'depth'})
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
108 # Covert the NaN to 0 coverage and get some metrics.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
109 coverage_df = coverage_df.fillna(0)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
110 coverage_df['depth'] = coverage_df['depth'].apply(int)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
111 total_length = len(coverage_df)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
112 average_coverage = coverage_df['depth'].mean()
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
113 zero_df = coverage_df[coverage_df['depth'] == 0]
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
114 total_zero_coverage = len(zero_df)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
115 total_coverage = total_length - total_zero_coverage
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
116 genome_coverage = "{:.2%}".format(total_coverage / total_length)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
117 # Output a zero-coverage vcf fil and the metrics file.
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
118 output_files(vcf_file, total_zero_coverage, zero_df, output_vcf, average_coverage, genome_coverage, output_metrics)
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
119
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
120
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
121 if __name__ == '__main__':
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
122 parser = argparse.ArgumentParser()
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
123
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
124 parser.add_argument('--bam_input', action='store', dest='bam_input', help='bam input file')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
125 parser.add_argument('--output_metrics', action='store', dest='output_metrics', required=False, default=None, help='Output metrics text file')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
126 parser.add_argument('--output_vcf', action='store', dest='output_vcf', required=False, default=None, help='Output VCF file')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
127 parser.add_argument('--reference', action='store', dest='reference', help='Reference dataset')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
128 parser.add_argument('--vcf_input', action='store', dest='vcf_input', help='vcf input file')
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
129
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
130 args = parser.parse_args()
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
131
ec6e02f4eab7 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/vsnp commit 95b221f68d19702681babd765c67caeeb24e7f1d"
iuc
parents:
diff changeset
132 get_coverage_and_snp_count(args.bam_input, args.vcf_input, args.reference, args.output_metrics, args.output_vcf)