<commit_before>'''
PipelineRrbs.py - Utility functions for analysing rrbs sequence data
=====================================================================

:Author: <NAME>
:Release: $Id$
:Date: |today|
:Tags: Python

Mapping reads is a common task in pipelines. Different pipelines
combine different sources of input (:term:`fastq` files, :term:`sra` files)
of different data (single end, paired end) with different mapping
algorithms (bowtie, tophat, stampy). This module provides utility
functions to abstract some of these variations.

The pipeline does not know what kind of data it gets (a :term:`sra` archive
might contain single end or paired end data or both).

A pipeline might get several input data (:term:`fastq` and :term:`sra`
formatted files at the same time).

The module currently is able to deal with:

   * tophat mapping against genome
   * bowtie mapping against transcriptome, genome and junctions
   * bwa against genome
   * stampy against genome

It implements:
   * .sra: paired-end and single-end
   * .fastq: paired-end and single-end
   * .csfasta: colour-space, single-end

Code
----

'''
import random
import os
import shutil
import glob
import collections
import re
import gzip
import itertools
import copy
import CGATPipelines.Pipeline as P
import logging as L
import CGAT.Experiment as E
import CGAT.IOTools as IOTools
import CGAT.GTF as GTF
import CGAT.Fastq as Fastq
import CGAT.IndexedFasta as IndexedFasta
import CGAT.Fasta as Fa
import pysam
import pandas as pd
from CGATPipelines.Pipeline import cluster_runnable
import numpy as np
import pandas.rpy.common as com
from rpy2.robjects import r
import rpy2.robjects as robj
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import FloatVector
import CGATPipelines.PipelineTracks as PipelineTracks
stats = importr('stats')


def hjoin(items):
    return "-".join(items)


@cluster_runnable
def plotReadBias(infile, outfile):
    '''take the m-bias outfile from bismark and plot read position biases'''

    def num(s):
        try:
            return int(s)
        except ValueError:
            return float(s)

    def dfFromTsvTable(lines, line, context):
        row = 2
        rows = []
        row_values = lines[line + row].strip().split("\t")
        header = [re.sub(" ", "_", re.sub("%", "perc", x))
                  for x in row_values]
        row += 1
        while lines[line + row].strip():
            print lines[line+row].strip()
            row_values = lines[line + row].strip().split("\t")
            row_values = map(num, row_values)
            rows.append(row_values)
            row += 1
        df = pd.DataFrame(rows, columns=header)<commit_msg>remove R module import to avoid import error<commit_after>'''
PipelineRrbs.py - Utility functions for analysing rrbs sequence data
=====================================================================

:Author: <NAME>
:Release: $Id$
:Date: |today|
:Tags: Python

Mapping reads is a common task in pipelines. Different pipelines
combine different sources of input (:term:`fastq` files, :term:`sra` files)
of different data (single end, paired end) with different mapping
algorithms (bowtie, tophat, stampy). This module provides utility
functions to abstract some of these variations.

The pipeline does not know what kind of data it gets (a :term:`sra` archive
might contain single end or paired end data or both).

A pipeline might get several input data (:term:`fastq` and :term:`sra`
formatted files at the same time).

The module currently is able to deal with:

   * tophat mapping against genome
   * bowtie mapping against transcriptome, genome and junctions
   * bwa against genome
   * stampy against genome

It implements:
   * .sra: paired-end and single-end
   * .fastq: paired-end and single-end
   * .csfasta: colour-space, single-end

Code
----

'''
import random
import os
import shutil
import glob
import collections
import re
import gzip
import itertools
import copy
import CGATPipelines.Pipeline as P
import logging as L
import CGAT.Experiment as E
import CGAT.IOTools as IOTools
import CGAT.GTF as GTF
import CGAT.Fastq as Fastq
import CGAT.IndexedFasta as IndexedFasta
import CGAT.Fasta as Fa
import pysam
import pandas as pd
from CGATPipelines.Pipeline import cluster_runnable
import numpy as np
import pandas.rpy.common as com
from rpy2.robjects import r
import rpy2.robjects as robj
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import FloatVector
import CGATPipelines.PipelineTracks as PipelineTracks
# AH: this causes an import error:
# AssertionError: PipelineRrbs scripts/modules - ImportError: Conflict when converting R symbol in the package "stats" to a Python symbol (format.perc -> format_perc while there is already format_perc)
# best only to import when needed in individual methods
# stats = importr('stats')


def hjoin(items):
    return "-".join(items)


@cluster_runnable
def plotReadBias(infile, outfile):
    '''take the m-bias outfile from bismark and plot read position biases'''

    def num(s):
        try:
            return int(s)
        except ValueError:
            return float(s)

    def dfFromTsvTable(lines, line, context):
        row = 2
        rows = []
        row_values = lines[line + row].strip().split("\t")
        header = [re.sub(" ", "_", re.sub("%", "perc", x))
                  for x in row_values]
        row += 1
        while lines[line + row].strip():
            print lines[line+row].strip()
            row_values = lines[line + row].strip().split("\t")
            row_values = map(num, row_values)
            rows.append(row_values)
            row += 1
        df = pd.DataFrame(rows, columns=header)
{'fraction_non_alphanumeric': 0.10797050561797752, 'fraction_numerical': 0.0024578651685393258, 'mean_word_length': 3.532219570405728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6512872', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1630, 'n_words': 637}