Commit e38c7580 authored by Fabrice Allain's avatar Fabrice Allain
Browse files

Syntax update according to pylint res

parent cab261fc
......@@ -10,13 +10,14 @@ import json
import re
import ast
import sys
import shutil
import numpy as np
import pkg_resources as pkgr
import matplotlib.artist as art
from io import StringIO
logger = logging.getLogger()
LOG = logging.getLogger()
def titleprint(outfile, progname='', desc=''):
......@@ -121,11 +122,11 @@ def format_str(string):
try:
ev_str = ast.literal_eval(string)
except ValueError:
logger.error("Don't understand given string %s. Please check "
"format." % string)
LOG.error("Don't understand given string %s. Please check "
"format.", string)
return None
except SyntaxError:
logger.error("Given string %s is not a valid expression" % string)
LOG.error("Given string %s is not a valid expression", string)
return None
return ev_str
else:
......@@ -251,15 +252,23 @@ class CustomLogging:
:param outdir: path output directory
:return:
"""
outdir = os.path.join(outdir, "log") if "log" not in outdir else outdir
outdir = os.path.join(outdir,
"log") if "log" not in outdir else outdir
if not os.path.exists(os.path.abspath(outdir)):
os.makedirs(outdir)
else:
# Trick to avoid overwriting files with w mode after copy2 call
shutil.rmtree(os.path.abspath(outdir))
os.makedirs(outdir)
if outdir and "handlers" in self.config:
for hand in self.config["handlers"]:
if "filename" in self.config["handlers"][hand]:
self.config["handlers"][hand]["filename"] = \
os.path.join(outdir, os.path.basename(
self.config["handlers"][hand]["filename"]))
oldpath = self.config["handlers"][hand]["filename"]
newpath = os.path.abspath(os.path.join(
outdir, os.path.basename(
self.config["handlers"][hand]["filename"])))
self.config["handlers"][hand]["filename"] = newpath
shutil.copy2(oldpath, newpath)
logging.config.dictConfig(self.config)
def welcome(self):
......@@ -275,6 +284,11 @@ class CustomLogging:
================================================================================
'''.format(self.msg)
print(desc)
for hand in self.config.get("handlers"):
if "filename" in self.config["handlers"][hand]:
with open(self.config["handlers"][hand]["filename"],
'w') as f:
f.write(desc)
class Capturing(list):
......@@ -293,7 +307,7 @@ class Capturing(list):
"""
:return:
"""
logger.error("Error during capture [%s]" % str(args))
LOG.error("Error during capture [%s]", str(args))
self.extend("\n".join(self._stringio.getvalue().splitlines()))
sys.stdout = self._stdout
......@@ -301,6 +315,6 @@ class Capturing(list):
if __name__ == "__main__":
# Test Logger
CustomLogging().set_outdir("../examples/out")
logger = logging.getLogger("TEST")
logger.info(dir(logger))
logger.info("Log test")
LOG = logging.getLogger("TEST")
LOG.info(dir(LOG))
LOG.info("Log test")
No preview for this file type
......@@ -12,7 +12,8 @@ from .maplot import AriaEcContactMap
from .econverter import AriaEcBbConverter
from .setup import AriaEcSetup
logger = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)
def check_file(prospective_file):
......@@ -62,7 +63,7 @@ class AriaEcCommand:
parser = self._create_argparser()
# parse args
self.args = parser.parse_args()
# Update logger with outdir
# Update LOG with outdir
self._update_logger(custom_logging)
def _update_logger(self, log):
......@@ -70,9 +71,9 @@ class AriaEcCommand:
if not self.args.nolog:
# Don't generate log files
# TODO: get handler list from json file or customlogging object
logger.removeHandler("info_file_handler")
logger.removeHandler("error_file_handler")
logger.removeHandler("debug_file_handler")
LOG.removeHandler("info_file_handler")
LOG.removeHandler("error_file_handler")
LOG.removeHandler("debug_file_handler")
log.set_outdir(self.args.output_directory)
log.update_msg(self.args.command)
log.welcome()
......@@ -201,24 +202,24 @@ class AriaEcCommand:
return parser
def create_settings(self):
logger.debug("Create AriaEcSettings")
LOG.debug("Create AriaEcSettings")
settings = AriaEcSettings(self.args.command)
logger.info("Loading default config file")
LOG.info("Loading default config file")
settings.load_config(self.default_confile, pkg=True)
if self.args.conf_file:
logger.info("Updating settings with conf file")
LOG.info("Updating settings with conf file")
settings.load_config(self.args.conf_file)
# Update settings associated to command section
logger.info("Updating %s settings with args" % self.args.command)
LOG.info("Updating %s settings with args", self.args.command)
getattr(settings, self.args.command).args.update(self.args.__dict__)
if self.args.output_directory:
logger.info("Updating output directory")
LOG.info("Updating output directory")
settings.infra = self.args.output_directory
return settings
def run(self):
# call method relative to args.command
logger.info("Run %s command" % self.args.command)
LOG.info("Run %s command", self.args.command)
getattr(self, self.args.command)()
def setup(self):
......@@ -238,5 +239,5 @@ class AriaEcCommand:
if __name__ == "__main__":
# Test AriaEcCommand object
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("IO")
LOG = logging.getLogger("IO")
argparser = AriaEcCommand()
......@@ -3,10 +3,10 @@
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(levelname)-8s --- %(message)s"
"format": "%(levelname)-8s %(message)s"
},
"detail": {
"format": "[%(asctime)s] --- %(levelname)8s --- %(message)s (%(filename)s:%(lineno)s)",
"format": "[%(asctime)s] --- %(levelname)-8s --- %(message)s (%(filename)s:%(lineno)s)",
"datefmt": "%m/%d/%Y %I:%M:%S %p"
},
"colored": {
......@@ -18,7 +18,7 @@
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"level": "DEBUG",
"formatter": "colored",
"stream": "ext://sys.stdout"
},
......@@ -26,19 +26,18 @@
"info_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "detail",
"filename": "/tmp/aria_ec.log",
"maxBytes": 10485760,
"backupCount": 20,
"formatter": "simple",
"filename": "/tmp/ariaec.log",
"mode": "a",
"encoding": "utf8"
},
"debug_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "simple",
"filename": "/tmp/debug.log",
"mode": "w",
"formatter": "detail",
"filename": "/tmp/ariaec.debug",
"mode": "a",
"encoding": "utf8"
},
......@@ -46,15 +45,14 @@
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "detail",
"filename": "/tmp/errors.log",
"maxBytes": 10485760,
"backupCount": 20,
"filename": "/tmp/ariaec.error",
"mode": "a",
"encoding": "utf8"
}
},
"root": {
"level": "DEBUG",
"level": "INFO",
"handlers": ["console", "info_file_handler", "error_file_handler",
"debug_file_handler"]
}
......
......@@ -25,7 +25,7 @@ from aria.AriaXML import AriaXMLPickler
from aria.conversion import Converter, SequenceList, MoleculeSettings
logger = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)
class AriaEcBbConverter(object):
......@@ -38,10 +38,10 @@ class AriaEcBbConverter(object):
def run(self):
# Check input
logger.debug("Settings:\n" + json.dumps(self.settings.setup.config,
indent=4))
logger.debug("Args:\n" + json.dumps(self.settings.setup.args,
indent=4))
LOG.debug("Settings:\n" + json.dumps(self.settings.setup.config,
indent=4))
LOG.debug("Args:\n" + json.dumps(self.settings.setup.args,
indent=4))
# TODO: redirect print output to logging ? (low priority)
# ----------------------------- Input -------------------------------- #
self.outprefix = get_filename(self.settings.bbconv.args.get("seq",
......@@ -128,7 +128,7 @@ class AriaXMLConverter(Converter, object):
sequence.parse(self._mol_set['input'], self._mol_set['format'],
self._mol_set['naming_convention'])
# logger.info("\n" + "".join(output))
# LOG.info("\n" + "".join(output))
factory = self.create_factory()
......@@ -232,7 +232,7 @@ assign (resid {res1} and name o) (resid {res2} and name hn) 1.8 {dminus} {dplus
distmap = hbmap.get("distmap")
if n_hb:
contacts = contacts[:n_hb]
logger.debug(contacts)
LOG.debug(contacts)
donors = []
acces = []
for contact in contacts:
......@@ -265,15 +265,15 @@ assign (resid {res1} and name o) (resid {res2} and name hn) 1.8 {dminus} {dplus
raise NotImplementedError
else:
if hb_type != "main":
logger.error("Wrong longrange hbond type given. Default "
LOG.error("Wrong longrange hbond type given. Default "
"option used (main)")
# First residue = donor for a backbone hydrogen bond (N-H...O=C)
outfile.write('''\
assign (resid {res1} and name o) (resid {res2} and name n) 2.8 {dminus} {dplus}
assign (resid {res1} and name o) (resid {res2} and name hn) 1.8 {dminus} {dplus}
'''.format(res1=res1, res2=res2, dminus=dminus, dplus=dplus, dist=dist))
logger.info("Writing %d hbonds from hbmap in %s" % (len(donors),
outfile.name))
LOG.info("Writing %d hbonds from hbmap in %s", len(donors),
outfile.name)
def write_hb_tbl(self, protein, outfile, hbmap=None, dminus=0.0,
dplus=0.5, n_hb=None, lr_type='main'):
......@@ -428,7 +428,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
elif prod_type == "all":
return list(itertools.product(atms1, atms2))
else:
logger.error("Wrong pair_list option. Pair_list set to min")
LOG.error("Wrong pair_list option. Pair_list set to min")
return min_atms(res1, res2, list(itertools.product(atms1, atms2)))
def targetdistmap(self, distype, sequence, distfile=None, groupby=None):
......@@ -514,7 +514,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
nb_c)
# Initial contact list start at 0
# pair_list = [(int(x[0]) + 1, int(x[1]) + 1) for x in pair_list]
logger.info("Selecting %d contacts:\n%s" % (nb_c, pair_list))
LOG.info("Selecting %d contacts:\n%s", nb_c, pair_list)
if self.settings.setup.config['evfold_weight'] and scoremap is not None:
weight_list = list(float(10.0 / (x + 1)) for x, v in enumerate(
......@@ -544,7 +544,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
contrib_id = 0
for contactidx, contact in enumerate(pair_list):
logger.debug("Contact %s" % str(contact))
LOG.debug("Contact %s" % str(contact))
# Add neighbors if neigh_flag
resx_idx = range(min_ind(contact[0] - 1),
......@@ -616,10 +616,10 @@ class AriaEcXMLConverter(AriaXMLConverter):
# In case missing distance values
target_dist = self.settings.setup.config[
"restraint_distance"]
logger.warning(
LOG.warning(
"Target distance is missing for restraint "
"%s-%s (%s). Using default distance (%s)"
% (idx_x + 1, idx_y + 1, atm_pair, target_dist))
"%s-%s (%s). Using default distance (%s)",
idx_x + 1, idx_y + 1, atm_pair, target_dist)
rest_id += 1
contrib_id = 1
......@@ -662,7 +662,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
}
xml_file = self.settings.infra["xml"] + "/" + "_".join((
self.outprefix, listname)) + ".xml"
logger.info("Write %d xml distance restraints in %s" % (nb_c, xml_file))
LOG.info("Write %d xml distance restraints in %s", nb_c, xml_file)
self.write_dist_xml(restraint_dict, xml_file)
return xml_file, pair_list
......@@ -670,7 +670,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
out = ([], [])
for maptype in maplist:
logger.info("Writing %s ARIA XML distance restraints" % maptype)
LOG.info("Writing %s ARIA XML distance restraints", maptype)
outfile, pairlist = self.write_map_restraint(
maplist[maptype]['contactmap'], maplist[maptype]["nb_c"],
targetmap, listname=maptype,
......@@ -694,16 +694,16 @@ class AriaEcXMLConverter(AriaXMLConverter):
"_hbond.tbl")
ssdist_file = os.path.join(self.settings.infra["tbl"], self.outprefix +
"_ssdist.tbl")
logger.info(" Dihedral restraints (%s)" % dihed_file)
LOG.info(" Dihedral restraints (%s)", dihed_file)
self.write_dihedral_tbl(protein.sec_struct.ss_matrix, dihed_file)
logger.info(" Helix bond restraints (%s)" % hb_file)
LOG.info(" Helix bond restraints (%s)", hb_file)
self.write_hb_tbl(protein, hb_file,
hbmap=hbmap, n_hb=n_hb,
lr_type=self.settings.setup.config['longrange_hbtype'],
dminus=self.settings.setup.config['hb_dminus'],
dplus=self.settings.setup.config['hb_dplus'])
logger.info(" Secondary structure restraints (%s)" %
ssdist_file)
LOG.info(" Secondary structure restraints (%s)",
ssdist_file)
self.write_ssdist_tbl(protein.sec_struct.ss_matrix,
protein.sec_struct.ssdist,
ssdist_file)
......@@ -718,7 +718,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
except Exception as msg:
logger.error("Error writing xml seq file : %s" % msg)
LOG.error("Error writing xml seq file : %s", msg)
return self._mol_set['output']
......@@ -733,7 +733,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
:param desc:
:return:
"""
logger.info("Loading aria template file %s" % aria_template)
LOG.info("Loading aria template file %s", aria_template)
templatepath = os.path.abspath(aria_template)
ariaproj_template = Template(filename=templatepath,
......@@ -741,7 +741,7 @@ class AriaEcXMLConverter(AriaXMLConverter):
try:
t = open(templatepath, 'r')
except Exception, msg:
except Exception as msg:
sys.exit("""Can't open "%s" file. %s""" % (templatepath, msg))
aria_project_template = t.read()
......@@ -761,13 +761,13 @@ class AriaEcXMLConverter(AriaXMLConverter):
temp_root = os.path.abspath(aria_project_dict['temp_root'])
if not os.path.exists(work_dir):
logger.info("Working dir %s doesn't exist." % work_dir)
logger.info("Create new directory %s" % work_dir)
LOG.info("Working dir %s doesn't exist.", work_dir)
LOG.info("Create new directory %s", work_dir)
os.makedirs(work_dir)
if not os.path.exists(temp_root):
logger.info("Temp directory %s doesn't exist." % temp_root)
logger.info("Create new directory %s" % temp_root)
LOG.info("Temp directory %s doesn't exist.", temp_root)
LOG.info("Create new directory %s", temp_root)
os.makedirs(temp_root)
aria_project_dict['working_directory'] = work_dir
......@@ -834,5 +834,5 @@ class AriaEcXMLConverter(AriaXMLConverter):
proj_file = "%s/ariaproject.xml" % work_dir
with open(proj_file, 'w') as proj_xml:
logger.info("Writing ARIA project file (%s)" % proj_file)
LOG.info("Writing ARIA project file (%s)", proj_file)
proj_xml.write(ariaproj_template.render(**aria_project_dict))
......@@ -12,7 +12,7 @@ import pkg_resources as pkgr
from .base import format_dict
logger = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)
class Setting:
......@@ -21,6 +21,10 @@ class Setting:
self.config = collections.defaultdict()
self.args = collections.defaultdict()
def __repr__(self):
return "Setting object\n config: %s\n args : %s" % (self.config,
self.args)
class Settings(object):
......@@ -40,7 +44,7 @@ class Settings(object):
if os.path.exists(configpath):
self.configfile = configpath
elif not pkg:
logger.error("Configuration file not found (%s)" % configpath)
LOG.error("Configuration file not found (%s)", configpath)
return None
config = SafeConfigParser(allow_no_value=True)
if pkg:
......@@ -48,20 +52,20 @@ class Settings(object):
config.readfp(conf)
else:
config.read(configpath)
logger.debug(config)
LOG.debug(config)
for section in config.sections():
if hasattr(self, section):
tmp = format_dict(dict(config.items(section)))
getattr(self, section).config.update(tmp)
logger.debug("%s config updated" % section)
logger.debug("%s.%s : %s" % (self.__class__.__name__, section,
getattr(self, section)))
LOG.debug("%s config updated", section)
LOG.debug("%s.%s : %s", self.__class__.__name__, section,
getattr(self, section))
else:
logger.warning("Unknow config section %s" % section)
LOG.warning("Unknow config section %s", section)
def write_config(self, filename):
# Ecrit les config de toutes les sections dans un autre fichier
logger.info("Writing .ini file (%s)" % filename)
LOG.info("Writing .ini file (%s)", filename)
config = SafeConfigParser(allow_no_value=True)
iniout = open(filename, mode="w")
for section in self._sections:
......@@ -72,6 +76,9 @@ class Settings(object):
str(getattr(self, section).config.get(opt)))
config.write(iniout)
def __repr__(self):
return "<Settings object>\n sections: %s" % self._sections
class AriaEcSettings(Settings):
......@@ -97,7 +104,7 @@ class AriaEcSettings(Settings):
def _up_infra(self):
for d in self.infra:
logger.debug("set %s dir: %s" % (d, os.path.join(self.outdir, d)))
LOG.debug("set %s dir: %s", d, os.path.join(self.outdir, d))
self._infra[d] = os.path.join(self.outdir, d)
@property
......@@ -138,8 +145,8 @@ class AriaEcSettings(Settings):
if os.path.exists(pkgr.resource_filename(__name__, templatepath)):
self._template = pkgr.resource_filename(__name__, templatepath)
else:
logger.error("Template version for aria project (%s) is not "
"supported" % self.main.config.get("ariaproject_template"))
LOG.error("Template version for aria project (%s) is not "
"supported", self.main.config.get("ariaproject_template"))
self._template = pkgr.resource_filename(__name__,
self.ARIAPROJ_TEMPLATE)
return self._template
......@@ -167,9 +174,9 @@ class AriaEcSettings(Settings):
return self._scsc_min
def make_infra(self):
logger.info("Making output directories")
LOG.info("Making output directories")
for direct in self.infra:
logger.debug("Create %s directory" % self.infra[direct])
LOG.debug("Create %s directory", self.infra[direct])
if not os.path.exists(self.infra[direct]):
os.makedirs(os.path.abspath(self.infra[direct]))
......
No preview for this file type
......@@ -11,7 +11,7 @@ from .reader import ProtFileListReader
from .protmap import MapFilter
from .protein import Protein
logger = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)
class AriaEcContactMap(object):
......@@ -30,10 +30,10 @@ class AriaEcContactMap(object):
def run(self):
# Check input
logger.debug("Settings:\n" + json.dumps(self.settings.contactmap.config,
indent=4))
logger.debug("Args:\n" + json.dumps(self.settings.contactmap.args,
indent=4))
LOG.debug("Settings:\n" + json.dumps(self.settings.contactmap.config,
indent=4))
LOG.debug("Args:\n" + json.dumps(self.settings.contactmap.args,
indent=4))
if not self.settings.contactmap.args.get("onlyreport", False):
self.settings.make_infra()
# ----------------------------- Input -------------------------------- #
......@@ -65,7 +65,7 @@ class AriaEcContactMap(object):
# TODO: filter pour toutes les map de mapdict !! (fonction remove
# s'applique sur l'humanidx contenant les residus)
if idx == 0:
logger.info("%s map set as reference" % fo.filetype.capitalize())
LOG.info("%s map set as reference", fo.filetype.capitalize())
self.refmap = fo.mapdict
self.reftype = fo.filetype
self.refname = fo.filename if type(self.outprefix) != list \
......@@ -89,7 +89,7 @@ class AriaEcContactMap(object):
try:
refmap = self.refmap["contactmap"]
except TypeError:
logger.error("First contact map should be a valid file")
LOG.error("First contact map should be a valid file")
sys.exit(1)
# nb_c = int(len(self.protein.aa_sequence.sequence) *
......@@ -115,8 +115,8 @@ class AriaEcContactMap(object):
for mapname, mapt in self.allresmap.keys():
if mapt != self.reftype:
# TODO: DON'T WORK !!!!
logger.info("Merging %s with %s map" % (
mergetype, mapt))
LOG.info("Merging %s with %s map",
mergetype, mapt)
up_map = self.allresmap[mapt]["contactmap"]
up_map[:] = up_map[:] + mergecontactmap[:]
mergekey = "%s_%s" % (mapt, mergetype)
......@@ -150,8 +150,8 @@ class AriaEcContactMap(object):
cmplist = self.allresmap[(mapname, mapt, mapath)][
'contactmap'].contact_list(human_idx=True)
# else:
# logger.warning("%s map doesn't have any score related. Can't "
# "define top list related to this map" % mapt)
# LOG.warning("%s map doesn't have any score related. Can't "
# "define top list related to this map", mapt)
# continue
# TODO: only one function for output files
......@@ -183,5 +183,5 @@ class AriaEcContactMap(object):
**plotparams)
# Contingency table
# print(cmpmap.to_series())
# logger.info(pd.crosstab(cmpmap.values, refmap.values,
# LOG.info(pd.crosstab(cmpmap.values, refmap.values,
# rownames=[mapt], colnames=[self.reftype]))
......@@ -17,7 +17,7 @@ from .base import (reg_load, ppdict)
# TODO: interface skbio ??
logger = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)
class SsList:
......@@ -77,17 +77,16 @@ class SsList:
:return:
"""
self.check_filetype(filename)
logger.info("Reading secondary structure file %s [%s]" % (
filename,
self.filetype))
LOG.info("Reading secondary structure file %s [%s]",
filename, self.filetype)
# TODO: better read with getattr
if self.filetype == "indextableplus":
self.read_indextableplus(filename)
else:
self.read_psipred(filename)
logger.debug("Secondary structure matrix:\n%s\n"
"Secondary structure dict:\n%s" % (self.ss_matrix,
self.ssdict))
LOG.debug("Secondary structure matrix:\n%s\n"
"Secondary structure dict:\n%s", self.ss_matrix,
self.ssdict)
if sequence:
self.seq_sublist(sequence)
......@@ -175,7 +174,7 @@ class SsList:
:param filename:
:return:
"""
logger.info("Reading distance file {0}".format(filename))
LOG.info("Reading distance file {0}".format(filename))
c = 0
atom_list = []
for line in infile:
......@@ -208,14 +207,14 @@ class SsList:
"""