<commit_before># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import os
import sys
import gzip
import json
from os import path as osp
from os.path import join as pjoin
from glob import glob
from subprocess import Popen, PIPE
if sys.version_info[0] == 2:
    from itertools import izip as zip

from tqdm import tqdm
from click import group, command, option, Path, Choice

from phip.utils import compute_size_factors


# handle gzipped or uncompressed files
def open_maybe_compressed(*args, **kwargs):
    if args[0].endswith('.gz'):
        # gzip modes are different from default open modes
        if len(args[1]) == 1:
            args = (args[0], args[1] + 't') + args[2:]
        return gzip.open(*args, **kwargs)
    else:
        return open(*args, **kwargs)


@group(context_settings={'help_option_names': ['-h', '--help']})
def cli():
    """phip -- PhIP-seq analysis tools"""
    pass


@cli.command(name='truncate-fasta')
@option('-i', '--input', required=True, type=Path(exists=True, dir_okay=False),
        help='input fasta')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output fasta')
@option('-k', '--length', required=True, type=int,
        help='length of starting subsequence to extract')
def truncate_fasta(input, output, length):
    """truncate each sequence of a fasta file"""
    from Bio import SeqIO
    with open(output, 'w') as op:
        for sr in SeqIO.parse(input, 'fasta'):
            print(sr[:length].format('fasta'), end='', file=op)


@cli.command(name='merge-kallisto-tpm')
@option('-i', '--input', required=True, type=Path(exists=True, file_okay=False),
        help='input dir containing kallisto results')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output path')
def merge_kallisto_tpm(input, output):
    """merge kallisto abundance results

    Input directory should contain sample-named subdirectories, each containing
    an abundance.tsv file.  This command will generate a single tab-delim
    output file with each column containing the tpm values for that sample.
    """
    samples = os.listdir(input)
    iterators = [open(pjoin(input, s, 'abundance.tsv'), 'r') for s in samples]
    with open(output, 'w') as op:
        it = zip(*iterators)
        # burn headers of input files and write header of output file
        _ = next(it)
        print('id\t{}'.format('\t'.join(samples)), file=op)
        for lines in it:
            fields_array = [line.split('\t') for line in lines]
            # check that join column is the same
            assert all([fields[0] == fields_array[0][0] for fields in fields_array])
            merged_fields = [fields_array[0][0]] + [f[4].strip() for f in fields_array]
            print('\t'.join(merged_fields), file=op)


@cli.command(name='gamma-poisson-model')
@option('-i', '--input', required=True, type=Path(exists=True, dir_okay=False),
        help='input counts file (tab-delim)')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output directory')
@option('-t', '--trim-percentile', default=99.9,
        help='lower percent of data to keep for model fitting')
@option('-d', '--index-cols', default=1,
        help='number of columns to use as index/row-key')
def gamma_poisson_model(input, output, trim_percentile, index_cols):
    """compute -log10(pvals) with gamma-poisson model"""
    import pandas as pd
    from phip.stats import gamma_poisson_model as model
    counts = pd.read_csv(input, sep='\t', header=0, index_col=list(range(index_cols)))<commit_msg>Rename stats.py -> gampois.py<commit_after># Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import os
import sys
import gzip
import json
from os import path as osp
from os.path import join as pjoin
from glob import glob
from subprocess import Popen, PIPE
if sys.version_info[0] == 2:
    from itertools import izip as zip

from tqdm import tqdm
from click import group, command, option, Path, Choice

from phip.utils import compute_size_factors


# handle gzipped or uncompressed files
def open_maybe_compressed(*args, **kwargs):
    if args[0].endswith('.gz'):
        # gzip modes are different from default open modes
        if len(args[1]) == 1:
            args = (args[0], args[1] + 't') + args[2:]
        return gzip.open(*args, **kwargs)
    else:
        return open(*args, **kwargs)


@group(context_settings={'help_option_names': ['-h', '--help']})
def cli():
    """phip -- PhIP-seq analysis tools"""
    pass


@cli.command(name='truncate-fasta')
@option('-i', '--input', required=True, type=Path(exists=True, dir_okay=False),
        help='input fasta')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output fasta')
@option('-k', '--length', required=True, type=int,
        help='length of starting subsequence to extract')
def truncate_fasta(input, output, length):
    """truncate each sequence of a fasta file"""
    from Bio import SeqIO
    with open(output, 'w') as op:
        for sr in SeqIO.parse(input, 'fasta'):
            print(sr[:length].format('fasta'), end='', file=op)


@cli.command(name='merge-kallisto-tpm')
@option('-i', '--input', required=True, type=Path(exists=True, file_okay=False),
        help='input dir containing kallisto results')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output path')
def merge_kallisto_tpm(input, output):
    """merge kallisto abundance results

    Input directory should contain sample-named subdirectories, each containing
    an abundance.tsv file.  This command will generate a single tab-delim
    output file with each column containing the tpm values for that sample.
    """
    samples = os.listdir(input)
    iterators = [open(pjoin(input, s, 'abundance.tsv'), 'r') for s in samples]
    with open(output, 'w') as op:
        it = zip(*iterators)
        # burn headers of input files and write header of output file
        _ = next(it)
        print('id\t{}'.format('\t'.join(samples)), file=op)
        for lines in it:
            fields_array = [line.split('\t') for line in lines]
            # check that join column is the same
            assert all([fields[0] == fields_array[0][0] for fields in fields_array])
            merged_fields = [fields_array[0][0]] + [f[4].strip() for f in fields_array]
            print('\t'.join(merged_fields), file=op)


@cli.command(name='gamma-poisson-model')
@option('-i', '--input', required=True, type=Path(exists=True, dir_okay=False),
        help='input counts file (tab-delim)')
@option('-o', '--output', required=True, type=Path(exists=False),
        help='output directory')
@option('-t', '--trim-percentile', default=99.9,
        help='lower percent of data to keep for model fitting')
@option('-d', '--index-cols', default=1,
        help='number of columns to use as index/row-key')
def gamma_poisson_model(input, output, trim_percentile, index_cols):
    """compute -log10(pvals) with gamma-poisson model"""
    import pandas as pd
    from phip.gampois import gamma_poisson_model as model
    counts = pd.read_csv(input, sep='\t', header=0, index_col=list(range(index_cols)))
{'fraction_non_alphanumeric': 0.13426708524510989, 'fraction_numerical': 0.0070031393383240765, 'mean_word_length': 3.7250427837992013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3334693', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2518, 'n_words': 835}