• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openvax / varcode / 9781104697

03 Jul 2024 03:56PM UTC coverage: 88.33% (-6.4%) from 94.753%
9781104697

Pull #253

github

kodysy02
Defaulting convert_ucsc_contig_names to True for consistency with past behavior
Pull Request #253: Defaulting convert_ucsc_contig_names to True for consistency with past behavior

1 of 1 new or added line in 1 file covered. (100.0%)

144 existing lines in 17 files now uncovered.

1544 of 1748 relevant lines covered (88.33%)

2.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/varcode/cli/variant_args.py
1
# Licensed under the Apache License, Version 2.0 (the "License");
2
# you may not use this file except in compliance with the License.
3
# You may obtain a copy of the License at
4
#
5
#     http://www.apache.org/licenses/LICENSE-2.0
6
#
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS,
9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
# See the License for the specific language governing permissions and
11
# limitations under the License.
12

13
from argparse import ArgumentParser
3✔
14

15
from ..vcf import load_vcf
3✔
16
from ..maf import load_maf
3✔
17
from ..variant_collection import VariantCollection
3✔
18
from ..variant import Variant
3✔
19

20

21
def add_variant_args(arg_parser):
3✔
22
    """
23
    Extends an ArgumentParser instance with the following commandline arguments:
24
        --vcf
25
        --genome
26
        --maf
27
        --variant
28
        --json-variants
29
    """
30
    variant_arg_group = arg_parser.add_argument_group(
3✔
31
        title="Variants",
32
        description="Genomic variant files")
33

34
    variant_arg_group.add_argument(
3✔
35
        "--vcf",
36
        default=[],
37
        action="append",
38
        help="Genomic variants in VCF format")
39

40
    variant_arg_group.add_argument(
3✔
41
        "--maf",
42
        default=[],
43
        action="append",
44
        help="Genomic variants in TCGA's MAF format",)
45

46
    variant_arg_group.add_argument(
3✔
47
        "--variant",
48
        default=[],
49
        action="append",
50
        nargs=4,
51
        metavar=("CHR", "POS", "REF", "ALT"),
52
        help=(
53
            "Individual variant as 4 arguments giving chromsome, position, ref,"
54
            " and alt. Example: chr1 3848 C G. Use '.' to indicate empty alleles"
55
            " for insertions or deletions."))
56

57
    variant_arg_group.add_argument(
3✔
58
        "--genome",
59
        type=str,
60
        help=(
61
            "What reference assembly your variant coordinates are using. "
62
            "Examples: 'hg19', 'GRCh38', or 'mm9'. "
63
            "This argument is ignored for MAF files, since each row includes "
64
            "the reference. "
65
            "For VCF files, this is used if specified, and otherwise is guessed from "
66
            "the header. For variants specfied on the commandline with --variant, "
67
            "this option is required."))
68

69
    variant_arg_group.add_argument(
3✔
70
        "--download-reference-genome-data",
71
        action="store_true",
72
        default=False,
73
        help=(
74
            ("Automatically download genome reference data required for "
75
             "annotation using PyEnsembl. Otherwise you must first run "
76
             "'pyensembl install' for the release/species corresponding "
77
             "to the genome used in your VCF.")))
78

79
    variant_arg_group.add_argument(
3✔
80
        "--json-variants",
81
        default=[],
82
        action="append",
83
        help="Path to Varcode.VariantCollection object serialized as a JSON file.")
84

85
    return variant_arg_group
3✔
86

87

88
def make_variants_parser(**kwargs):
3✔
89
    """
90
    Parameters
91
    ----------
92
    **kwargs : dict
93
        Passed directly to argparse.ArgumentParser
94

95
    Creates argparse.ArgumentParser instance with options needed for loading
96
    variants from VCF, MAF, or JSON files.
97
    """
98
    parser = ArgumentParser(**kwargs)
3✔
99
    add_variant_args(parser)
3✔
100
    return parser
3✔
101

102

103
def download_and_install_reference_data(variant_collections):
3✔
UNCOV
104
    unique_genomes = {
×
105
        variant.ensembl
106
        for variant_collection in variant_collections
107
        for variant in variant_collection
108
    }
109
    for genome in unique_genomes:
×
110
        if not genome.required_local_files_exist():
×
111
            genome.download()
×
112
            genome.index()
×
113

114

115
def variant_collection_from_args(args, required=True):
3✔
116
    variant_collections = []
3✔
117

118
    for vcf_path in args.vcf:
3✔
UNCOV
119
        variant_collections.append(
×
120
            load_vcf(vcf_path, genome=args.genome))
121

122
    for maf_path in args.maf:
3✔
123
        variant_collections.append(load_maf(maf_path))
3✔
124

125
    if args.variant:
3✔
126
        if not args.genome:
3✔
UNCOV
127
            raise ValueError(
×
128
                "--genome must be specified when using --variant")
129

130
        variants = [
3✔
131
            Variant(
132
                chromosome,
133
                start=position,
134
                ref=ref,
135
                alt=alt,
136
                genome=args.genome)
137
            for (chromosome, position, ref, alt)
138
            in args.variant
139
        ]
140
        variant_collection = VariantCollection(variants)
3✔
141
        variant_collections.append(variant_collection)
3✔
142

143
    for json_path in args.json_variants:
3✔
144
        with open(json_path, 'r') as f:
×
UNCOV
145
            variant_collections.append(
×
146
                VariantCollection.from_json(f.read()))
147

148
    if required and len(variant_collections) == 0:
3✔
UNCOV
149
        raise ValueError(
×
150
            "No variants loaded (use --maf, --vcf, --variant, or --json-variants options)")
151

152
    if args.download_reference_genome_data:
3✔
153
        download_and_install_reference_data(variant_collections)
×
154

155
    # pylint: disable=no-value-for-parameter
156
    return VariantCollection.union(*variant_collections)
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc