annotate ivar_variants_to_vcf.py @ 12:e3bb03cf207a draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit a14db40361bcb2ee608bccd9222e1654aaea3324
author iuc
date Wed, 11 Jan 2023 09:55:20 +0000
parents c3f9b8720d37
children 0893a1dbb807
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
1 #!/usr/bin/env python
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
2 import argparse
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
3 import errno
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
4 import os
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
5 import re
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
6 import sys
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
7
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
8
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
9 def parse_args(args=None):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
10 Description = "Convert iVar variants tsv file to vcf format."
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
11 Epilog = """Example usage: python ivar_variants_to_vcf.py <FILE_IN> <FILE_OUT>"""
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
12
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
13 parser = argparse.ArgumentParser(description=Description, epilog=Epilog)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
14 parser.add_argument("FILE_IN", help="Input tsv file.")
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
15 parser.add_argument("FILE_OUT", help="Full path to output vcf file.")
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
16 parser.add_argument(
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
17 "-po",
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
18 "--pass_only",
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
19 dest="PASS_ONLY",
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
20 help="Only output variants that PASS all filters.",
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
21 action="store_true",
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
22 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
23
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
24 return parser.parse_args(args)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
25
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
26
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
27 def make_dir(path):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
28 if not len(path) == 0:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
29 try:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
30 os.makedirs(path)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
31 except OSError as exception:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
32 if exception.errno != errno.EEXIST:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
33 raise
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
34
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
35
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
36 def info_line(info_keys, kv):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
37 info_strings = []
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
38 for key in info_keys:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
39 if key not in kv:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
40 raise KeyError(
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
41 'Expected key {} missing from INFO field key value pairs'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
42 .format(key)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
43 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
44 if kv[key] is False:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
45 # a FLAG element, which should not be set
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
46 continue
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
47 if kv[key] is True:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
48 # a FLAG element => write the key only
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
49 info_strings.append(key)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
50 else:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
51 info_strings.append('{}={}'.format(key, kv[key]))
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
52 return ';'.join(info_strings)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
53
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
54
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
55 def ivar_variants_to_vcf(FileIn, FileOut, passOnly=False):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
56 filename = os.path.splitext(FileIn)[0]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
57 header = (
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
58 "##fileformat=VCFv4.2\n"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
59 "##source=iVar\n"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
60 '##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
61 '##INFO=<ID=REF_DP,Number=1,Type=Integer,Description="Depth of reference base">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
62 '##INFO=<ID=REF_RV,Number=1,Type=Integer,Description="Depth of reference base on reverse reads">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
63 '##INFO=<ID=REF_QUAL,Number=1,Type=Integer,Description="Mean quality of reference base">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
64 '##INFO=<ID=ALT_DP,Number=1,Type=Integer,Description="Depth of alternate base">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
65 '##INFO=<ID=ALT_RV,Number=1,Type=Integer,Description="Deapth of alternate base on reverse reads">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
66 '##INFO=<ID=ALT_QUAL,Number=1,Type=Integer,Description="Mean quality of alternate base">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
67 '##INFO=<ID=AF,Number=1,Type=Float,Description="Frequency of alternate base">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
68 '##INFO=<ID=INDEL,Number=0,Type=Flag,Description="Indicates that the variant is an INDEL.">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
69 '##FILTER=<ID=PASS,Description="Result of p-value <= 0.05">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
70 '##FILTER=<ID=FAIL,Description="Result of p-value > 0.05">\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
71 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
72 info_keys = [
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
73 re.match(r'##INFO=<ID=([^,]+),', line).group(1)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
74 for line in header.splitlines()
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
75 if line.startswith('##INFO=')
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
76 ]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
77 header += (
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
78 "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
79 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
80
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
81 vars_seen = set()
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
82 varCountDict = {"SNP": 0, "INS": 0, "DEL": 0}
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
83 OutDir = os.path.dirname(FileOut)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
84 make_dir(OutDir)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
85 with open(FileIn) as f, open(FileOut, "w") as fout:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
86 fout.write(header)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
87 for line in f:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
88 if line.startswith("REGION"):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
89 continue
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
90
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
91 line = line.split("\t")
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
92 CHROM = line[0]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
93 POS = line[1]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
94 ID = "."
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
95 REF = line[2]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
96 ALT = line[3]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
97 if ALT[0] == "+":
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
98 ALT = REF + ALT[1:]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
99 var_type = "INS"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
100 elif ALT[0] == "-":
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
101 REF += ALT[1:]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
102 ALT = line[2]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
103 var_type = "DEL"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
104 else:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
105 var_type = "SNP"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
106 QUAL = "."
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
107 pass_test = line[13]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
108 if pass_test == "TRUE":
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
109 FILTER = "PASS"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
110 else:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
111 FILTER = "FAIL"
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
112
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
113 if (passOnly and FILTER != "PASS"):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
114 continue
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
115 var = (CHROM, POS, REF, ALT)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
116 if var in vars_seen:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
117 continue
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
118
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
119 info_elements = {
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
120 'DP': line[11],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
121 'REF_DP': line[4],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
122 'REF_RV': line[5],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
123 'REF_QUAL': line[6],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
124 'ALT_DP': line[7],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
125 'ALT_RV': line[8],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
126 'ALT_QUAL': line[9],
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
127 'AF': line[10]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
128 }
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
129 if var_type in ['INS', 'DEL']:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
130 # add INDEL FLAG
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
131 info_elements['INDEL'] = True
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
132 else:
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
133 info_elements['INDEL'] = False
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
134 INFO = info_line(info_keys, info_elements)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
135
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
136 vars_seen.add(var)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
137 varCountDict[var_type] += 1
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
138 fout.write(
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
139 '\t'.join(
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
140 [CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
141 ) + '\n'
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
142 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
143
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
144 # Print variant counts to pass to MultiQC
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
145 varCountList = [(k, str(v)) for k, v in sorted(varCountDict.items())]
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
146 print("\t".join(["sample"] + [x[0] for x in varCountList]))
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
147 print("\t".join([filename] + [x[1] for x in varCountList]))
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
148
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
149
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
150 def main(args=None):
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
151 args = parse_args(args)
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
152 ivar_variants_to_vcf(
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
153 args.FILE_IN, args.FILE_OUT, args.PASS_ONLY
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
154 )
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
155
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
156
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
157 if __name__ == "__main__":
c3f9b8720d37 "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ivar/ commit 847ec10cd36ea4f3cd4c257d5742f0fb401e364e"
iuc
parents:
diff changeset
158 sys.exit(main())