sys.modules.keys

Here are the examples of the python api sys.modules.keys taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

169 Examples 7

Example 51

Project: dill
Source File: __diff.py
View license
def _imp(*args, **kwds):
    """
    Replaces the default __import__, to allow a module to be memorised
    before the user can change it
    """
    before = set(sys.modules.keys())
    mod = __import__(*args, **kwds)
    after = set(sys.modules.keys()).difference(before)
    for m in after:
        memorise(sys.modules[m])
    return mod

Example 52

Project: pyleaf
Source File: prj.py
View license
    def _updateUserModule(self):
        import sys

        #the following is needed to sync the inspect module
        #with actual disk content
        import linecache
        linecache.checkcache()

        if self._modulename in sys.modules.keys():
            log.send('Reloading user module.')
            self._usermodule = sys.modules[self._modulename]
            reload(self._usermodule)
        else:
            log.send('Loading user module.')
            self._usermodule = __import__(self._modulename)

        log.send('Your module is: ' + str(self._usermodule), 2)

        return self._usermodule

Example 53

Project: Voodoo-Mock
Source File: untee.py
View license
def importFromTee( name, fromlist ):
    if name not in sys.modules.keys():
        sys.modules[ name ] = imp.new_module( name );
    module = sys.modules[ name ];
    for each in fromlist:
        setattr( module, each, oldImport( each ) );
    return module;

Example 54

Project: automat
Source File: test_discover.py
View license
    def setUp(self):
        super(_WritesPythonModules, self).setUp()

        from twisted.python.modules import getModule, PythonPath
        from twisted.python.filepath import FilePath

        self.getModule = getModule
        self.PythonPath = PythonPath
        self.FilePath = FilePath

        self.originalSysModules = set(sys.modules.keys())
        self.savedSysPath = sys.path[:]

        self.pathDir = tempfile.mkdtemp()
        self.makeImportable(self.pathDir)

Example 55

Project: pyff
Source File: HexoViz.py
View license
    def shut_down(self):
        # try to remove all Panda3D stuff from working memory so that it has to be completely reloaded
        base.exitfunc()
        for key in sys.modules.keys():
            if key.startswith("direct.") and not(sys.modules[key]==None):# and not key.endswith('Messenger'):
                del sys.modules[key]
                #sys.modules.pop(key)
        pass

Example 56

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    def test_xml(self):
        """
        verify printing of XML box

        Original test file was input/conformance/file1.jp2
        """
        elt = ET.fromstring(fixtures.file1_xml)
        xml = ET.ElementTree(elt)
        box = glymur.jp2box.XMLBox(xml=xml, length=439, offset=36)
        actual = str(box)
        expected = fixtures.file1_xml_box
        self.assertEqual(actual, expected)

Example 57

Project: LiveRemoteScripts
Source File: Debug.py
View license
	def _reimport_loaded_modules(self):
		self.log_message('reimporting loaded modules.')
		for module in sys.modules.keys():
			self.log_message('preexisting: ' + str(module))
			if module is 'Livid_Base':
				newBase = Livid_Base
				sys.modules[module] = newBase
				self.log_message('replaced Livid_Base with new version!')
			if module is 'Livid_Alias8':
				import Livid_Alias8
				newAlias = Livid_Alias8
				sys.modules[module] = newAlias

Example 58

Project: pyff
Source File: RollbackImporter.py
View license
    def uninstall(self):
        """Unload all modules since __init__ and restore the original import."""
        for module in sys.modules.keys():
            if not self.oldmodules.has_key(module):
                del sys.modules[module]
        __builtin__.__import__ = self.realimport

Example 59

Project: iot-utilities
Source File: __init__.py
View license
    def tearDown(self):
        """Make sure no modules pre-exist in sys.modules which are being used to
        test."""
        for key in list(sys.modules.keys()):
            if key.startswith('test.test_import.data.circular_imports'):
                del sys.modules[key]

Example 60

Project: mirage
Source File: test_module.py
View license
    def tearDown(self):
        import sys

        test_modules = [x for x in sys.modules.keys() if x.startswith('localhost_stubotest')]
        for mod in test_modules:
            del sys.modules[mod]
        self.q_patch.stop()

Example 61

Project: jaikuenginepatch
Source File: settings_post.py
View license
def check_app_imports(app):
    before = sys.modules.keys()
    __import__(app, {}, {}, [''])
    after = sys.modules.keys()
    added = [key[len(app)+1:] for key in after if key not in before and
             key.startswith(app + '.') and key[len(app)+1:]]
    if added:
        import logging
        logging.warn('The app "%(app)s" contains imports in '
                     'its __init__.py (at least %(added)s). This can cause '
                     'strange bugs due to recursive imports! You should '
                     'either do the import lazily (within functions) or '
                     'ignore the app settings/urlsauto with '
                     'IGNORE_APP_SETTINGS and IGNORE_APP_URLSAUTO in '
                     'your settings.py.'
                     % {'app': app, 'added': ', '.join(added)})

Example 62

Project: flexx
Source File: testing.py
View license
def _clear_our_modules():
    """ Remove ourselves from sys.modules to force an import.
    """
    for key in list(sys.modules.keys()):
        if key.startswith(PACKAGE_NAME) and 'testing' not in key:
            del sys.modules[key]

Example 63

Project: apptools
Source File: version_registry.py
View license
def _create_registry():
    """Creates a reload safe, singleton registry.
    """
    registry = None
    for key in sys.modules.keys():
        if 'version_registry' in key:
            mod = sys.modules[key]
            if hasattr(mod, 'registry'):
                registry = mod.registry
                break
    if not registry:
        registry = HandlerRegistry()
    return registry

Example 64

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    def test_jp2_codestream_0(self):
        """Verify dumping with -c 0, supressing all codestream details."""
        actual = self.run_jp2dump(['', '-c', '0', self.jp2file])

        # shave off the codestream details
        lines = fixtures.nemo.split('\n')
        expected = lines[0:105]
        expected = '\n'.join(expected)
        self.assertEqual(actual, expected)

Example 65

Project: dagbldr
Source File: __diff.py
View license
def _imp(*args, **kwds):
    """
    Replaces the default __import__, to allow a module to be memorised
    before the user can change it
    """
    before = set(sys.modules.keys())
    mod = __import__(*args, **kwds)
    after = set(sys.modules.keys()).difference(before)
    for m in after:
        memorise(sys.modules[m])
    return mod

Example 66

Project: clastic
Source File: basic.py
View license
def create_decked_out_app():
    resources = {'start_time': time.time(),
                 'module_list': sys.modules.keys()}
    middlewares = [GetParamMiddleware(['name', 'date', 'session_id', 'limit']),
                   SignedCookieMiddleware(),
                   SimpleContextProcessor('name')]
    routes = [('/', cookie_hello_world, render_basic),
              ('/debug', debug, render_basic),
              ('/fizzbuzz', fizzbuzz, render_basic),
              ('/modules', see_modules, render_basic),
              ('/fraiser', fraiser, render_basic)]
    return Application(routes, resources, middlewares=middlewares)

Example 67

Project: django-load
Source File: tests.py
View license
    def tearDown(self):
        """
        Reset imports
        """
        for key in sys.modules.keys():
            if key not in self.old_modules:
                del sys.modules[key]

Example 68

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    def test_jp2_codestream_1(self):
        """Verify dumping with -c 1, print just the header."""
        actual = self.run_jp2dump(['', '-c', '1', self.jp2file])

        # shave off the  non-main-header segments
        lines = fixtures.nemo.split('\n')
        expected = lines[0:140]
        expected = '\n'.join(expected)
        self.assertEqual(actual, expected)

Example 69

Project: keystonemiddleware
Source File: utils.py
View license
    def clear_module(self):
        cleared_modules = {}
        for fullname in list(sys.modules.keys()):
            if (fullname == self.module or
                    fullname.startswith(self.module + '.')):
                cleared_modules[fullname] = sys.modules.pop(fullname)
        return cleared_modules

Example 70

Project: pybuilder
Source File: coverage_plugin.py
View license
def _delete_non_essential_modules():
    sys_packages, sys_modules = _get_system_assets()
    for module_name in list(sys.modules.keys()):
        module = sys.modules[module_name]
        if module:
            if not _is_module_essential(module.__name__, sys_packages, sys_modules):
                _delete_module(module_name, module)

Example 71

Project: iot-utilities
Source File: __init__.py
View license
    def tearDown(self):
        """Make sure no modules pre-exist in sys.modules which are being used to
        test."""
        for key in list(sys.modules.keys()):
            if key.startswith('test.test_import.data.circular_imports'):
                del sys.modules[key]

Example 72

Project: sphinx-gallery
Source File: gen_rst.py
View license
def clean_modules():
    """Remove "unload" seaborn from the name space

    After a script is executed it can load a variety of setting that one
    does not want to influence in other examples in the gallery."""

    # Horrible code to 'unload' seaborn, so that it resets
    # its default when is load
    # Python does not support unloading of modules
    # https://bugs.python.org/issue9072
    for module in list(sys.modules.keys()):
        if 'seaborn' in module:
            del sys.modules[module]

    # Reset Matplotlib to default
    plt.rcdefaults()

Example 73

Project: func
Source File: funccover.py
View license
    def begin(self):
        log.debug("Coverage begin")
        import coverage
        self.skipModules = sys.modules.keys()[:]
        if self.coverErase:
            log.debug("Clearing previously collected coverage statistics")
            coverage.erase()
        coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
        coverage.start()

Example 74

Project: hifive
Source File: test_hic_data.py
View license
    def test_hic_bam_data_creation(self):
        if 'pysam' not in sys.modules.keys():
            print >> sys.stderr, "pysam required for bam import"
            return None
        subprocess.call("./bin/hifive hic-data -q -S %s %s -i 500 %s test/data/test_temp.hcd" %
                        (self.bam_fname1, self.bam_fname2, self.fend_fname), shell=True)
        data = h5py.File('test/data/test_temp.hcd', 'r')
        self.compare_hdf5_dicts(self.bam_data, data, 'data')

Example 75

Project: wxbanker
Source File: misc.py
View license
def printImported():
    '''Output a list of pubsub modules imported so far'''
    import sys
    ll = [mod for mod in sys.modules.keys() if mod.find('pubsub') >= 0]
    ll.sort()
    print '\n'.join(ll)

Example 76

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    def test_suppress_xml(self):
        """Verify dumping with -x, suppress XML."""
        actual = self.run_jp2dump(['', '-x', self.jp2file])

        # shave off the XML and non-main-header segments
        lines = fixtures.nemo.split('\n')
        expected = lines[0:18]
        expected.extend(lines[104:140])
        expected = '\n'.join(expected)
        self.assertEqual(actual, expected)

Example 77

Project: wxbanker
Source File: misc.py
View license
def printImported():
    '''Output a list of pubsub modules imported so far'''
    import sys
    ll = [mod for mod in sys.modules.keys() if mod.find('pubsub') >= 0]
    ll.sort()
    print '\n'.join(ll)

Example 78

Project: popcorn_maker
Source File: cover.py
View license
    def begin(self):
        """
        Begin recording coverage information.
        """
        log.debug("Coverage begin")
        self.skipModules = sys.modules.keys()[:]
        if self.coverErase:
            log.debug("Clearing previously collected coverage statistics")
            self.coverInstance.erase()
        self.coverInstance.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
        self.coverInstance.start()

Example 79

Project: popcorn_maker
Source File: isolate.py
View license
    def afterContext(self):
        """Pop my mod stack and restore sys.modules to the state
        it was in when mod stack was pushed.
        """
        mods = self._mod_stack.pop()
        to_del = [ m for m in sys.modules.keys() if m not in mods ]
        if to_del:
            log.debug('removing sys modules entries: %s', to_del)
            for mod in to_del:
                del sys.modules[mod]
        sys.modules.update(mods)

Example 80

Project: iot-utilities
Source File: __init__.py
View license
    def tearDown(self):
        """Make sure no modules pre-exist in sys.modules which are being used to
        test."""
        for key in list(sys.modules.keys()):
            if key.startswith('test.test_import.data.circular_imports'):
                del sys.modules[key]

Example 81

Project: rejected
Source File: utils.py
View license
def get_module_data():
    modules = {}
    for module_name in sys.modules.keys():
        module = sys.modules[module_name]
        if hasattr(module, '__version__'):
            modules[module_name] = module.__version__
        elif hasattr(module, 'version'):
            modules[module_name] = module.version
        else:
            try:
                version = get_version(module_name)
                if version:
                    modules[module_name] = version
            except Exception:
                pass
    return modules

Example 82

Project: python-haystack
Source File: vol.py
View license
    def _unload_volatility(self):
        '''we cannot have volatility already loaded.
        we need to remove it'''
        for mod in sys.modules.keys():
            if 'volatility' in mod:
                del sys.modules[mod]

Example 83

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    def test_full_codestream(self):
        """
        Verify printing with the full blown codestream
        """
        jp2 = Jp2k(self.jp2file)
        glymur.set_option('parse.full_codestream', True)

        # Get rid of the file line
        actual = '\n'.join(str(jp2).splitlines()[1:])

        expected = fixtures.nemo
        self.assertEqual(actual, expected)

        opt = glymur.get_option('print.codestream')
        self.assertTrue(opt)

Example 84

Project: locality-sensitive-hashing
Source File: cover.py
View license
    def begin(self):
        """
        Begin recording coverage information.
        """
        log.debug("Coverage begin")
        self.skipModules = sys.modules.keys()[:]
        if self.coverErase:
            log.debug("Clearing previously collected coverage statistics")
            self.coverInstance.combine()
            self.coverInstance.erase()
        self.coverInstance.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]')
        self.coverInstance.load()
        self.coverInstance.start()

Example 85

Project: Z2Pack
Source File: test_doc.py
View license
def test_all_doc():
    old_name = 'z2pack'
    new_name = 'hoopy_z2pack'
    for key in list(sys.modules.keys()):
        # move previous z2pack to hoopy_z2pack
        if key.startswith(old_name):
            new_key = key.replace(old_name, new_name)
            sys.modules[new_key] = sys.modules[key]
            del sys.modules[key]
    fsc.export.test_doc()
    try:
        import z2pack
    finally:
        # reset to the previous z2pack -- just doing import breaks pickle
        for key in list(sys.modules.keys()):
            if key.startswith(old_name):
                del sys.modules[key]
        for key in list(sys.modules.keys()):
            if key.startswith(new_name):
                new_key = key.replace(new_name, old_name)
                sys.modules[new_key] = sys.modules[key]
                del sys.modules[key]

Example 86

Project: netzob
Source File: DepCheck.py
View license
    @staticmethod
    def checkCExtensions():
        # Verify we can load the lib Needleman
        try:
            from netzob import _libNeedleman
        except ImportError as e:
            logging.error("Impossible to import the libNeedleman: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libNeedleman":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libNeedleman from {0}".format(pathToImportedLib))
        try:
            BIDNeedleman = _libNeedleman.getBID()
            if BIDNeedleman != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDNeedleman, NetzobResources.BID))
                logging.error("The loaded libNeedleman library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libNeedleman imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # Verify we can load the lib ScoreComputation
        try:
            from netzob import _libScoreComputation
        except ImportError as e:
            logging.error("Impossible to import the libScoreComputation: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libScoreComputation":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libScoreComputation from {0}".format(pathToImportedLib))
        try:
            BIDScoreComputation = _libScoreComputation.getBID()
            if BIDScoreComputation != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDScoreComputation, NetzobResources.BID))
                logging.error("The loaded libScoreComputation library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libScoreComputation imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # Verify we can load the lib Interface
        try:
            from netzob import _libInterface
        except ImportError as e:
            logging.error("Impossible to import the libInterface: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libInterface":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libInterface from {0}".format(pathToImportedLib))
        try:
            BIDInterface = _libInterface.getBID()
            if BIDInterface != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDInterface, NetzobResources.BID))
                logging.error("The loaded libInterface library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libInterface imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # # Verify we can load the lib Regex
        # try:
        #     from netzob import _libRegex
        # except ImportError:
        #     logging.error("Impossible to import the libRegex")
        #     return False

        # pathToImportedLib = "Unknown path"
        # for m in sys.modules.keys():
        #     if m == "_libRegex":
        #         pathToImportedLib = sys.modules[m].__file__
        #         logging.debug("Imported _libRegex from {0}".format(pathToImportedLib))
        # try:
        #     BIDRegex = _libRegex.getBID()
        #     if BIDRegex != NetzobResources.BID and NetzobResources.BID != "$BID":
        #         logging.error("Binary Identifier is {0} (expected {1}).".format(BIDRegex, NetzobResources.BID))
        #         logging.error("The loaded libRegex library is deprecated, please rebuild it.")
        #         return False
        # except AttributeError:
        #     logging.error("The _libScoreComputation imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
        #     return False

        if NetzobResources.BID == "$BID":
            logging.warning("The current executed python code has not been built using setup.py, hence it's not possible to compare the BID of executed libs.")

        return True

Example 87

Project: netzob
Source File: DepCheck.py
View license
    @staticmethod
    def checkCExtensions():
        # Verify we can load the lib Needleman
        try:
            from netzob import _libNeedleman
        except ImportError as e:
            logging.error("Impossible to import the libNeedleman: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libNeedleman":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libNeedleman from {0}".format(pathToImportedLib))
        try:
            BIDNeedleman = _libNeedleman.getBID()
            if BIDNeedleman != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDNeedleman, NetzobResources.BID))
                logging.error("The loaded libNeedleman library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libNeedleman imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # Verify we can load the lib ScoreComputation
        try:
            from netzob import _libScoreComputation
        except ImportError as e:
            logging.error("Impossible to import the libScoreComputation: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libScoreComputation":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libScoreComputation from {0}".format(pathToImportedLib))
        try:
            BIDScoreComputation = _libScoreComputation.getBID()
            if BIDScoreComputation != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDScoreComputation, NetzobResources.BID))
                logging.error("The loaded libScoreComputation library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libScoreComputation imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # Verify we can load the lib Interface
        try:
            from netzob import _libInterface
        except ImportError as e:
            logging.error("Impossible to import the libInterface: {0}".format(e))
            return False

        pathToImportedLib = "Unknown path"
        for m in sys.modules.keys():
            if m == "_libInterface":
                pathToImportedLib = sys.modules[m].__file__
                logging.debug("Imported _libInterface from {0}".format(pathToImportedLib))
        try:
            BIDInterface = _libInterface.getBID()
            if BIDInterface != NetzobResources.BID and NetzobResources.BID != "$BID":
                logging.error("Binary Identifier is {0} (expected {1}).".format(BIDInterface, NetzobResources.BID))
                logging.error("The loaded libInterface library is deprecated, please rebuild it.")
                return False
        except AttributeError:
            logging.error("The _libInterface imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
            return False

        # # Verify we can load the lib Regex
        # try:
        #     from netzob import _libRegex
        # except ImportError:
        #     logging.error("Impossible to import the libRegex")
        #     return False

        # pathToImportedLib = "Unknown path"
        # for m in sys.modules.keys():
        #     if m == "_libRegex":
        #         pathToImportedLib = sys.modules[m].__file__
        #         logging.debug("Imported _libRegex from {0}".format(pathToImportedLib))
        # try:
        #     BIDRegex = _libRegex.getBID()
        #     if BIDRegex != NetzobResources.BID and NetzobResources.BID != "$BID":
        #         logging.error("Binary Identifier is {0} (expected {1}).".format(BIDRegex, NetzobResources.BID))
        #         logging.error("The loaded libRegex library is deprecated, please rebuild it.")
        #         return False
        # except AttributeError:
        #     logging.error("The _libScoreComputation imported ({0}) is not the expected one and do not provide all the required methods.".format(pathToImportedLib))
        #     return False

        if NetzobResources.BID == "$BID":
            logging.warning("The current executed python code has not been built using setup.py, hence it's not possible to compare the BID of executed libs.")

        return True

Example 88

Project: MCEdit-Unified
Source File: filter.py
View license
def tryImport_old(_root, name, org_lang, stock=False, subFolderString="", unicode_name=False, notify=True):
    with open(os.path.join(_root, name)) as module_file:
        module_name = name.split(os.path.sep)[-1].replace(".py", "")
        try:
            if unicode_name:
                source_code = module_file.read()
                module = imp.new_module(module_name)
                exec (source_code, module.__dict__)
                if module_name not in sys.modules.keys():
                    sys.modules[module_name] = module
            else:
                module = imp.load_source(module_name, os.path.join(_root, name), module_file)
            module.foldersForDisplayName = subFolderString
            if not (hasattr(module, 'displayName')):
                module.displayName = module_name  # Python is awesome
            if not stock:
                if "trn" in sys.modules.keys():
                    del sys.modules["trn"]
                if "albow.translate" in sys.modules.keys():
                    del sys.modules["albow.translate"]
                from albow import translate as trn
                if directories.getFiltersDir() in name:
                    trn_path = os.path.split(name)[0]
                else:
                    trn_path = directories.getFiltersDir()
                trn_path = os.path.join(trn_path, subFolderString[1:-1], module_name)
                module.trn = trn
                if os.path.exists(trn_path):
                    module.trn.setLangPath(trn_path)
                    module.trn.buildTranslation(config.settings.langCode.get())
                    n = module.displayName
                    if hasattr(module, "trn"):
                        n = module.trn._(module.displayName)
                    if n == module.displayName:
                        n = _(module.displayName)
                    module.displayName = n
                import albow.translate
                albow.translate.lang = org_lang
            return module

        except Exception as e:
            traceback.print_exc()
            if notify:
                alert(_(u"Exception while importing filter module {}. " +
                        u"See console for details.\n\n{}").format(name, e))
            return None

Example 89

Project: MCEdit-Unified
Source File: filter.py
View license
def tryImport_old(_root, name, org_lang, stock=False, subFolderString="", unicode_name=False, notify=True):
    with open(os.path.join(_root, name)) as module_file:
        module_name = name.split(os.path.sep)[-1].replace(".py", "")
        try:
            if unicode_name:
                source_code = module_file.read()
                module = imp.new_module(module_name)
                exec (source_code, module.__dict__)
                if module_name not in sys.modules.keys():
                    sys.modules[module_name] = module
            else:
                module = imp.load_source(module_name, os.path.join(_root, name), module_file)
            module.foldersForDisplayName = subFolderString
            if not (hasattr(module, 'displayName')):
                module.displayName = module_name  # Python is awesome
            if not stock:
                if "trn" in sys.modules.keys():
                    del sys.modules["trn"]
                if "albow.translate" in sys.modules.keys():
                    del sys.modules["albow.translate"]
                from albow import translate as trn
                if directories.getFiltersDir() in name:
                    trn_path = os.path.split(name)[0]
                else:
                    trn_path = directories.getFiltersDir()
                trn_path = os.path.join(trn_path, subFolderString[1:-1], module_name)
                module.trn = trn
                if os.path.exists(trn_path):
                    module.trn.setLangPath(trn_path)
                    module.trn.buildTranslation(config.settings.langCode.get())
                    n = module.displayName
                    if hasattr(module, "trn"):
                        n = module.trn._(module.displayName)
                    if n == module.displayName:
                        n = _(module.displayName)
                    module.displayName = n
                import albow.translate
                albow.translate.lang = org_lang
            return module

        except Exception as e:
            traceback.print_exc()
            if notify:
                alert(_(u"Exception while importing filter module {}. " +
                        u"See console for details.\n\n{}").format(name, e))
            return None

Example 90

Project: pywikibot-core
Source File: version.py
View license
def package_versions(modules=None, builtins=False, standard_lib=None):
    """Retrieve package version information.

    When builtins or standard_lib are None, they will be included only
    if a version was found in the package.

    @param modules: Modules to inspect
    @type modules: list of strings
    @param builtins: Include builtins
    @type builtins: Boolean, or None for automatic selection
    @param standard_lib: Include standard library packages
    @type standard_lib: Boolean, or None for automatic selection
    """
    if not modules:
        modules = sys.modules.keys()

    std_lib_dir = get_python_lib(standard_lib=True)

    root_packages = set(key.split('.')[0] for key in modules)

    builtin_packages = set(name.split('.')[0] for name in root_packages
                           if name in sys.builtin_module_names or
                           '_' + name in sys.builtin_module_names)

    # Improve performance by removing builtins from the list if possible.
    if builtins is False:
        root_packages = list(root_packages - builtin_packages)

    std_lib_packages = []

    paths = {}
    data = {}

    for name in root_packages:
        try:
            package = __import__(name, level=0)
        except Exception as e:
            data[name] = {'name': name, 'err': e}
            continue

        info = {'package': package, 'name': name}

        if name in builtin_packages:
            info['type'] = 'builtins'

        if '__file__' in package.__dict__:
            # Determine if this file part is of the standard library.
            if os.path.normcase(package.__file__).startswith(
                    os.path.normcase(std_lib_dir)):
                std_lib_packages.append(name)
                if standard_lib is False:
                    continue
                info['type'] = 'standard libary'

            # Strip '__init__.py' from the filename.
            path = package.__file__
            if '__init__.py' in path:
                path = path[0:path.index('__init__.py')]

            if PY2:
                path = path.decode(sys.getfilesystemencoding())

            info['path'] = path
            assert path not in paths, 'Path of the package is in defined paths'
            paths[path] = name

        if '__version__' in package.__dict__:
            info['ver'] = package.__version__
        elif name.startswith('unicodedata'):
            info['ver'] = package.unidata_version

        # If builtins or standard_lib is None,
        # only include package if a version was found.
        if (builtins is None and name in builtin_packages) or \
                (standard_lib is None and name in std_lib_packages):
            if 'ver' in info:
                data[name] = info
            else:
                # Remove the entry from paths, so it isnt processed below
                del paths[info['path']]
        else:
            data[name] = info

    # Remove any pywikibot sub-modules which were loaded as a package.
    # e.g. 'wikipedia_family.py' is loaded as 'wikipedia'
    _program_dir = _get_program_dir()
    for path, name in paths.items():
        if _program_dir in path:
            del data[name]

    return data

Example 91

Project: edd
Source File: ereloader.py
View license
def unload(silent=True, packages=None):
    """
        performs unloading.
            * silent flag specifies if utility should print the unloaded modules
            * packages: array of packages to unload. specify None to use 
                defaults (DEFAULT_RELOAD_PACKAGES variable)
    """
    
    if packages is None:
        packages = DEFAULT_RELOAD_PACKAGES
        
    # construct reload list
    reloadList = []
    for i in sys.modules.keys():
        for package in packages:
            if i.startswith(package):
                reloadList.append(i)

    # unload everything
    for i in reloadList:
        try:
            if sys.modules[i] is not None:
                del(sys.modules[i])
                if not silent:
                    print "unloaded: %s " % str(i)
        except:
            print "failed to unload: %s " % str(i)

Example 92

Project: spitfire
Source File: crunner.py
View license
def reset_sys_modules():
    for key in sys.modules.keys():
        if key not in sys_modules:
            del sys.modules[key]

Example 93

Project: zoof
Source File: testing.py
View license
def _clear_our_modules():
    # Remove ourselves from sys.modules to force an import
    for key in list(sys.modules.keys()):
        if key.startswith(PACKAGE_NAME):
            del sys.modules[key]

Example 94

Project: glymur
Source File: test_printing.py
View license
    @unittest.skipIf('lxml' not in sys.modules.keys(), "No lxml")
    @unittest.skipIf(re.match("1.5|2",
                              glymur.version.openjpeg_version) is None,
                     "Must have openjpeg 1.5 or higher to run")
    def test_asoc_label_box(self):
        """verify printing of asoc, label boxes"""
        # Construct a fake file with an asoc and a label box, as
        # OpenJPEG doesn't have such a file.
        data = glymur.Jp2k(self.jp2file)[::2, ::2]
        with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
            with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile2:
                glymur.Jp2k(tfile.name, data=data)

                # Offset of the codestream is where we start.
                wbuffer = tfile.read(77)
                tfile2.write(wbuffer)

                # read the rest of the file, it's the codestream.
                codestream = tfile.read()

                # Write the asoc superbox.
                # Length = 36, id is 'asoc'.
                wbuffer = struct.pack('>I4s', int(56), b'asoc')
                tfile2.write(wbuffer)

                # Write the contained label box
                wbuffer = struct.pack('>I4s', int(13), b'lbl ')
                tfile2.write(wbuffer)
                tfile2.write('label'.encode())

                # Write the xml box
                # Length = 36, id is 'xml '.
                wbuffer = struct.pack('>I4s', int(35), b'xml ')
                tfile2.write(wbuffer)

                wbuffer = '<test>this is a test</test>'
                wbuffer = wbuffer.encode()
                tfile2.write(wbuffer)

                # Now append the codestream.
                tfile2.write(codestream)
                tfile2.flush()

                jasoc = glymur.Jp2k(tfile2.name)
                actual = str(jasoc.box[3])
                expected = ('Association Box (asoc) @ (77, 56)\n'
                            '    Label Box (lbl ) @ (85, 13)\n'
                            '        Label:  label\n'
                            '    XML Box (xml ) @ (98, 35)\n'
                            '        <test>this is a test</test>')
                self.assertEqual(actual, expected)

Example 95

Project: hifive
Source File: complete_hic_project.py
View license
def run(args):
    if 'mpi4py' in sys.modules.keys():
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        num_procs = comm.Get_size()
    else:
        comm = None
        rank = 0
        num_procs = 1
    if args.chroms is None:
        chroms = []
    else:
        chroms = args.chroms.split(',')
        if len(chroms) == 1 and chroms[0] == '':
            chroms = []
    if args.matrix is not None and args.binned is None:
        if rank == 0:
            print sys.stderr, ("Loading data from matrices is only supported for binned data.\n")
        return 1
    if args.algorithm.count('binning') > 0:
        if args.binned is not None:
            if rank == 0:
                print sys.stderr, ("This normalization algorithm is not currently supported for binned data.\n")
            return 1
        model = args.model.split(',')
        modelbins = args.modelbins.split(',')
        parameters = args.parameters.split(',')
        for i in range(len(modelbins)):
            try:
                modelbins[i] = int(modelbins[i])
            except:
                if rank == 0:
                    print sys.stderr, ("Not all arguments in -n/--modelbins could be converted to integers.\n")
                return 1
        if len(model) != len(modelbins) or len(model) != len(parameters):
            if rank == 0:
                print sys.stderr, ("-v/--model, -n/--modelbins, and -u/--parameter-types must be equal lengths.\n")
            return 1
    if args.binned == 0 and args.bed is None:
        if rank == 0:
            print  >> sys.stderr, ("Non-uniforming binning (binned=0) must have a bed file to read bin partitions from.\n"),
        return None
    elif args.binned is None or args.binned < 1 and args.length is not None:
        if rank == 0:
            print  >> sys.stderr, ("Binning from a chromosome length file needs a positive integer value for binning.\n"),
        return None
    if args.prefix is None:
        fend_fname, data_fname, project_fname = args.output
    else:
        fend_fname = "%s.fends" % args.prefix
        data_fname = "%s.hcd" % args.prefix
        project_fname = "%s.hcp" % args.prefix
    if rank == 0:
        fends = Fend(fend_fname, mode='w', binned=args.binned, silent=args.silent)
        if args.bed is not None:
            if args.binned is not None and args.binned == 0:
                fends.load_bins(args.bed, genome_name=args.genome, format='bed')
            else:
                fends.load_fends(args.bed, genome_name=args.genome, re_name=args.re, format="bed")
        elif args.fend is not None:
            fends.load_fends(args.fend, genome_name=args.genome, re_name=args.re, format="fend")
        else:
            fends.load_bins(args.length, genome_name=args.genome, format='len')
        fends.save()
        del fends
        data = HiCData(data_fname, 'w', silent=args.silent)
        if not args.bam is None: 
            data.load_data_from_bam(fend_fname, args.bam, args.insert, args.skipdups)
        elif not args.raw is None: 
            data.load_data_from_raw(fend_fname, args.raw, args.insert, args.skipdups)
        elif not args.mat is None: 
            data.load_data_from_mat(fend_fname, args.mat, args.insert)
        elif not args.matrix is None:
            data.load_binned_data_from_matrices(fend_fname, args.matrix, format=None)
        data.save()
        del data
        for i in range(1, num_procs):
            comm.send(1, dest=i, tag=11)
    else:
        comm.recv(source=0, tag=11)
    hic = HiC(project_fname, 'w', silent=args.silent)
    hic.load_data(data_fname)
    hic.filter_fends(mininteractions=args.minint, mindistance=args.mindist, maxdistance=args.maxdist)
    hic.find_distance_parameters(minsize=args.minbin, numbins=args.numbins)
    precorrect = False
    if args.algorithm in ['binning', 'binning-express', 'binning-probability']:
        hic.find_binning_fend_corrections(mindistance=args.mindist, maxdistance=args.maxdist, parameters=parameters,
                                             chroms=chroms, num_bins=modelbins, model=model, usereads=args.binreads,
                                             learning_threshold=args.threshold, max_iterations=args.biniter,
                                             pseudocounts=args.pseudo)
        precorrect = True
    if args.algorithm in ['probability', 'binning-probability']:
        hic.find_probability_fend_corrections(mindistance=args.mindist, maxdistance=args.maxdist,
                                              minchange=args.change, max_iterations=args.probiter,
                                              learningstep=args.step, chroms=chroms,
                                              precalculate=args.precalc, precorrect=precorrect)
    elif args.algorithm in ['express', 'binning-express']:
        hic.find_express_fend_corrections(iterations=args.expiter, mindistance=args.mindist,
                                          maxdistance=args.maxdist, remove_distance=args.nodist,
                                          usereads=args.expreads, mininteractions=args.minint,
                                          chroms=chroms, minchange=args.change, precorrect=precorrect,
                                          binary=args.binary, kr=args.kr)
    if rank == 0:
        hic.save()

Example 96

Project: hifive
Source File: create_fivec_heatmap.py
View license
def run(args):
    if not args.image is None and args.pdf and "pyx" not in sys.modules.keys():
        print >> sys.stderr, ("-p/--pdf requires the package 'pyx'"),
        return 1
    if args.binsize > 0:
        args.arraytype = 'full'
    if args.regions is None:
        regions = []
    else:
        regions = args.regions.split(',')
        if len(regions) == 1 and regions[0] == '':
            regions = []
    for i in range(len(regions)):
        try:
            regions[i] = int(regions[i])
        except:
            print sys.stderr, ("Not all arguments in -r/--regions could be converted to integers.")
            return 1
    fivec = FiveC(args.project, 'r', silent=args.silent)
    fivec.write_heatmap(args.output, binsize=args.binsize, includetrans=args.trans, arraytype=args.arraytype,
                        datatype=args.datatype, regions=regions, dynamically_binned=args.dynamic,
                        expansion_binsize=args.expbinsize, minobservations=args.minobs, searchdistance=args.search,
                        removefailed=args.remove)
    if not args.image is None:
        kwargs = {}
        for arg in args.keywords:
            temp = arg.split("=")
            if temp[1] in ["True", "TRUE", "true"]:
                temp[1] = True
            elif temp[1] in ["False", "FALSE", "false"]:
                temp[1] = False
            elif temp[1][0] == "(":
                temp[1] = temp[1].strip('()').split(',')
                for i in range(len(temp[1])):
                    temp[1][i] = int(temp[1][i])
                temp[1] = tuple(temp[1])
            elif temp[1][0] == "[":
                temp[1] = temp[1].strip('[]').split(',')
                for i in range(len(temp[1])):
                    temp[1][i] = int(temp[1][i])
            else:
                try:
                    temp[1] = int(temp[1])
                except:
                    try:
                        temp[1] = float(temp[1])
                    except:
                        # strip off extra characters introduced by galaxy into color format
                        temp[1] = temp[1].replace('__pd__','')
            kwargs[temp[0]] = temp[1]
        if 'symmetricscaling' not in kwargs:
            if args.datatype == 'enrichment':
                kwargs['symmetricscaling'] = True
            else:
                kwargs['symmetricscaling'] = False
        img, minscore, maxscore = plot_fivec_heatmap(args.output, returnscale=True, silent=args.silent, **kwargs)
        if not args.pdf:
            img_format = args.image.split('.')[-1].upper()
            if img_format not in ['PNG', 'TIF', 'JPG', 'JPEG']:
                img_format = 'PNG'
            img.save(args.image, img_format)
        else:
            unit.set(defaultunit="cm")
            text.set(mode="latex")
            text.preamble(r"\usepackage{times}")
            text.preamble(r"\usepackage{sansmath}")
            text.preamble(r"\sansmath")
            text.preamble(r"\renewcommand*\familydefault{\sfdefault}")
            hm = h5py.File(args.output, 'r')
            totalx = 0
            totaly = 0
            minsize = 999999999
            sizes = [0]
            names = []
            all_regions = hm['regions'][...]
            for i, region in enumerate(all_regions['index']):
                names.append("chr%s:%i-%i" % (all_regions['chromosome'][i], all_regions['start'][i],
                                              all_regions['stop'][i]))
                if "%i.fragments" % region in hm:
                    name = "%i.fragments" % int(region)
                    sizes.append(hm[name].shape[0])
                    totalx += hm[name].shape[0]
                    totaly += hm[name].shape[0]
                    minsize = min(minsize, hm[name].shape[0])
                elif "%i.reverse_fragments" % int(region) in hm:
                    name = "%i.reverse_fragments" % int(region)
                    sizes.append(hm[name].shape[0])
                    totaly += hm[name].shape[0]
                    totalx += hm["%i.forward_fragments" % int(region)].shape[0]
                    minsize = min(minsize, hm[name].shape[0])
                elif "%i.positions" % int(region) in hm:
                    name = "%i.positions" % int(region)
                    sizes.append(hm[name].shape[0])
                    totalx += hm[name].shape[0]
                    totaly += hm[name].shape[0]
                    minsize = min(minsize, hm[name].shape[0])
            totaly += all_regions.shape[0] - 1
            totalx += all_regions.shape[0] - 1
            height = max(5.0, totaly * 0.5 / minsize)
            width = height / totaly * totalx
            if len(sizes) > 2:
                sizes[1] += 0.5
                sizes[-1] += 0.5
                if len(sizes) > 3:
                    for i in range(2, len(sizes) - 1):
                        sizes[i] += 1.0
            for i in range(1, len(sizes)):
                sizes[i] += sizes[i - 1]
            for i in range(1, len(sizes)):
                sizes[i] = sizes[i] / totaly * height
            c = canvas.canvas()
            c.insert(bitmap.bitmap(0, 0, img, width=width))
            if args.legend:
                if 'min_color' in kwargs:
                    min_color = kwargs['min_color']
                else:
                    min_color = (0, 0, 1)
                if 'mid_color' in kwargs:
                    mid_color = kwargs['mid_color']
                else:
                    mid_color = (1, 1, 1)
                if 'max_color' in kwargs:
                    max_color = kwargs['max_color']
                else:
                    max_color = (1, 0, 0)
                if 'logged' in kwargs:
                    logged = kwargs['logged']
                else:
                    logged = True
                c.insert(plot_key(min_score=minscore, max_score=maxscore, height=(height * 0.05),
                                  width=width, orientation='top', num_ticks=5, min_color=min_color,
                                  mid_color=mid_color, max_color=max_color, log_display=False),
                                  [trafo.translate(0, height * 1.05)])
                if logged:
                    label = "Log2 "
                else:
                    label = ""
                if args.datatype == 'enrichment':
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sEnrichment" % (label),
                           [text.halign.center, text.valign.bottom, text.size(-2)])
                elif args.datatype == 'raw':
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sCounts" % (label),
                           [text.halign.center, text.valign.bottom, text.size(-2)])
                else:
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sNormalized Counts" % (label),
                           [text.halign.center, text.valign.bottom, text.size(-2)])
            if args.names:
                for i, name in enumerate(names):
                    c.text(width + 0.25, height - (sizes[i] + sizes[i + 1]) / 2, name,
                           [text.halign.left, text.valign.middle, text.size(-2)])
            c.writePDFfile(args.image)
            if len(args.image.split('.')) <= 1 or args.image.split('.')[-1] != 'pdf':
                subprocess.call('mv %s.pdf %s' % (args.image, args.image), shell=True)

Example 97

Project: hifive
Source File: create_hic_heatmap.py
View license
def run(args):
    if 'mpi4py' in sys.modules.keys():
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        num_procs = comm.Get_size()
    else:
        comm = None
        rank = 0
        num_procs = 1
    if not args.image is None and args.pdf and "pyx" not in sys.modules.keys():
        if rank == 0:
            print >> sys.stderr, ("-p/--pdf requires the package 'pyx'"),
        sys.exit(1)
    if args.chroms is None:
        chroms = []
    else:
        chroms = args.chroms.split(',')
        if len(chroms) == 1 and chroms[0] == '':
            chroms = []
    hic = HiC(args.project, 'r', silent=args.silent)
    hic.write_heatmap(args.output, binsize=args.binsize, includetrans=args.trans,
                      datatype=args.datatype, chroms=chroms, dynamically_binned=args.dynamic,
                      expansion_binsize=args.expbinsize, minobservations=args.minobs,
                      searchdistance=args.search, removefailed=args.remove, format=args.format)
    if rank > 0:
        sys.exit(0)
    if not args.image is None:
        if args.format == 'txt':
            if rank == 0:
                print >> sys.stderr, ("Plotting is only available for non-txt formats.\n"),
            return None
        kwargs = {}
        for arg in args.keywords:
            temp = arg.split("=")
            if temp[1] in ["True", "TRUE", "true"]:
                temp[1] = True
            elif temp[1] in ["False", "FALSE", "false"]:
                temp[1] = False
            elif temp[1][0] == "(":
                temp[1] = temp[1].strip('()').split(',')
                for i in range(len(temp[1])):
                    temp[1][i] = int(temp[1][i])
                temp[1] = tuple(temp[1])
            elif temp[1][0] == "[":
                temp[1] = temp[1].strip('[]').split(',')
                for i in range(len(temp[1])):
                    temp[1][i] = int(temp[1][i])
            else:
                try:
                    temp[1] = int(temp[1])
                except:
                    try:
                        temp[1] = float(temp[1])
                    except:
                        # strip off extra characters introduced by galaxy into color format
                        temp[1] = temp[1].replace('__pd__','')
            kwargs[temp[0]] = temp[1]
        if 'symmetricscaling' not in kwargs:
            if args.datatype == 'enrichment':
                kwargs['symmetricscaling'] = True
            else:
                kwargs['symmetricscaling'] = False
        img, minscore, maxscore = plot_hic_heatmap(args.output, returnscale=True, silent=args.silent,
                                                   format=args.format, **kwargs)
        if not args.pdf:
            img_format = args.image.split('.')[-1].upper()
            if img_format not in ['PNG', 'TIF', 'JPG', 'JPEG']:
                img_format = 'PNG'
            img.save(args.image, img_format)
        else:
            unit.set(defaultunit="cm")
            text.set(mode="latex")
            text.preamble(r"\usepackage{times}")
            text.preamble(r"\usepackage{sansmath}")
            text.preamble(r"\sansmath")
            text.preamble(r"\renewcommand*\familydefault{\sfdefault}")
            hm = h5py.File(args.output, 'r')
            chroms = hm['chromosomes'][...]
            sizes = [0]
            minsize = 999999999
            for chrom in chroms:
                sizes.append(hm['%s.positions' % chrom].shape[0])
                minsize = min(minsize, sizes[-1])
            if len(sizes) > 2:
                sizes[1] += 0.5
                sizes[-1] += 0.5
                if len(sizes) > 3:
                    for i in range(2, len(sizes) - 1):
                        sizes[i] += 1.0
            for i in range(1, len(sizes)):
                sizes[i] += sizes[i - 1]
            height = width = max(5.0, sizes[-1] * 0.5 / minsize)
            for i in range(len(sizes)):
                sizes[i] = sizes[i] / sizes[-1] * height
            c = canvas.canvas()
            c.insert(bitmap.bitmap(0, 0, img, width=width))
            if args.legend:
                if 'min_color' in kwargs:
                    min_color = kwargs['min_color']
                else:
                    min_color = "0000ff"
                if 'mid_color' in kwargs:
                    mid_color = kwargs['mid_color']
                else:
                    mid_color = "ffffff"
                if 'max_color' in kwargs:
                    max_color = kwargs['max_color']
                else:
                    max_color = "ff0000"
                if 'logged' in kwargs:
                    logged = kwargs['logged']
                else:
                    logged = True
                c.insert(plot_key(min_score=minscore, max_score=maxscore, height=(height * 0.05),
                                  width=width, orientation='top', num_ticks=5, min_color=min_color,
                                  mid_color=mid_color, max_color=max_color, log_display=False),
                                  [trafo.translate(0, height * 1.05)])
                if logged:
                    label = "Log2 "
                else:
                    label = ""
                if args.datatype == 'enrichment':
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sEnrichment" % label,
                           [text.halign.center, text.valign.bottom, text.size(-2)])
                elif args.datatype == 'raw':
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sCounts" % label,
                           [text.halign.center, text.valign.bottom, text.size(-2)])
                else:
                    c.text(width * 0.5, height * 1.1 + 0.75, "%sNormalized Counts" % label,
                           [text.halign.center, text.valign.bottom, text.size(-2)])
            if args.names:
                for i, chrom in enumerate(chroms):
                    c.text(width + 0.25, height - (sizes[i] + sizes[i + 1]) / 2, 'chr%s' % chrom,
                           [text.halign.left, text.valign.middle, text.size(-2)])
            c.writePDFfile(args.image)
            if len(args.image.split('.')) <= 1 or args.image.split('.')[-1] != 'pdf':
                subprocess.call('mv %s.pdf %s' % (args.image, args.image), shell=True)

Example 98

Project: hifive
Source File: create_hic_project.py
View license
def run(args):
    if 'mpi4py' in sys.modules.keys():
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        num_procs = comm.Get_size()
    else:
        comm = None
        rank = 0
        num_procs = 1
    if rank == 0:
        hic = HiC(args.output, 'w', silent=args.silent)
        hic.load_data(args.data)
        hic.filter_fends(mininteractions=args.minint, mindistance=args.mindist, maxdistance=args.maxdist)
        hic.save()
        for i in range(1, num_procs):
            comm.send(1, dest=i, tag=11)
    else:
        comm.recv(source=0, tag=11)
        hic = HiC(args.output, 'r', silent=True)
    hic.find_distance_parameters(minsize=args.minbin, numbins=args.numbins)
    if rank == 0:
        hic.save()
    return None

Example 99

Project: hifive
Source File: get_fivec_interval.py
View license
def run(args):
    if not args.image is None and args.pdf and "pyx" not in sys.modules.keys():
        print sys.stderr, ("-p/--pdf requires the package 'pyx'"),
        return 1
    fivec = FiveC(args.project, 'r', silent=args.silent)
    if args.binsize == 0:
        arraytype = 'compact'
    else:
        arraytype = 'upper'
    kwargs = {}
    for arg in args.keywords:
        temp = arg.split("=")
        if temp[1] in ["True", "TRUE", "true"]:
            temp[1] = True
        elif temp[1] in ["False", "FALSE", "false"]:
            temp[1] = False
        elif temp[1][0] == "(":
            temp[1] = temp[1].strip('()').split(',')
            for i in range(len(temp[1])):
                temp[1][i] = int(temp[1][i])
            temp[1] = tuple(temp[1])
        elif temp[1][0] == "[":
            temp[1] = temp[1].strip('[]').split(',')
            for i in range(len(temp[1])):
                temp[1][i] = int(temp[1][i])
        else:
            try:
                temp[1] = int(temp[1])
            except:
                # strip off extra characters introduced by galaxy into color format
                temp[1] = temp[1].replace('__pd__','')
    chrom = fivec.frags['regions']['chromosome'][args.region]
    if args.region2 is None:
        temp = fivec.cis_heatmap(region=args.region, binsize=args.binsize, start=args.start,
                                 stop=args.stop, datatype=args.datatype, arraytype=arraytype,
                                 returnmapping=True, skipfiltered=True, dynamically_binned=args.dynamic,
                                 expansion_binsize=args.expbinsize, minobservations=args.minobs,
                                 searchdistance=args.search, removefailed=args.remove)
    else:
        chrom2 = fivec.frags['regions']['chromosome'][args.region2]
        temp = fivec.trans_heatmap(region1=args.region, region2=args.region2, binsize=args.binsize, start1=args.start,
                                 stop1=args.stop, start2=args.start2, stop2=args.stop2, datatype=args.datatype,
                                 arraytype='full', returnmapping=True, skipfiltered=True,
                                 dynamically_binned=args.dynamic, expansion_binsize=args.expbinsize,
                                 minobservations=args.minobs, searchdistance=args.search, removefailed=args.remove)
    output = open(args.output, 'w')
    if args.region2 is None:
        if args.binsize == 0:
            data = temp[0]
            xmapping = temp[1][:, :2]
            ymapping = temp[2][:, :2]
            all_data = []
            for i in range(xmapping.shape[0]):
                for j in range(ymapping.shape[0]):
                    if data[i, j, 0] <= 0.0:
                        continue
                    if xmapping[i, 0] < ymapping[j, 0]:
                        all_data.append((xmapping[i, 0], xmapping[i, 1], ymapping[j, 0], ymapping[j, 1],
                                         numpy.log2(data[i, j, 0] / data[i, j, 1])))
                    else:
                        all_data.append((ymapping[j, 0], ymapping[j, 1], xmapping[i, 0], xmapping[i, 1],
                                         numpy.log2(data[i, j, 0] / data[i, j, 1])))
            all_data = numpy.array(all_data, dtype=numpy.dtype([('start1', numpy.int32), ('stop1', numpy.int32),
                                                                ('start2', numpy.int32), ('stop2', numpy.int32),
                                                                ('value', numpy.float32)]))
            order = numpy.lexsort((all_data['start2'], all_data['start1']))
            for i in order:
                print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (chrom, all_data['start1'][i],
                    all_data['stop1'][i], chrom, all_data['start2'][i], all_data['stop2'][i], all_data['value'][i])
        else:
            data = temp[0]
            mapping = temp[1][:, :2]
            pos = 0
            for i in range(mapping.shape[0] - 1):
                for j in range(i + 1, mapping.shape[0]):
                    if data[pos, 0] > 0.0 and data[pos, 1] > 0.0:
                        print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (chrom, mapping[i, 0], mapping[i, 1],
                            chrom, mapping[j, 0], mapping[j, 1], numpy.log2(data[pos, 0] / data[pos, 1]))
                    pos += 1
    else:
        data, mapping1, mapping2 = temp
        for i in range(mapping1.shape[0]):
            for j in range(mapping2.shape[0]):
                if data[i, j, 0] <= 0.0:
                    continue
                print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (chrom, mapping1[i, 0], mapping1[i, 1], chrom2,
                                                                       mapping2[j, 0], mapping2[j, 1],
                                                                       numpy.log2(data[i, j, 0] / data[i, j, 1]))
    output.close()
    if not args.image is None:
        if args.datatype == 'enrichment':
            symmetricscaling = True
        else:
            symmetricscaling = False
        if 'symmetricscaling' in kwargs:
            symmetricscaling = kwargs['symmetricscaling']
        if not args.region2 is None:
            img, minscore, maxscore = plot_full_array(data, returnscale=True, symmetricscaling=symmetricscaling,
                                                      silent=args.silent, **kwargs)
            offset = 0.0
            width = 5.0
            height = width / mapping1.shape[0] * mapping2.shape[0]
        elif arraytype == 'compact':
            img, minscore, maxscore = plot_full_array(data, returnscale=True, symmetricscaling=symmetricscaling,
                                                      silent=args.silent, **kwargs)
            offset = 0.0
            width = 5.0
            height = width / xmapping.shape[0] * ymapping.shape[0]
        else:
            if args.rotate:
                img, minscore, maxscore = plot_diagonal_from_upper_array(data, returnscale=True,
                                          symmetricscaling=symmetricscaling, silent=args.silent, **kwargs)
                offset = 2.5 / (mapping.shape[0] * 2 - 2)
                height = 2.5
                width = 5.0
            else:
                img, minscore, maxscore = plot_upper_array(data, returnscale=True, symmetricscaling=symmetricscaling,
                                                           silent=args.silent, **kwargs)
                offset = 0.0
                height = width = 5.0
        if args.pdf:
            c = canvas.canvas()
            c1 = canvas.canvas([canvas.clip(path.rect(0, 0, width, height))])
            c1.insert(bitmap.bitmap(-offset, -offset, img, width=width))
            c.insert(c1)
            if args.region2 is None:
                if args.ticks and args.binsize > 0:
                    c.stroke(path.line(0, 0, width, 0))
                    xmin = (mapping[0, 0] + mapping[0, 1]) / 2
                    xmax = (mapping[-1, 0] + mapping[-1, 1]) / 2
                    order = int(floor(log10(xmax - xmin))) - 1
                    step = int(floor((xmax - xmin) / (10.0 ** order * width))) * 10 ** order
                    values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                    ticks = (values - float(mapping[0, 0] + mapping[0, 1]) / 2) / (mapping[-1, 0] -
                                                                                   mapping[0, 0]) * width
                    for i in range(values.shape[0]):
                        c.stroke(path.line(ticks[i], 0, ticks[i], -0.25), [style.linewidth.Thin])
                        c.text(ticks[i], -0.3, "%0.2e" % values[i],
                               [text.valign.middle, text.halign.left, text.size(-2), trafo.rotate(-90)])
                    if not args.rotate:
                        c.stroke(path.line(width, 0, width, height))
                        for i in range(values.shape[0]):
                            c.stroke(path.line(width, height - ticks[i], width + 0.25, height - ticks[i]),
                                     [style.linewidth.Thin])
                            c.text(width + 0.3, height - ticks[i], "%0.2e" % values[i],
                                   [text.valign.middle, text.halign.left, text.size(-2)])
            elif args.ticks:
                c.stroke(path.line(0, 0, width, 0))
                xmin = (mapping1[0, 0] + mapping1[0, 1]) / 2
                xmax = (mapping1[-1, 0] + mapping1[-1, 1]) / 2
                order = int(floor(log10(xmax - xmin))) - 1
                step = int(floor((xmax - xmin) / (10.0 ** order * width))) * 10 ** order
                values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                ticks = (values - float(mapping1[0, 0] + mapping1[0, 1]) / 2) / (mapping1[-1, 0] -
                                                                               mapping1[0, 0]) * width
                for i in range(values.shape[0]):
                    c.stroke(path.line(ticks[i], 0, ticks[i], -0.25), [style.linewidth.Thin])
                    c.text(ticks[i], -0.3, "%0.2e" % values[i],
                           [text.valign.middle, text.halign.left, text.size(-2), trafo.rotate(-90)])

                c.stroke(path.line(width, 0, width, height))
                xmin = (mapping2[0, 0] + mapping2[0, 1]) / 2
                xmax = (mapping2[-1, 0] + mapping2[-1, 1]) / 2
                order = int(floor(log10(xmax - xmin))) - 1
                step = int(floor((xmax - xmin) / (10.0 ** order * width))) * 10 ** order
                values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                ticks = (values - float(mapping2[0, 0] + mapping2[0, 1]) / 2) / (mapping2[-1, 0] -
                                                                               mapping2[0, 0]) * height
                for i in range(values.shape[0]):
                    c.stroke(path.line(width, height - ticks[i], width + 0.25, height - ticks[i]),
                             [style.linewidth.Thin])
                    c.text(width + 0.3, height - ticks[i], "%0.2e" % values[i],
                           [text.valign.middle, text.halign.left, text.size(-2)])
            if args.legend:
                if 'min_color' in kwargs:
                    min_color = kwargs['min_color']
                else:
                    min_color = "0000ff"
                if 'mid_color' in kwargs:
                    mid_color = kwargs['mid_color']
                else:
                    mid_color = "ffffff"
                if 'max_color' in kwargs:
                    max_color = kwargs['max_color']
                else:
                    max_color = "ff0000"
                if 'logged' in kwargs:
                    logged = kwargs['logged']
                else:
                    logged = True
                c.insert(plot_key(min_score=minscore, max_score=maxscore, height=0.25, width=width,
                                  orientation='top', num_ticks=5, min_color=min_color,
                                  mid_color=mid_color, max_color=max_color,
                                  log_display=False), [trafo.translate(0, height + 0.25)])
                if logged:
                    label = "Log2 "
                else:
                    label = ""
                if args.datatype == 'enrichment':
                    c.text(width * 0.5, height + 0.8, "%sEnrichment" % label, [text.halign.center, text.valign.bottom,
                                                                       text.size(-2)])
                elif args.datatype == 'raw':
                    c.text(width + 0.5, height + 0.8, "%sCounts" % label, [text.halign.center, text.valign.bottom,
                                                                   text.size(-2)])
                else:
                    c.text(width * 0.5, height + 0.8, "%sNormalized Counts" % label,
                           [text.halign.center, text.valign.bottom, text.size(-2)])

            c.writePDFfile(args.image)
            if len(args.image.split('.')) <= 1 or args.image.split('.')[-1] != 'pdf':
                subprocess.call('mv %s.pdf %s' % (args.image, args.image), shell=True)

        else:
            img_format = args.image.split('.')[-1].upper()
            if img_format not in ['PNG', 'TIF', 'JPG', 'JPEG']:
                img_format = 'PNG'
            img.save(args.image, img_format)

Example 100

Project: hifive
Source File: get_hic_interval.py
View license
def run(args):
    if not args.image is None and args.pdf and "pyx" not in sys.modules.keys():
        parser.error("-p/--pdf requires the package 'pyx'")
    hic = HiC(args.project, 'r', silent=args.silent)
    if 'binned' in hic.fends['/'].attrs and hic.fends['/'].attrs['binned'] is not None:
        binned = True
        chr_indices = 'bin_indices'
        fends = 'bins'
    else:
        binned = False
        chr_indices = 'chr_indices'
        fends = 'fends'
    if args.stop == 0 or args.stop is None:
        maxstop = hic.fends[fends]['stop'][hic.fends[chr_indices][hic.chr2int[args.chrom] + 1] - 1]
    else:
        maxstop = args.stop
    if args.stop is None:
        args.stop = maxstop
    if args.start is None:
        args.start = hic.fends[fends]['start'][hic.fends[chr_indices][hic.chr2int[args.chrom]]]
    if not args.chrom2 is None:
        if args.stop2 == 0 or args.stop2 is None:
            maxstop2 = hic.fends[fends]['stop'][hic.fends[chr_indices][hic.chr2int[args.chrom2] + 1] - 1]
        else:
            maxstop2 = args.stop2
        if args.stop2 is None:
            args.stop2 = maxstop2
        if args.start2 is None:
            args.start2 = hic.fends[fends]['start'][hic.fends[chr_indices][hic.chr2int[args.chrom2]]]
    else:
        if args.maxdist is None:
            args.maxdist = 0
        if args.maxdist == 0 or args.maxdist >= (maxstop - args.start) / 2:
            arraytype = 'upper'
        else:
            arraytype = 'compact'
    kwargs = {}
    for arg in args.keywords:
        temp = arg.split("=")
        if temp[1] in ["True", "TRUE", "true"]:
            temp[1] = True
        elif temp[1] in ["False", "FALSE", "false"]:
            temp[1] = False
        elif temp[1][0] == "(":
            temp[1] = temp[1].strip('()').split(',')
            for i in range(len(temp[1])):
                temp[1][i] = int(temp[1][i])
            temp[1] = tuple(temp[1])
        elif temp[1][0] == "[":
            temp[1] = temp[1].strip('[]').split(',')
            for i in range(len(temp[1])):
                temp[1][i] = int(temp[1][i])
        else:
            try:
                temp[1] = int(temp[1])
            except:
                # strip off extra characters introduced by galaxy into color format
                temp[1] = temp[1].replace('__pd__','')
        kwargs[temp[0]] = temp[1]
    if not args.chrom2 is None:
        data, mapping1, mapping2 = hic.trans_heatmap(chrom1=args.chrom, chrom2=args.chrom2, binsize=args.binsize,
                                                     start1=args.start, stop1=args.stop, start2=args.start2,
                                                     stop2=args.stop2, datatype=args.datatype,
                                                     maxdistance=args.maxdist, returnmapping=True, skipfiltered=True,
                                                     dynamically_binned=args.dynamic,
                                                     expansion_binsize=args.expbinsize, minobservations=args.minobs,
                                                     searchdistance=args.search, removefailed=args.remove)
    else:
        data, mapping = hic.cis_heatmap(chrom=args.chrom, binsize=args.binsize, start=args.start,
                                        stop=args.stop, datatype=args.datatype, arraytype=arraytype,
                                        maxdistance=args.maxdist, returnmapping=True, skipfiltered=True,
                                        dynamically_binned=args.dynamic, expansion_binsize=args.expbinsize,
                                        minobservations=args.minobs, searchdistance=args.search,
                                        removefailed=args.remove)
    output = open(args.output, 'w')
    if args.matrix:
        if args.chrom2 is None:
            diag = int(hic.binned is not None)
            if arraytype == 'upper':
                temp = numpy.zeros((mapping.shape[0], mapping.shape[0]), dtype=numpy.float64)
                indices = numpy.triu_indices(mapping.shape[0], 1 - diag)
                where = numpy.where(data[:, 1] > 0)[0]
                temp[indices[0][where], indices[1][where]] = data[where, 0] / data[where, 1]
                temp[indices[1][where], indices[0][where]] += data[where, 0] / data[where, 1]
            else:
                temp = numpy.zeros((mapping.shape[0], mapping.shape[0]), dtype=numpy.float64)
                for i in range(mapping.shape[0] - 1 + diag):
                    where = numpy.where(data[i, :, 1] > 0)[0]
                    temp[i, where + i + 1 - diag] = data[i, where, 0] / data[i, where, 1]
                indices = numpy.triu_indices(mapping.shape[0], 1 - diag)
                temp[indices[1], indices[0]] += temp[indices]
        else:
            temp = numpy.zeros((data.shape[0], data.shape[1]), dtype=numpy.float64)
            where = numpy.where(data[:, :, 1] > 0)
            temp[where] = data[where[0], where[1], 0] / data[where[0], where[1], 1]
        if args.datatype == 'raw':
            for i in range(temp.shape[0]):
                tempout = []
                for j in range(temp.shape[1]):
                    tempout.append("%i" % temp[i, j])
                print >> output, '\t'.join(tempout)
        else:
            for i in range(temp.shape[0]):
                tempout = []
                for j in range(temp.shape[1]):
                    tempout.append("%0.6f" % temp[i, j])
                print >> output, '\t'.join(tempout)
    else:
        if args.chrom2 is None:
            diag = binned
            if arraytype == 'upper':
                pos = 0
                for i in range(mapping.shape[0] - 1 + diag):
                    for j in range(i + 1 - diag, mapping.shape[0]):
                        if data[pos, 0] > 0.0 and data[pos, 1] > 0.0:
                            print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (args.chrom, mapping[i, 0],
                                                                           mapping[i, 1], args.chrom,
                                                                           mapping[j, 0], mapping[j, 1],
                                                                           numpy.log2(data[pos, 0] / data[pos, 1]))
                        pos += 1
            else:
                for i in range(mapping.shape[0] - 1 + diag):
                    for pos in range(min(mapping.shape[0] - i - 1 + diag, data.shape[1])):
                        j = i + pos + 1 - diag
                        if data[i, pos, 0] > 0.0 and data[i, pos, 1] > 0.0:
                            print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (args.chrom, mapping[i, 0],
                                                                            mapping[i, 1], args.chrom,
                                                                            mapping[j, 0], mapping[j, 1],
                                                                            numpy.log2(data[i, pos, 0] /
                                                                            data[i, pos, 1]))
        else:
            for i in range(mapping1.shape[0]):
                for j in range(mapping2.shape[0]):
                    if data[i, j, 0] > 0.0 and data[i, j, 1] > 0.0:
                        print >> output, "chr%s\t%i\t%i\tchr%s\t%i\t%i\t%f" % (args.chrom,
                                                                        mapping1[i, 0], mapping1[i, 1],
                                                                        args.chrom2, mapping2[j, 0], mapping2[j, 1],
                                                                        numpy.log2(data[i, j, 0] / data[i, j, 1]))
    output.close()
    if not args.image is None:
        width = max(5.0, (args.stop - args.start) / 1000000.)
        if args.datatype == 'enrichment':
            symmetricscaling = True
        else:
            symmetricscaling = False
        if 'symmetricscaling' in kwargs:
            symmetricscaling = kwargs['symmetricscaling']
        if not args.chrom2 is None:
            img, minscore, maxscore = plot_full_array(data, returnscale=True, symmetricscaling=symmetricscaling,
                                                      silent=args.silent, **kwargs)
            offset = 0.0
            height = (width / data.shape[0]) * data.shape[1]
        elif arraytype == 'compact':
            if args.rotate:
                img, minscore, maxscore = plot_diagonal_from_compact_array(data, returnscale=True,
                                          symmetricscaling=symmetricscaling, silent=args.silent,
                                          diagonal_included=diag, **kwargs)
                offset = width / 2. / (data.shape[0] * 2 - 1 + diag)
                height = width / (data.shape[0] * 2.0 - 2) * data.shape[1]
            else:
                img, minscore, maxscore = plot_compact_array(data, returnscale=True,
                                          symmetricscaling=symmetricscaling, silent=args.silent,
                                          diagonal_included=diag, **kwargs)
                offset = 0.0
                height = width
        else:
            if args.rotate:
                img, minscore, maxscore = plot_diagonal_from_upper_array(data, returnscale=True,
                                          symmetricscaling=symmetricscaling, silent=args.silent,
                                          diagonal_included=diag, **kwargs)
                offset = width / 2. / (mapping.shape[0] * 2 - 1 + diag)
                height = width / 2.
            else:
                img, minscore, maxscore = plot_upper_array(data, returnscale=True,
                                          symmetricscaling=symmetricscaling, silent=args.silent,
                                          diagonal_included=diag, **kwargs)
                offset = 0.0
                height = width
        if args.pdf:
            c = canvas.canvas()
            if args.chrom2 is None:
                c1 = canvas.canvas([canvas.clip(path.rect(0, 0, width, height))])
                c1.insert(bitmap.bitmap(-offset, -offset, img, width=width))
            else:
                c1 = canvas.canvas([canvas.clip(path.rect(0, 0, width, height))])
                c1.insert(bitmap.bitmap(-offset, -offset, img, width=width))
            c.insert(c1)
            if args.ticks and args.binsize > 0:
                if args.chrom2 is None:
                    c.stroke(path.line(0, 0, width, 0))
                    xmin = (mapping[0, 0] + mapping[0, 1]) / 2
                    xmax = (mapping[-1, 0] + mapping[-1, 1]) / 2
                    #order = int(floor(log10(xmax - xmin))) - 1
                    #step = int(floor((xmax - xmin) / (10.0 ** order))) * 10 ** order
                    
                    order = int(floor(log10((xmax - xmin) / (width * 2.0))))
                    step = int(floor((xmax - xmin) / (width * 2.0) / (10.0 ** order))) * 10 ** order
                    values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                    ticks = (values - float(mapping[0, 0] + mapping[0, 1]) / 2) / (mapping[-1, 0] -
                                                                                   mapping[0, 0]) * width
                    for i in range(values.shape[0]):
                        c.stroke(path.line(ticks[i], 0, ticks[i], -0.25), [style.linewidth.Thin])
                        c.text(ticks[i], -0.3, "%0.2e" % values[i],
                               [text.valign.middle, text.halign.left, text.size(-2), trafo.rotate(-90)])
                    if not args.rotate:
                        c.stroke(path.line(width, 0, width, height))
                        for i in range(values.shape[0]):
                            c.stroke(path.line(width, height - ticks[i], width + 0.25, height - ticks[i]), [style.linewidth.Thin])
                            c.text(width + 0.3, height - ticks[i], "%0.2e" % values[i], [text.valign.middle, text.halign.left,
                                                                              text.size(-2)])
                else:
                    c.stroke(path.line(0, 0, width, 0))
                    xmin = (mapping1[0, 0] + mapping1[0, 1]) / 2
                    xmax = (mapping1[-1, 0] + mapping1[-1, 1]) / 2
                    order = int(floor(log10((xmax - xmin) / (width * 2.0))))
                    step = int(floor((xmax - xmin) / (width * 2.0) / (10.0 ** order))) * 10 ** order
                    values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                    ticks = (values - float(mapping1[0, 0] + mapping1[0, 1]) / 2) / (mapping1[-1, 0] -
                                                                                     mapping1[0, 0]) * width
                    for i in range(values.shape[0]):
                        c.stroke(path.line(ticks[i], 0, ticks[i], -0.25), [style.linewidth.Thin])
                        c.text(ticks[i], -0.3, "%0.2e" % values[i],
                               [text.valign.middle, text.halign.left, text.size(-2), trafo.rotate(-90)])
                    c.stroke(path.line(0, 0, width, 0))
                    xmin = (mapping2[0, 0] + mapping2[0, 1]) / 2
                    xmax = (mapping2[-1, 0] + mapping2[-1, 1]) / 2
                    order = int(floor(log10((xmax - xmin) / (width * 2.0))))
                    step = int(floor((xmax - xmin) / (width * 2.0) / (10.0 ** order))) * 10 ** order
                    values = numpy.arange(((xmin - 1) / step + 1) * step, (xmax / step) * step + 1, step)
                    ticks = (values - float(mapping2[0, 0] + mapping2[0, 1]) / 2) / (mapping2[-1, 0] -
                                                                                     mapping2[0, 0]) * height
                    for i in range(values.shape[0]):
                        c.stroke(path.line(width, height - ticks[i], width + 0.25, height - ticks[i]), [style.linewidth.Thin])
                        c.text(width + 0.3, height - ticks[i], "%0.2e" % values[i],
                               [text.valign.middle, text.halign.left, text.size(-2)])
            if args.legend:
                if 'min_color' in kwargs:
                    min_color = kwargs['min_color']
                else:
                    min_color = "0000ff"
                if 'mid_color' in kwargs:
                    mid_color = kwargs['mid_color']
                else:
                    mid_color = "ffffff"
                if 'max_color' in kwargs:
                    max_color = kwargs['max_color']
                else:
                    max_color = "ff0000"
                if 'logged' in kwargs:
                    logged = kwargs['logged']
                else:
                    logged = True
                c.insert(plot_key(min_score=minscore, max_score=maxscore, height=0.25, width=min(5., width),
                                  orientation='top', num_ticks=5, min_color=min_color,
                                  mid_color=mid_color, max_color=max_color,
                                  log_display=False), [trafo.translate(width * 0.5 - min(2.5, width * 0.5), height + 0.25)])
                if logged:
                    label = "Log2 "
                else:
                    label = ""
                if args.datatype == 'enrichment':
                    c.text(width * 0.5, height + 0.8, "%sEnrichment" % label, [text.halign.center, text.valign.bottom,
                                                                       text.size(-2)])
                elif args.datatype == 'raw':
                    c.text(width * 0.5, height + 0.8, "%sCounts" % label, [text.halign.center, text.valign.bottom,
                                                                   text.size(-2)])
                else:
                    c.text(width * 0.5, height + 0.8, "%sNormalized Counts" % label,
                           [text.halign.center, text.valign.bottom, text.size(-2)])

            c.writePDFfile(args.image)
            if len(args.image.split('.')) <= 1 or args.image.split('.')[-1] != 'pdf':
                subprocess.call('mv %s.pdf %s' % (args.image, args.image), shell=True)

        else:
            img_format = args.image.split('.')[-1].upper()
            if img_format not in ['PNG', 'TIF', 'JPG', 'JPEG']:
                img_format = 'PNG'
            img.save(args.image, img_format)