Commit 603f202a authored by Fabrice Allain's avatar Fabrice Allain

Bug revision: dealing with empty contactlist file in maplot

parent 9c17de17
# coding=utf-8
"""
Input/Output aria_ec
"""
......@@ -17,7 +18,10 @@ LOG = logging.getLogger(__name__)
def check_file(prospective_file):
"""
:param prospective_file:
"""
if not os.path.exists(prospective_file):
raise argp.ArgumentTypeError("readable_file:'{0}' is not a valid "
"path".format(prospective_file))
......@@ -27,6 +31,9 @@ def check_file(prospective_file):
class ReadableFile(argp.Action):
"""
Class used with argparse action to check if a file is readable
"""
def __init__(self, *args, **kwargs):
super(ReadableFile, self).__init__(*args, **kwargs)
......@@ -42,7 +49,7 @@ class ReadableFile(argp.Action):
# TODO: Make parent Command class with _create_argparser, self.args,
# update_logger and run
class AriaEcCommand:
class AriaEcCommand(object):
"""
Argparse interface for aria_ec
"""
......@@ -84,7 +91,8 @@ class AriaEcCommand:
formatter_class=argp.ArgumentDefaultsHelpFormatter)
parser.add_argument("-o", "--output", dest="output_directory",
type=str, help="Output directory", required=True)
parser.add_argument("-c", "--conf", action=ReadableFile, dest="conf_file",
parser.add_argument("-c", "--conf", action=ReadableFile,
dest="conf_file",
default=None, help="configuration file")
parser.add_argument("--nolog", action="store_true",
default=False, help="Don't generate log files")
......@@ -100,8 +108,9 @@ class AriaEcCommand:
"""
for index, command in enumerate(self.command_list):
# Create subparser defined in command list
subcommand = getattr(self, "_" + command + "_parser")(desc=self.desc_list[
index])
subcommand = getattr(self, "_" + command + "_parser")(
desc=self.desc_list[
index])
parser.add_parser(command, parents=[subcommand])
def _setup_parser(self, desc=None):
......@@ -202,6 +211,10 @@ class AriaEcCommand:
return parser
def create_settings(self):
"""
create settings relative to args.command
:return:
"""
LOG.debug("Create AriaEcSettings")
settings = AriaEcSettings(self.args.command)
LOG.info("Loading default config file")
......@@ -218,20 +231,30 @@ class AriaEcCommand:
return settings
def run(self):
# call method relative to args.command
"""
call method relative to args.command
"""
LOG.info("Run %s command", self.args.command)
getattr(self, self.args.command)()
def setup(self):
"""
Setup call
"""
setup_inst = AriaEcSetup(self.create_settings())
setup_inst.run()
def bbconv(self):
"""
bbcontacts converter call
"""
bbconverter = AriaEcBbConverter(self.create_settings())
bbconverter.run()
def contactmap(self):
# instantiate AriaEcContactmap with AriaSettings
"""
instantiate AriaEcContactmap with AriaSettings
"""
econtactmap = AriaEcContactMap(self.create_settings())
econtactmap.run()
......
No preview for this file type
No preview for this file type
# coding=utf-8
"""
Settings section
"""
from __future__ import absolute_import, division, print_function
import logging
import os
from configparser import SafeConfigParser
import collections
import logging
import pickle
import collections
import pkg_resources as pkgr
# noinspection PyCompatibility
from ConfigParser import ConfigParser
from .base import format_dict
LOG = logging.getLogger(__name__)
class Setting:
class Setting(object):
"""
Main setting object with args and config section
"""
def __init__(self):
self.config = collections.defaultdict()
......@@ -27,6 +32,9 @@ class Setting:
class Settings(object):
"""
Group settings with each section corresponding to a Setting object
"""
def __init__(self, sections):
self._sections = set(sections)
......@@ -46,7 +54,8 @@ class Settings(object):
elif not pkg:
LOG.error("Configuration file not found (%s)", configpath)
return None
config = SafeConfigParser(allow_no_value=True)
# config = SafeConfigParser(allow_no_value=True)
config = ConfigParser(allow_no_value=True)
if pkg:
with pkgr.resource_stream(__name__, configpath) as conf:
config.readfp(conf)
......@@ -64,9 +73,12 @@ class Settings(object):
LOG.warning("Unknow config section %s", section)
def write_config(self, filename):
# Ecrit les config de toutes les sections dans un autre fichier
"""
Write config of all sections into another file
:param filename:
"""
LOG.info("Writing .ini file (%s)", filename)
config = SafeConfigParser(allow_no_value=True)
config = ConfigParser(allow_no_value=True)
iniout = open(filename, mode="w")
for section in self._sections:
config.add_section(section)
......@@ -81,20 +93,26 @@ class Settings(object):
class AriaEcSettings(Settings):
# ss_dist = os.path.join(os.path.dirname(os.path.realpath(__file__)),
# 'conf/ss_dist.txt')
# TODO: move these constant variable in objects which can read these file !!
# TODO: Baseclass inspired from this class and ariabase class. All
# objects in this package should extend the base object
"""
Settings object for ariaec
"""
ARIAPROJ_TEMPLATE = 'templates/aria_project_v2.3.0.xml'
SS_DIST = 'data/ss_dist.txt'
SCSC_MIN = 'data/scsc_min.p'
TOPO = 'data/topallhdg5.3.pro'
COMMANDS = ("main", "setup", "contactmap", "bbconv", "contactdef")
def __init__(self, name):
super(AriaEcSettings, self).__init__(("main", "setup", "contactmap",
"bbconv", "contactdef"))
"""
Initiate settings with name related to a command
:param name:
"""
super(AriaEcSettings, self).__init__(self.COMMANDS)
self._infra = {}
self._scsc_min = None
self._ssdist = None
......@@ -109,6 +127,10 @@ class AriaEcSettings(Settings):
@property
def infra(self):
"""
Infrastructure for a specific command
:return:
"""
if self.name == "setup" and not self._infra:
self._infra = {"xml": "", "tbl": "", "others": ""}
self._up_infra()
......@@ -130,6 +152,11 @@ class AriaEcSettings(Settings):
@property
def ssdist(self):
"""
Get distance file for secondary structures in the package or in config
file
:return:
"""
if not self._ssdist:
if self.main.config["ss_dist_file"] and \
os.path.exists(self.main.config["ss_dist_file"]):
......@@ -140,20 +167,28 @@ class AriaEcSettings(Settings):
@property
def template(self):
"""
Get template files in config file or in the package
:return:
"""
if not self._template:
templatepath = "templates/aria_project_v%s.xml" % str(self.main.config["ariaproject_template"])
templatepath = "templates/aria_project_v%s.xml" % \
str(self.main.config["ariaproject_template"])
if os.path.exists(pkgr.resource_filename(__name__, templatepath)):
self._template = pkgr.resource_filename(__name__, templatepath)
else:
LOG.error("Template version for aria project (%s) is not "
"supported", self.main.config.get("ariaproject_template"))
"supported", self.main.config.get("ariaproject_template"))
self._template = pkgr.resource_filename(__name__,
self.ARIAPROJ_TEMPLATE)
return self._template
@property
def scsc_min(self):
# If scsc_min already computed
"""
Get contact index for side chains in package or config file
:return:
"""
if not self._scsc_min:
try:
# Read scsc_min_file given in aria_ec.ini
......@@ -174,6 +209,9 @@ class AriaEcSettings(Settings):
return self._scsc_min
def make_infra(self):
"""
Generate infrastructure
"""
LOG.info("Making output directories")
for direct in self.infra:
LOG.debug("Create %s directory", self.infra[direct])
......@@ -181,4 +219,9 @@ class AriaEcSettings(Settings):
os.makedirs(os.path.abspath(self.infra[direct]))
def load_config(self, configpath, **kwargs):
"""
:param configpath:
:param kwargs:
"""
super(AriaEcSettings, self).load_config(configpath, **kwargs)
No preview for this file type
# coding=utf-8
"""
Input/Output aria_ec scripts
"""
......@@ -15,6 +16,10 @@ LOG = logging.getLogger(__name__)
class AriaEcContactMap(object):
"""
Contact maplot class
"""
def __init__(self, settings):
# TODO: check_type settings (AriaEcSettings)
self.settings = settings
......@@ -30,6 +35,9 @@ class AriaEcContactMap(object):
def run(self):
# Check input
"""
Main method
"""
LOG.debug("Settings:\n" + json.dumps(self.settings.contactmap.config,
indent=4))
LOG.debug("Args:\n" + json.dumps(self.settings.contactmap.args,
......@@ -128,7 +136,7 @@ class AriaEcContactMap(object):
prefix = "_".join((mapname, self.refname)).replace(".", "_")
if mapname == self.refname:
if not self.settings.contactmap.args.get("onlyreport", False):
if self.settings.contactmap.args.get("onlyreport", False) is not False:
refmap.write_contacts(mapname,
outdir=outdir,
scoremap=self.refmap.get("scoremap",
......
No preview for this file type
This diff is collapsed.
No preview for this file type
This diff is collapsed.
No preview for this file type
# coding=utf-8
"""
Input/Output aria_ec scripts
"""
from __future__ import absolute_import, division, print_function
import logging
import json
import os
import json
import logging
from .protein import Protein
from .reader import ProtFileListReader
......@@ -14,15 +15,17 @@ from .protmap import MapFilter
from .econverter import AriaEcXMLConverter
# TODO: S'inspirer de pandas/__init__.py pour les dependances
# from basictools import *
LOG = logging.getLogger(__name__)
class AriaEcSetup:
class AriaEcSetup(object):
"""
Aria Ec Setup protocol
"""
def __init__(self, settings):
"""
:param settings:
:return:
"""
......@@ -40,7 +43,7 @@ class AriaEcSetup:
def run(self):
"""
main method
:return:
"""
# Check input
......@@ -70,21 +73,24 @@ class AriaEcSetup:
# TODO: change read method in reader to __call__
# -------------------------- contact maps ---------------------------- #
self.reader.read(self.settings.setup.args.get("infiles"),
filetypelist=self.settings.setup.args.get("contact_types"),
filetypelist=self.settings.setup.args.get(
"contact_types"),
protein=self.protein,
groupby_method=self.settings.setup.config['groupby_method'],
groupby_method=self.settings.setup.config[
'groupby_method'],
scsc=self.settings.scsc_min)
for fo in self.reader.filelist:
for mapfile in self.reader.filelist:
# fo need a contactmap in order to wite XML dist restraints
# TODO: filter pour toutes les map de mapdict !! (fonction remove
# s'applique sur l'humanidx contenant les residus)
self.filter(fo.mapdict, fo.filetype, fo.contactlist, self.protein,
clashlist=fo.clashlist, outprefix=self.outprefix,
self.filter(mapfile.mapdict, mapfile.filetype, mapfile.contactlist,
self.protein, clashlist=mapfile.clashlist,
outprefix=self.outprefix,
outdir=self.settings.infra.get("others", ''))
self.allresmap[fo.filetype] = fo.mapdict
self.allresmap[mapfile.filetype] = mapfile.mapdict
if fo.filetype != "pdb" and "pdb" in self.allresmap:
fo.contactmap.compareplot(self.allresmap["pdb"])
if mapfile.filetype != "pdb" and "pdb" in self.allresmap:
mapfile.contactmap.compareplot(self.allresmap["pdb"])
# ---------------------------- target map ---------------------------- #
if self.settings.setup.args.get("distfile") and \
......@@ -138,9 +144,11 @@ class AriaEcSetup:
# ------------------------------ Output ------------------------------ #
# ----------------------------- SEQ file ----------------------------- #
self.protein.write_seq(os.path.join(self.settings.infra.get("others", ''),
self.outprefix + ".seq"))
# Load aria molecule object from generated seq file
self.protein.write_seq(
os.path.join(self.settings.infra.get("others", ''),
self.outprefix + ".seq"))
# Load aria molecule object from seq file and convert it into xml format
LOG.info("Load molecule file and convert it into xml format")
self.converter.load_molecule(self.protein.seqfile_path)
# --------------------------- TBL restraints ------------------------- #
# Setting contact number limit for hbmap
......@@ -152,11 +160,12 @@ class AriaEcSetup:
# --------------------------- XML restraints ------------------------- #
# Setting contact number limit for map restraints (native, ec, ...)
# nb_c = int(len(self.protein.aa_sequence.sequence) *
# self.settings.setup.config.get("n_factor"))
dist_files, pair_lists = self.converter.write_maplist_restraints(
self.allresmap, self.targetmap)
# dist_files, pair_lists = self.converter.write_maplist_restraints(
# self.allresmap, self.targetmap)
dist_files = self.converter.write_maplist_restraints(
self.allresmap, self.targetmap)[0]
# --------------------------- XML SEQ file --------------------------- #
xmlseq_file = self.converter.write_xmlseq()
......@@ -169,7 +178,7 @@ class AriaEcSetup:
def write_optional_files(self):
"""
Write filtered contacts & distance maps (.csv)
:return:
"""
# Indextableplus file (submatrix)
......@@ -184,16 +193,18 @@ class AriaEcSetup:
"%s/%s.distmap.csv" % (self.settings.infra.get("others"),
maptype))
if self.refmaps:
self._write_contacts(self.allresmap[maptype].get(
"filteredlist", None), self.protein.aa_sequence.sequence,
self.settings.infra.get("others", ''), "_".join((
self.outprefix, maptype, "filtered")),
self._write_contacts(
self.allresmap[maptype].get("filteredlist", None),
self.protein.aa_sequence.sequence,
self.settings.infra.get("others", ''),
"_".join((self.outprefix, maptype, "filtered")),
ref=self.refmaps["contactmap"],
distmap=self.refmaps["distmap"])
if self.refmaps:
self.refmaps["alldistmap"].to_csv(
"%s/%s_%s.distmap.csv" % (self.settings.infra.get("others"),
self.outprefix, self.refmaps.filetype))
self.outprefix,
self.refmaps.filetype))
def _write_contacts(self, contacts_list, seq, out, prefix, nc=None,
append=False, ref=None, distmap=None):
......@@ -209,7 +220,7 @@ class AriaEcSetup:
:return:
"""
mapy = []
tp = 0
tp_count = 0
filemode = 'a' if append else 'w'
dist_desc = '\tTP/FP\tdist%s(ref)' % self.settings.setup.config[
"groupby_method"] if \
......@@ -217,10 +228,12 @@ class AriaEcSetup:
else ''
with open("%s/%s.contacts.txt" % (out, prefix), filemode) as outfile:
if not append:
outfile.write('''# resid1\tresid2\tres1\tres2%s\n''' % dist_desc)
outfile.write(
'''# resid1\tresid2\tres1\tres2%s\n''' % dist_desc)
if type(contacts_list) is dict:
contacts = contacts_list.keys() if not nc else contacts_list.keys()[:nc]
if hasattr(contacts_list, 'keys'):
contacts = contacts_list.keys() if not nc else \
contacts_list.keys()[:nc]
d_type = True
else:
# Check if contacts is 2-tuple
......@@ -228,6 +241,7 @@ class AriaEcSetup:
raise TypeError('Contact list must be 2-tuple !')
contacts = contacts_list if not nc else contacts_list[:nc]
d_type = False
LOG.debug("Contact list %s", contacts)
for contact in contacts:
......@@ -250,7 +264,7 @@ class AriaEcSetup:
if ref is not None:
if ref.ix[(resid1, resid2)]:
asses = 'TP'
tp += 1
tp_count += 1
else:
asses = 'FP'
outfile.write(
......@@ -265,12 +279,12 @@ class AriaEcSetup:
seq[resid1],
seq[resid2]))
ptp = (tp / float(len(contacts))) * 100.0 if ref is not None else \
ptp = (tp_count / float(len(contacts))) * 100.0 if ref is not None else \
None
outfile.write('''
# TP number : {tp} ({ptp:.2f} %)
# Number of contacts : {nc}
'''.format(tp=tp, ptp=ptp, nc=len(contacts)))
'''.format(tp=tp_count, ptp=ptp, nc=len(contacts)))
if __name__ == "__main__":
......
No preview for this file type
# coding=utf-8
import os
import subprocess
from setuptools import setup, find_packages, Command
......@@ -9,7 +10,7 @@ try:
version_git = subprocess.check_output(["git", "describe",
"--tags"]).rstrip()
except subprocess.CalledProcessError:
with open(version_py, 'r') as fh:
with open(version_py) as fh:
version_git = open(version_py).read().strip().split('=')[-1].replace('"',
'')
......@@ -26,14 +27,27 @@ class CleanCommand(Command):
"""Custom clean command to tidy up the project root."""
user_options = []
def initialize_options(self):
@staticmethod
def initialize_options():
"""
:return:
"""
pass
def finalize_options(self):
@staticmethod
def finalize_options():
"""
:return:
"""
pass
@staticmethod
def run():
"""
call clean command
"""
os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg '
'./*.egg-info')
......@@ -93,6 +107,7 @@ setup(
'matplotlib'],
install_requires=[
# 'configparser', # TODO: add this requiirement only if python v2
'docutils>=0.3',
'pandas',
'seaborn',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment