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