Main Program

The top level application (xo.py) uses the argparse library to parse command line arguments and then call the function that implemements the command specified on the command line.

init_cli

The default file names for the SNPs file, intervals file, and peaks file are defined in the init_cli function. First see if there is an environment variable for a file name (there will be when the program is run in a Docker container), otherwise use the predefined default.

The argument parser sets up a "subparser" for each command (peaks, gui, or vis). The subparsers define the options valid for that command and specify which function to call if the command is found on the command line. These functions are all imported from other modules.

Source

Use argparse to create the command line API.

Returns:
  • –

    a Namespace object with values of the command line arguments.

Source code in src/xo/xo.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def init_cli():
    """
    Use argparse to create the command line API.

    Returns:
        a Namespace object with values of the command line arguments. 
    """
    c = Config()

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(
        title = 'subcommands',
        description = 'operation to perform',
        dest='cmnd'
    )

    parser.add_argument('--log', metavar='X', choices=['quiet','info','debug'])

    gui_parser = subparsers.add_parser('gui', help='explore blocks and NCOs')
    gui_parser.add_argument('--peaks', metavar='F', default=c.peaks_output_default, help='blocks saved by peaks.py')
    # gui_parser.add_argument('--filtered', metavar='F', default=c.filter_output_default, help='filtered blocks')
    # gui_parser.add_argument('--ncos', metavar='F', default=c.post_output_default, help='NCOs')
    gui_parser.add_argument('--port', metavar='N', type=int, default=5006, help='local port for the Panel server')
    gui_parser.set_defaults(func=start_app)

    peak_parser = subparsers.add_parser('peaks', help='find blocks around peaks in the SNP data')
    peak_parser.add_argument('--snps', metavar='F', default=c.snps_default, help='input (TIGER marker) file')
    peak_parser.add_argument('--crossovers', metavar='F', default=c.crossovers_default, help='file with crossover locations')
    peak_parser.add_argument('--output', metavar='F', default=c.peaks_output_default, help='output file')
    peak_parser.add_argument('--max_snps', metavar='N', type=int, default=c.peaks_max_snps, help="max number of SNPs in a block")
    peak_parser.set_defaults(func=peak_finder)

    filter_parser = subparsers.add_parser('filter', help='apply filters to blocks')
    filter_parser.add_argument('--peaks', metavar='F', default=c.peaks_output_default, help='blocks saved by peaks.py')
    filter_parser.add_argument('--output', metavar='F', default=c.filter_output_default, help='output file')
    filter_parser.add_argument('--crossovers', metavar='F', default=c.crossovers_default, help='file with crossover locations')
    filter_parser.add_argument('--chromosomes', metavar='P', default=c.filter_chromosome_pattern, help='chromosome name pattern')
    filter_parser.add_argument('--size', metavar='N', nargs=2, type=int, default=c.filter_block_size, help='block size range (#SNPs)')
    filter_parser.add_argument('--length', metavar='N', nargs=2, type=int, default=c.filter_block_length, help='block length range (bp)')
    filter_parser.add_argument('--coverage', metavar='N', type=int, default=c.filter_coverage, help='minimum coverage')
    filter_parser.add_argument('--match', action='store_true', help='require genome match')
    filter_parser.set_defaults(func=filter_blocks)

    post_parser = subparsers.add_parser('post', help='postprocessing of filtered blocks')
    post_parser.add_argument('--blocks', metavar='F', default=c.filter_output_default, help='file with filtered blocks')
    post_parser.add_argument('--output', metavar='F', default=c.post_output_default, help='output file')
    post_parser.add_argument('--min_z', metavar='N', type=float, default=c.post_min_z, help='homozygosity for Type A blocks')
    post_parser.add_argument('--delta_z', metavar='N', type=float, default=c.post_delta_z, help='homozygosity for Type B blocks')
    post_parser.add_argument('--min_cover', metavar='N', type=int, default=c.post_min_cover, help='minimum number of reads of each type')
    post_parser.add_argument('--size', metavar='N', type=int, default=c.post_block_size, help='minimum block size (bp)')
    post_parser.set_defaults(func=postprocess)

    if len(sys.argv) == 1:
        parser.print_help()
        exit()

    return parser.parse_args()

main

The argument parser associates a function with each script name, all we need to do here is call that function.

Source code in src/xo/xo.py
188
189
190
191
192
193
194
195
196
197
198
def main():
    """
    The argument parser associates a function with each script name, all
    we need to do here is call that function.
    """
    args = init_cli()
    setup_logging(args)
    try:
        args.func(args)
    except Exception as err:
        logging.exception(err)