# Path: deepcpg/utils.py
# EPS = 10e-8
# 
# def linear_weights(length, start=0.1):
#     """Create linear-triangle weights.
# 
#     Create array `x` of length `length` with linear weights, where the weight is
#     highest (one) for the center x[length//2] and lowest (`start` ) at the ends
#     x[0] and x[-1].
# 
#     Parameters
#     ----------
#     length: int
#         Length of the weight array.
#     start: float
#         Minimum weights.
# 
#     Returns
#     -------
#     :class:`np.ndarray`
#         Array of length `length` with weight.
#     """
#     weights = np.linspace(start, 1, np.ceil(length / 2))
#     tmp = weights
#     if length % 2:
#         tmp = tmp[:-1]
#     weights = np.hstack((weights, tmp[::-1]))
#     return weights
# 
# def make_dir(dirname):
#     """Create directory `dirname` if non-existing.
# 
#     Parameters
#     ----------
#     dirname: str
#         Path of directory to be created.
# 
#     Returns
#     -------
#     bool
#         `True`, if directory did not exist and was created.
#     """
#     if os.path.exists(dirname):
#         return False
#     else:
#         os.makedirs(dirname)
#         return True
# 
# Path: deepcpg/data/dna.py
# CHAR_TO_INT = OrderedDict([('A', 0), ('T', 1), ('G', 2), ('C', 3), ('N', 4)])
# INT_TO_CHAR = {v: k for k, v in CHAR_TO_INT.items()}
# def get_alphabet(special=False, reverse=False):
# def char_to_int(seq):
# def int_to_char(seq, join=True):
# def int_to_onehot(seqs, dim=4):
# def onehot_to_int(seqs, axis=-1):
# 
# Path: deepcpg/motifs.py
# def read_meme_db(meme_db_file):
#     """Read MEME database as Pandas DataFrame.
# 
#     Parameters
#     ----------
#     meme_db_file: str
#         File name of MEME database.
# 
#     Returns
#     -------
#     :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` with columns 'id', 'protein', 'url'.
#     """
#     motifs = []
#     motif = None
#     for line in open(meme_db_file):
#         if line.startswith('MOTIF'):
#             if motif:
#                 motifs.append(motif)
#                 motif = None
#             tmp = line.split()[1:]
#             if len(tmp) < 2:
#                 continue
#             motif = OrderedDict()
#             motif['id'] = tmp[0]
#             protein = re.sub(r'\(([^)]+)\)', r'\1', tmp[1])
#             motif['protein'] = protein.split('_')[0]
#             motif['url'] = ''
#         elif motif and line.startswith('URL'):
#             motif['url'] = line.split()[1]
#     if motif:
#         motifs.append(motif)
#     for i, motif in enumerate(motifs):
#         motifs[i] = pd.DataFrame(motif, index=[0])
#     motifs = pd.concat(motifs)
#     return motifs
# 
# def get_report(filter_stats_file, tomtom_file, meme_motifs):
#     """Read and join `filter_stats_file` and `tomtom_file`.
# 
#     Used by `dcpg_filter_motifs.py` to read and join output files.
# 
#     Paramters
#     ---------
#     filter_stats_file: str
#         Path of stats file created with `dcpg_filter_motifs.py`.
#     tomtom_file: str
#         Path of Tomtom output file.
#     meme_motifs: :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` from `read_meme_db`.
# 
#     Returns
#     -------
#     :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` with columns from Tomtom and statistic file.
#     """
#     filter_stats = pd.read_table(filter_stats_file)
#     tomtom = read_tomtom(tomtom_file)
#     tomtom = tomtom.sort_values(['idx', 'q-value', 'e-value'])
#     tomtom = tomtom.loc[:, ~tomtom.columns.isin(['query id', 'optimal offset'])]
#     d = pd.merge(filter_stats, tomtom, on='idx', how='outer')
#     meme_motifs = meme_motifs.rename(columns={'id': 'target id'})
#     d = pd.merge(d, meme_motifs, on='target id', how='left')
#     d.index.name = None
#     return d


from collections import OrderedDict
from sklearn.decomposition import PCA
from six.moves import range
from matplotlib import pyplot as plt
from deepcpg.utils import EPS, linear_weights, make_dir
from deepcpg.data import dna
from deepcpg.motifs import read_meme_db, get_report
import sys
import os
import os.path as pt
import argparse
import h5py as h5
import logging
import numpy as np
import pandas as pd
import six
import subprocess
import matplotlib as mpl
import seaborn as sns

        # Create output directories
        make_dir(opts.out_dir)
        sub_dirs = dict()
        names = ['logos', 'fa']
        if opts.plot_dens:
            names.append('dens')
        if opts.plot_heat:
            names.append('heat')
        if opts.motif_dbs:
            names.append('tomtom')
        for name in names:
            dirname = pt.join(opts.out_dir, name)
            sub_dirs[name] = dirname
            make_dir(dirname)

        meme_filename = pt.join(opts.out_dir, 'meme.txt')
        meme_file = open_meme(meme_filename, seqs)

        if opts.plot_pca:
            tmp = min(len(filters_act), opts.nb_sample_pca)
            log.info('Performing PCA on activations using %d samples' % tmp)
            # Down-sample activations to at most nb_sample_pca samples to reduce
            # memory usage and run-time.
            pca_act = filters_act[:tmp, :, filters_idx]

            act = pca_act.mean(axis=1)
            tmp = self.plot_filename(opts.out_dir, 'pca_mean')
            plot_pca(act, labels=filters_idx, filename=tmp)


# Path: deepcpg/utils.py
# EPS = 10e-8
# 
# def linear_weights(length, start=0.1):
#     """Create linear-triangle weights.
# 
#     Create array `x` of length `length` with linear weights, where the weight is
#     highest (one) for the center x[length//2] and lowest (`start` ) at the ends
#     x[0] and x[-1].
# 
#     Parameters
#     ----------
#     length: int
#         Length of the weight array.
#     start: float
#         Minimum weights.
# 
#     Returns
#     -------
#     :class:`np.ndarray`
#         Array of length `length` with weight.
#     """
#     weights = np.linspace(start, 1, np.ceil(length / 2))
#     tmp = weights
#     if length % 2:
#         tmp = tmp[:-1]
#     weights = np.hstack((weights, tmp[::-1]))
#     return weights
# 
# def make_dir(dirname):
#     """Create directory `dirname` if non-existing.
# 
#     Parameters
#     ----------
#     dirname: str
#         Path of directory to be created.
# 
#     Returns
#     -------
#     bool
#         `True`, if directory did not exist and was created.
#     """
#     if os.path.exists(dirname):
#         return False
#     else:
#         os.makedirs(dirname)
#         return True
# 
# Path: deepcpg/data/dna.py
# CHAR_TO_INT = OrderedDict([('A', 0), ('T', 1), ('G', 2), ('C', 3), ('N', 4)])
# INT_TO_CHAR = {v: k for k, v in CHAR_TO_INT.items()}
# def get_alphabet(special=False, reverse=False):
# def char_to_int(seq):
# def int_to_char(seq, join=True):
# def int_to_onehot(seqs, dim=4):
# def onehot_to_int(seqs, axis=-1):
# 
# Path: deepcpg/motifs.py
# def read_meme_db(meme_db_file):
#     """Read MEME database as Pandas DataFrame.
# 
#     Parameters
#     ----------
#     meme_db_file: str
#         File name of MEME database.
# 
#     Returns
#     -------
#     :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` with columns 'id', 'protein', 'url'.
#     """
#     motifs = []
#     motif = None
#     for line in open(meme_db_file):
#         if line.startswith('MOTIF'):
#             if motif:
#                 motifs.append(motif)
#                 motif = None
#             tmp = line.split()[1:]
#             if len(tmp) < 2:
#                 continue
#             motif = OrderedDict()
#             motif['id'] = tmp[0]
#             protein = re.sub(r'\(([^)]+)\)', r'\1', tmp[1])
#             motif['protein'] = protein.split('_')[0]
#             motif['url'] = ''
#         elif motif and line.startswith('URL'):
#             motif['url'] = line.split()[1]
#     if motif:
#         motifs.append(motif)
#     for i, motif in enumerate(motifs):
#         motifs[i] = pd.DataFrame(motif, index=[0])
#     motifs = pd.concat(motifs)
#     return motifs
# 
# def get_report(filter_stats_file, tomtom_file, meme_motifs):
#     """Read and join `filter_stats_file` and `tomtom_file`.
# 
#     Used by `dcpg_filter_motifs.py` to read and join output files.
# 
#     Paramters
#     ---------
#     filter_stats_file: str
#         Path of stats file created with `dcpg_filter_motifs.py`.
#     tomtom_file: str
#         Path of Tomtom output file.
#     meme_motifs: :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` from `read_meme_db`.
# 
#     Returns
#     -------
#     :class:`pandas.DataFrame`
#         :class:`pandas.DataFrame` with columns from Tomtom and statistic file.
#     """
#     filter_stats = pd.read_table(filter_stats_file)
#     tomtom = read_tomtom(tomtom_file)
#     tomtom = tomtom.sort_values(['idx', 'q-value', 'e-value'])
#     tomtom = tomtom.loc[:, ~tomtom.columns.isin(['query id', 'optimal offset'])]
#     d = pd.merge(filter_stats, tomtom, on='idx', how='outer')
#     meme_motifs = meme_motifs.rename(columns={'id': 'target id'})
#     d = pd.merge(d, meme_motifs, on='target id', how='left')
#     d.index.name = None
#     return d


# Path: scripts/dcpg_filter_motifs.py
from collections import OrderedDict
from sklearn.decomposition import PCA
from six.moves import range
from matplotlib import pyplot as plt
from deepcpg.utils import EPS, linear_weights, make_dir
from deepcpg.data import dna
from deepcpg.motifs import read_meme_db, get_report
import sys
import os
import os.path as pt
import argparse
import h5py as h5
import logging
import numpy as np
import pandas as pd
import six
import subprocess
import matplotlib as mpl
import seaborn as sns


        # Create output directories
        make_dir(opts.out_dir)
        sub_dirs = dict()
        names = ['logos', 'fa']
        if opts.plot_dens:
            names.append('dens')
        if opts.plot_heat:
            names.append('heat')
        if opts.motif_dbs:
            names.append('tomtom')
        for name in names:
            dirname = pt.join(opts.out_dir, name)
            sub_dirs[name] = dirname
            make_dir(dirname)

        meme_filename = pt.join(opts.out_dir, 'meme.txt')
        meme_file = open_meme(meme_filename, seqs)

        if opts.plot_pca:
            tmp = min(len(filters_act), opts.nb_sample_pca)
            log.info('Performing PCA on activations using %d samples' % tmp)
            # Down-sample activations to at most nb_sample_pca samples to reduce
            # memory usage and run-time.
            pca_act = filters_act[:tmp, :, filters_idx]

            act = pca_act.mean(axis=1)
            tmp = self.plot_filename(opts.out_dir, 'pca_mean')
            plot_pca(act, labels=filters_idx, filename=tmp)


            weights = linear_weights(pca_act.shape[1])