commands.py 16.5 KB
Newer Older
1
# coding=utf-8
fabrice's avatar
fabrice committed
2
3
4
"""
                            Input/Output aria_ec
"""
5
from __future__ import absolute_import, division, print_function
fabrice's avatar
fabrice committed
6
7
8
9
10

import os
import logging
import argparse as argp

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
11
from . import __doc__
12
from .base import format_dict, CustomLogging
13
from .settings import AriaEcSettings
fabrice's avatar
fabrice committed
14
from .maplot import AriaEcContactMap
15
from .converter import AriaEcBbConverter, AriaEcXMLConverter
fabrice's avatar
fabrice committed
16
from .setup import AriaEcSetup
Fabrice Allain's avatar
Fabrice Allain committed
17
from .pdbqual import AriaEcPdbqual
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
18
from .pdbdist import PDBDist
19
from .analysis import EnsembleAnalysis
fabrice's avatar
fabrice committed
20

21
22

LOG = logging.getLogger(__name__)
fabrice's avatar
fabrice committed
23
24
25


def check_file(prospective_file):
26
    """
fabrice's avatar
fabrice committed
27

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
28
29
30
31
32
33
34
35
36
    Parameters
    ----------
    prospective_file :
        

    Returns
    -------

    
37
    """
38
    LOG.debug("Checking if %s is a readable file", prospective_file)
fabrice's avatar
fabrice committed
39
40
41
42
43
44
45
46
47
    if not os.path.exists(prospective_file):
        raise argp.ArgumentTypeError("readable_file:'{0}' is not a valid "
                                     "path".format(prospective_file))
    if not os.access(prospective_file, os.R_OK):
        raise argp.ArgumentTypeError("readable_file:'{0}' is not a readable "
                                     "file".format(prospective_file))


class ReadableFile(argp.Action):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
48
    """Class used with argparse action to check if a file is readable"""
fabrice's avatar
fabrice committed
49

Fabrice Allain's avatar
Fabrice Allain committed
50
51
52
    def __init__(self, *args, **kwargs):
        super(ReadableFile, self).__init__(*args, **kwargs)

fabrice's avatar
fabrice committed
53
    def __call__(self, parser, namespace, values, option_string=None):
54
        if isinstance(values, list):
fabrice's avatar
fabrice committed
55
56
            for prospective_file in values:
                check_file(prospective_file)
Fabrice Allain's avatar
Fabrice Allain committed
57
58
            setattr(namespace, self.dest,
                    [os.path.abspath(os.path.expanduser(val)) for val in values])
59
        elif isinstance(values, str):
fabrice's avatar
fabrice committed
60
            check_file(values)
Fabrice Allain's avatar
Fabrice Allain committed
61
            setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))
fabrice's avatar
fabrice committed
62
63
64
65


# TODO: Make parent Command class with _create_argparser, self.args,
# update_logger and run
66
class AriaEcCommands(object):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
67
    """Argparse interface for aria_ec"""
fabrice's avatar
fabrice committed
68

69
    command_list = ("setup", "bbconv", "maplot", "pdbqual", "analysis",
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
70
                    "tbl2xml", "pdbdist")
71
    desc_list = (u"Setup ARIA infrastructure with contact maps translated "
fabrice's avatar
fabrice committed
72
                 u"into ARIA restraints",
73
74
                 u"Convert a contact map in bbcontact format",
                 u"Contactmap visualization tool",
75
                 u"Quality tool for pdb file(s)",
76
                 u"Extended ARIA ensemble analysis on a specific iteration ",
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
77
78
79
80
81
82
                 u"XML converter for tbl distance restraint",
                 u"Extract distance distribution from culled list of pdb files")
    contact_types = ("evfold", "plmev", "plm", "plmdca", "plmc", "bbcontacts",
                     "pconsc", "pconsc1", "pconsc2", "psicov", "metapsicovhb",
                     "metapsicov_stg1", "metapsicov_stg2", "gremlin", "pdb",
                     "native", "native_full", "contactlist")
83
    default_confile = "conf/config.ini"
fabrice's avatar
fabrice committed
84
85

    def __init__(self, custom_logging=None):
86
        # Def Argument pdbparser
fabrice's avatar
fabrice committed
87
88
89
        parser = self._create_argparser()
        # parse args
        self.args = parser.parse_args()
90
        # Update LOG with outdir
fabrice's avatar
fabrice committed
91
92
93
        self._update_logger(custom_logging)

    def _update_logger(self, log):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
94
95
96
97
98
99
100
101
102
103
104
105
        """

        Parameters
        ----------
        log :
            

        Returns
        -------

        
        """
fabrice's avatar
fabrice committed
106
        if log and hasattr(self.args, "output_directory"):
fabrice's avatar
fabrice committed
107
108
109
            if not self.args.nolog:
                # Don't generate log files
                # TODO: get handler list from json file or customlogging object
110
111
112
                LOG.removeHandler("info_file_handler")
                LOG.removeHandler("error_file_handler")
                LOG.removeHandler("debug_file_handler")
fabrice's avatar
fabrice committed
113
                log.set_outdir(self.args.output_directory)
fabrice's avatar
fabrice committed
114
115
116
117
            log.update_msg(self.args.command)
            log.welcome()

    def _create_argparser(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
118
        """ """
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
119
        parser = argp.ArgumentParser(description=__doc__,
fabrice's avatar
fabrice committed
120
121
                                     formatter_class=argp.ArgumentDefaultsHelpFormatter)
        parser.add_argument("-o", "--output", dest="output_directory",
122
                            type=str, help="Output directory", required=True)
123
124
        parser.add_argument("-c", "--conf", action=ReadableFile,
                            dest="conf_file",
fabrice's avatar
fabrice committed
125
                            default=None, help="configuration file")
fabrice's avatar
fabrice committed
126
127
        parser.add_argument("--nolog", action="store_true",
                            default=False, help="Don't generate log files")
128
        parser.add_argument("-d", "--debug", dest="verbose", default=False,
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
129
130
                            action='store_true',
                            help="Increase output verbosity")
fabrice's avatar
fabrice committed
131
132
133
134
135
        # Create subcommands
        self._create_subparsers(parser.add_subparsers(dest="command"))
        return parser

    def _create_subparsers(self, parser):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
136
137
138
139
140
141
142
143
144
145
146
        """Generate subcommands

        Parameters
        ----------
        parser :
            argparser object

        Returns
        -------

        
fabrice's avatar
fabrice committed
147
148
149
        """
        for index, command in enumerate(self.command_list):
            # Create subparser defined in command list
150
            subcommand = getattr(self, "_" + command + "_argparser")(
151
                desc=self.desc_list[index])
fabrice's avatar
fabrice committed
152
153
            parser.add_parser(command, parents=[subcommand])

154
    def _setup_argparser(self, desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
155
156
157
158
159
160
161
162
163
164
165
        """setup opt & args

        Parameters
        ----------
        desc :
            command descriptor (Default value = None)

        Returns
        -------

        
fabrice's avatar
fabrice committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        """
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
        # Options
        # Args
        group = parser.add_argument_group('required arguments')
        group.add_argument("seq", action=ReadableFile,
                           help="sequence file [FASTA]")
        group.add_argument("sspred", action=ReadableFile,
                           help="secondary structure prediction file")
        group.add_argument("infiles", nargs="+", metavar="infile",
                           action=ReadableFile,
                           help="contact or pdb file(s) used to build aria "
                                "distance restraints")
        group.add_argument("-d", "--distfile", dest="distfile",
                           help="Pdb or distance matrix iif distance_type "
                                "set  to   distfile in conf  file,  "
                                "use distances in the  given file as "
                                "target  distance to build  distance "
                                "restraints")
        group.add_argument("-t", "--type", required=True,
                           nargs="+", dest="contact_types",
                           choices=self.contact_types, help="Infile(s) contact "
                                                            "type(s)")
        group.add_argument("-r", "--ref", dest="ref",
                           help="Native pdb. Allow TP/FP detection.")
        group.add_argument("--hb", dest="hb",
                           help="H-bonds contact file (eg: metapsicov.hb)")
        group.add_argument("--ssidx", dest="ssidx", action="store_true",
                           default=False, help="Use secondary structure index")
196
197
        group.add_argument("--no-filter", dest="no_filter", action="store_true",
                           default=False, help="Do not filter contact map.")
fabrice's avatar
fabrice committed
198
199
        return parser

200
    def _bbconv_argparser(self, desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
201
202
203
204
205
206
207
208
209
210
211
        """bbconv opt & args

        Parameters
        ----------
        desc :
            command descriptor (Default value = None)

        Returns
        -------

        
fabrice's avatar
fabrice committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        """
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
        # args
        parser.add_argument("contactfile", help="contacts file (pconsc, plm)",
                            action=ReadableFile)
        parser.add_argument("sspred", help="psipred file",
                            action=ReadableFile)
        parser.add_argument("seq", help="sequence file [FASTA]",
                            action=ReadableFile)
        parser.add_argument("msa", nargs='?',
                            help="MSA [FASTA] for diversityvalue"
                                 "used with bbcontacts")
        parser.add_argument("-t", "--type", required=True, dest="contact_type",
                            choices=self.contact_types, help="Infile contact "
                                                             "type")
        return parser

230
    def _maplot_argparser(self, desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
231
232
233
234
235
236
237
238
239
240
241
        """maplot opt & args

        Parameters
        ----------
        desc :
            command descriptor (Default value = None)

        Returns
        -------

        
fabrice's avatar
fabrice committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
        """
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
        parser.add_argument("seq", action=ReadableFile,
                            help="sequence file [FASTA]")
        parser.add_argument("sspred", action=ReadableFile,
                            help="secondary structure prediction file")
        parser.add_argument("infiles", nargs="+", metavar="infile",
                            action=ReadableFile,
                            help="contact or pdb file(s) used to build aria "
                                 "distance restraints")
        parser.add_argument("-t", "--type", required=True,
                            nargs="+", dest="contact_types",
                            choices=self.contact_types, help="Infile(s) "
                                                             "contact "
                                                             "type(s)")
fabrice's avatar
fabrice committed
258
259
260
        parser.add_argument("--merge", nargs="+", dest="merge",
                            choices=self.contact_types,
                            help="Merge given contact types with other maps")
261
        parser.add_argument("--filter", dest="filter", action="store_true",
262
263
                            default=False, help="Use contact list filter "
                                                "and top n contacts selection")
fabrice's avatar
fabrice committed
264
265
266
        parser.add_argument("--onlyreport", dest="onlyreport",
                            action="store_true",
                            default=False, help="Generate only report file")
fabrice's avatar
fabrice committed
267
268
269
        parser.add_argument("--ssidx", dest="ssidx", action="store_true",
                            default=False,
                            help="Use secondary structure index")
fabrice's avatar
fabrice committed
270
        parser.add_argument("--prefix", dest="prefix", default="",
271
                            help="Contact map name", nargs="+")
fabrice's avatar
fabrice committed
272
273
        return parser

Fabrice Allain's avatar
Fabrice Allain committed
274
    @staticmethod
275
    def _pdbqual_argparser(desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
276
277
278
279
280
281
282
283
284
285
286
287
        """

        Parameters
        ----------
        desc :
            (Default value = None)

        Returns
        -------

        
        """
Fabrice Allain's avatar
Fabrice Allain committed
288
289
290
291
292
293
294
295
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
        parser.add_argument("infiles", nargs="+", metavar="infile",
                            action=ReadableFile,
                            help="PDB file(s) used to run quality tools with "
                                 "aria API")
        return parser

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
296
    @staticmethod
297
    def _analysis_argparser(desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
298
299
300
301
302
303
304
305
306
307
308
309
        """

        Parameters
        ----------
        desc :
            (Default value = None)

        Returns
        -------

        
        """
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
310
311
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
312
313
314
        parser.add_argument("project", action=ReadableFile,
                            help="ARIA project file [XML]")
        parser.add_argument("iteration", metavar="iteration_path",
315
                            action=ReadableFile,
316
                            help="Iteration path used to run violation analysis")
317
318
319
        parser.add_argument(
            "listname", metavar="list_name",
            help="Name of restraint list analyzed in the tbl file")
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
320
321
322
323
        parser.add_argument("-r", "--ref", dest="ref",
                            help="Native pdb. Allow TP/FP detection.")
        return parser

324
325
    @staticmethod
    def _tbl2xml_argparser(desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
326
327
328
329
330
331
332
333
334
335
336
337
        """

        Parameters
        ----------
        desc :
            (Default value = None)

        Returns
        -------

        
        """
338
339
340
341
342
343
344
345
346
347
348
349
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
        parser.add_argument(
            "molecule", metavar="molecule.xml", action=ReadableFile,
            help="ARIA XML molecule file")
        parser.add_argument("listname", metavar="list_name",
                            help="Restraint list name in the tbl file")
        parser.add_argument("infiles", nargs="+", metavar="infile.tbl",
                            action=ReadableFile,
                            help="TBL distance restraint file(s)")
        return parser

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
350
351
    @staticmethod
    def _pdbdist_argparser(desc=None):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
352
353
354
355
356
357
358
359
360
361
362
363
        """

        Parameters
        ----------
        desc :
            (Default value = None)

        Returns
        -------

        
        """
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
364
365
        parser = argp.ArgumentParser(description=desc,
                                     add_help=False)
366
        # TODO: find a way to list all cullpdb file in package ressources
367
368
369
370
        # Maybe move this args to the config file

        # Culled pdb list are actually saved in data/cullpdb folder with yymmdd
        # pattern as naming convention
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
371
        parser.add_argument(
372
            "--cullist", dest="cullpdbs", default='',
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
373
            metavar="CULLED_PDB_LIST", action=ReadableFile,
374
375
376
            help="Culled list of pdb files from PISCES server ["
                 "G. Wang and R. L. Dunbrack, Jr. PISCES: a protein sequence "
                 "culling server. Bioinformatics, 19:1589-1591, 2003.]")
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
377
        parser.add_argument(
378
            "--pdbdir", dest="pdbdir", default='',
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
379
380
381
382
            metavar="PDB_FOLDER",
            help="Folder containing pdb file entries")
        return parser

fabrice's avatar
fabrice committed
383
    def create_settings(self):
384
385
        """Create settings relative to args.command"""

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
386
        if self.args.verbose:
Fabrice Allain's avatar
Fabrice Allain committed
387
388
            LOG.info("Toggle on debug mode")
            logging.getLogger().setLevel(logging.DEBUG)
389
        LOG.debug("Create AriaEcSettings")
390
        settings = AriaEcSettings(self.args.command)
391
        LOG.info("Loading default config file")
fabrice's avatar
fabrice committed
392
393
        settings.load_config(self.default_confile, pkg=True)
        if self.args.conf_file:
394
            LOG.info("Updating settings with conf file")
fabrice's avatar
fabrice committed
395
396
            settings.load_config(self.args.conf_file)
        # Update settings associated to command section
397
        LOG.info("Updating %s args settings", self.args.command)
398
        LOG.debug(self.args.__dict__)
399
400
        getattr(settings, self.args.command).args.update(format_dict(self.args.__dict__))
        LOG.debug(getattr(settings, self.args.command).args)
fabrice's avatar
fabrice committed
401
        if self.args.output_directory:
402
            LOG.info("Updating output directory %s", self.args.output_directory)
fabrice's avatar
fabrice committed
403
404
405
406
            settings.infra = self.args.output_directory
        return settings

    def run(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
407
        """call method relative to args.command"""
408
        LOG.info("Run %s command", self.args.command)
fabrice's avatar
fabrice committed
409
410
411
        getattr(self, self.args.command)()

    def setup(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
412
        """Setup call"""
fabrice's avatar
fabrice committed
413
414
415
416
        setup_inst = AriaEcSetup(self.create_settings())
        setup_inst.run()

    def bbconv(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
417
        """bbcontacts converter call"""
fabrice's avatar
fabrice committed
418
419
420
        bbconverter = AriaEcBbConverter(self.create_settings())
        bbconverter.run()

421
    def maplot(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
422
        """instantiate AriaEcContactmap with AriaSettings"""
fabrice's avatar
fabrice committed
423
424
425
        econtactmap = AriaEcContactMap(self.create_settings())
        econtactmap.run()

Fabrice Allain's avatar
Fabrice Allain committed
426
    def pdbqual(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
427
        """Quality run subcommand"""
Fabrice Allain's avatar
Fabrice Allain committed
428
429
430
        qualprot = AriaEcPdbqual(self.create_settings())
        qualprot.run()

431
    def tbl2xml(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
432
        """tbl2xml command"""
433
434
        tblconverter = AriaEcXMLConverter(self.create_settings())
        tblconverter.run_tbl2xml()
fabrice's avatar
fabrice committed
435

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
436
    def pdbdist(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
437
        """Extract pdb distance distributions"""
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
438
439
440
        inst = PDBDist(self.create_settings())
        inst.run()

441
    def analysis(self):
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
442
        """Extended ensemble analysis of distance restraints"""
443
        inst = EnsembleAnalysis(self.create_settings())
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
444
445
        inst.run()

446

447
def main():
Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
448
    """Launch ariaec command interface"""
449

Fabrice  ALLAIN's avatar
Fabrice ALLAIN committed
450
    mylog = CustomLogging(desc=__doc__)
451

452
    command = AriaEcCommands(custom_logging=mylog)
453
454
455
456

    command.run()


fabrice's avatar
fabrice committed
457
458
459
if __name__ == "__main__":
    # Test AriaEcCommand object
    logging.basicConfig(level=logging.DEBUG)
460
    LOG = logging.getLogger("IO")
461
    AriaEcCommands()