<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

"""
Automate genome assembly by iterating assembly on a set of files, individually.
This is useful for BAC pool assembly where you want to assemble per pool.
"""

import os
import os.path as op
import sys
import logging

from jcvi.utils.iter import grouper
from jcvi.formats.base import LineFile, write_file
from jcvi.formats.fastq import first, pairspf
from jcvi.apps.softlink import get_abs_path
from jcvi.apps.base import OptionParser, ActionDispatcher, debug, need_update, \
            mkdir, sh, glob
debug()


class Meta (object):

    def __init__(self, fastq, guess=True):
        # Note the guesswork is largely based on JIRA LIMS naming convention
        self.fastq = fastq.strip()
        self.suffix = op.splitext(fastq)[-1]
        if ".1." in fastq or ".2." in fastq:
            paired = ".1" if ".1." in fastq else ".2"
        elif "_R1_" in fastq or "_R2_" in fastq:
            paired = ".1" if "_R1_" in fastq else ".2"
        else:
            paired = ""
        self.paired = paired
        if guess:
            self.guess()

    def __str__(self):
        return "\t".join((self.genome, self.tag, self.fastq))

    @property
    def link(self):
        linkname = "{0}{1}{2}".format(self.tag, self.paired, self.suffix)
        return op.join(self.genome, linkname)

    def make_link(self, firstN=0):
        mkdir(self.genome)
        if firstN > 0:
            first([str(firstN), self.fastq, "--outfile={0}".format(self.link)])
            return

        if op.islink(self.link):
            os.unlink(self.link)
        os.symlink(get_abs_path(self.fastq), self.link)

    def guess(self):
        # Try to guess library info based on file name
        # SUBAC47-MP-IL73-1_CGGAAT_L001_R1_filtered.fastq
        basename = op.basename(self.fastq)
        baseparts = basename.split("-")
        self.genome = baseparts[0]
        self.tag = baseparts[1]

        if self.genome.endswith("BP"):
            self.genome, bp = self.genome[:-5], self.genome[-5:-2]
            self.tag = "-".join((self.tag, bp))  # 500BP


class MetaFile (LineFile):

    def __init__(self, filename):
        super(MetaFile, self).__init__(filename)
        fp = open(filename)
        for row in fp:
            genome, tag, fastq = row.split()
            m = Meta(fastq, guess=False)
            m.genome, m.tag = genome, tag
            self.append(m)

        self.sort(key=lambda x: (x.genome, x.tag, x.fastq))


def main():

    actions = (
        ('tophat', 'run tophat on a list of inputs'),
        ('prepare', 'parse JIRA report and prepare input'),
        ('pairs', 'estimate insert sizes for input files'),
        ('contamination', 'remove contaminated reads'),
        ('allpaths', 'run automated ALLPATHS on list of dirs'),
        ('allpathsX', 'run automated ALLPATHS on list of files'),
        ('soapX', 'run automated SOAP on list of files'),
        ('correctX', 'run automated ALLPATHS correction on list of files'),
            )
    p = ActionDispatcher(actions)
    p.dispatch(globals())


def contamination(args):
    """
    %prog contamination folder Ecoli.fasta

    Remove contaminated reads.
    """
    from jcvi.apps.bowtie import align

    p = OptionParser(contamination.__doc__)
    p.add_option("--mapped", default=False, action="store_true",
                 help="Retain contaminated reads instead [default: %default]")
    p.set_cutoff(cutoff=800)
    p.set_mateorientation()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    folder, ecoli = args
    ecoli = get_abs_path(ecoli)
    tag = "--mapped" if opts.mapped else "--unmapped"
    for p, pf in iter_project(folder, 2):
        align_opts = [ecoli] + p + ["--bam", tag]
        align_opts += ["--cutoff={0}".format(opts.cutoff)]
        if opts.mateorientation:
            align_opts += ["--mateorientation={0}".format(opts.mateorientation)]
        samfile, logfile = align(align_opts)


def tophat(args):
    """
    %prog tophat folder reference

    Run tophat on a folder of reads.
    """
    p = OptionParser(tophat.__doc__)
    p.add_option("--gtf", help="Reference annotation [default: %default]")
    p.add_option("--intron", default=15000, type="int",
                 help="Max intron size [default: %default]")
    p.set_cpus()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    folder, reference = args
    cwd = os.getcwd()
    for p, prefix in iter_project(folder, 2):
        a, b = p
        outdir = "{0}_tophat".format(prefix)
        outfile = op.join(outdir, "accepted_hits.bam")
        if not need_update(p, outfile):
            logging.debug("File `{0}` found. Skipping.".format(outfile))
            continue

        cmd = "tophat -p {0}".format(opts.cpus)
        if opts.gtf:
            cmd += " -G {0}".format(opts.gtf)
        cmd += " --max-intron-length {0}".format(opts.intron)
        cmd += " -o {0}".format(outdir)
        cmd += " {0} {1} {2}".format(reference, a, b)
        sh(cmd)


def pairs(args):
    """<commit_msg>add two tophat options in assembly.automaton.tophat()<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

"""
Automate genome assembly by iterating assembly on a set of files, individually.
This is useful for BAC pool assembly where you want to assemble per pool.
"""

import os
import os.path as op
import sys
import logging

from jcvi.utils.iter import grouper
from jcvi.formats.base import LineFile, write_file
from jcvi.formats.fastq import first, pairspf
from jcvi.apps.softlink import get_abs_path
from jcvi.apps.base import OptionParser, ActionDispatcher, debug, need_update, \
            mkdir, sh, glob
debug()


class Meta (object):

    def __init__(self, fastq, guess=True):
        # Note the guesswork is largely based on JIRA LIMS naming convention
        self.fastq = fastq.strip()
        self.suffix = op.splitext(fastq)[-1]
        if ".1." in fastq or ".2." in fastq:
            paired = ".1" if ".1." in fastq else ".2"
        elif "_R1_" in fastq or "_R2_" in fastq:
            paired = ".1" if "_R1_" in fastq else ".2"
        else:
            paired = ""
        self.paired = paired
        if guess:
            self.guess()

    def __str__(self):
        return "\t".join((self.genome, self.tag, self.fastq))

    @property
    def link(self):
        linkname = "{0}{1}{2}".format(self.tag, self.paired, self.suffix)
        return op.join(self.genome, linkname)

    def make_link(self, firstN=0):
        mkdir(self.genome)
        if firstN > 0:
            first([str(firstN), self.fastq, "--outfile={0}".format(self.link)])
            return

        if op.islink(self.link):
            os.unlink(self.link)
        os.symlink(get_abs_path(self.fastq), self.link)

    def guess(self):
        # Try to guess library info based on file name
        # SUBAC47-MP-IL73-1_CGGAAT_L001_R1_filtered.fastq
        basename = op.basename(self.fastq)
        baseparts = basename.split("-")
        self.genome = baseparts[0]
        self.tag = baseparts[1]

        if self.genome.endswith("BP"):
            self.genome, bp = self.genome[:-5], self.genome[-5:-2]
            self.tag = "-".join((self.tag, bp))  # 500BP


class MetaFile (LineFile):

    def __init__(self, filename):
        super(MetaFile, self).__init__(filename)
        fp = open(filename)
        for row in fp:
            genome, tag, fastq = row.split()
            m = Meta(fastq, guess=False)
            m.genome, m.tag = genome, tag
            self.append(m)

        self.sort(key=lambda x: (x.genome, x.tag, x.fastq))


def main():

    actions = (
        ('tophat', 'run tophat on a list of inputs'),
        ('prepare', 'parse JIRA report and prepare input'),
        ('pairs', 'estimate insert sizes for input files'),
        ('contamination', 'remove contaminated reads'),
        ('allpaths', 'run automated ALLPATHS on list of dirs'),
        ('allpathsX', 'run automated ALLPATHS on list of files'),
        ('soapX', 'run automated SOAP on list of files'),
        ('correctX', 'run automated ALLPATHS correction on list of files'),
            )
    p = ActionDispatcher(actions)
    p.dispatch(globals())


def contamination(args):
    """
    %prog contamination folder Ecoli.fasta

    Remove contaminated reads.
    """
    from jcvi.apps.bowtie import align

    p = OptionParser(contamination.__doc__)
    p.add_option("--mapped", default=False, action="store_true",
                 help="Retain contaminated reads instead [default: %default]")
    p.set_cutoff(cutoff=800)
    p.set_mateorientation()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    folder, ecoli = args
    ecoli = get_abs_path(ecoli)
    tag = "--mapped" if opts.mapped else "--unmapped"
    for p, pf in iter_project(folder, 2):
        align_opts = [ecoli] + p + ["--bam", tag]
        align_opts += ["--cutoff={0}".format(opts.cutoff)]
        if opts.mateorientation:
            align_opts += ["--mateorientation={0}".format(opts.mateorientation)]
        samfile, logfile = align(align_opts)


def tophat(args):
    """
    %prog tophat folder reference

    Run tophat on a folder of reads.
    """
    p = OptionParser(tophat.__doc__)
    p.add_option("--gtf", help="Reference annotation [default: %default]")
    p.add_option("--intron", default=15000, type="int",
                 help="Max intron size [default: %default]")
    p.add_option("--dist", default=-50, type="int",
                 help="Mate inner distance [default: %default]")
    p.add_option("--stdev", default=50, type="int",
                 help="Mate standard deviation [default: %default]")
    p.set_cpus()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    folder, reference = args
    cwd = os.getcwd()
    for p, prefix in iter_project(folder, 2):
        a, b = p
        outdir = "{0}_tophat".format(prefix)
        outfile = op.join(outdir, "accepted_hits.bam")
        if not need_update(p, outfile):
            logging.debug("File `{0}` found. Skipping.".format(outfile))
            continue

        cmd = "tophat -p {0}".format(opts.cpus)
        if opts.gtf:
            cmd += " -G {0}".format(opts.gtf)
        cmd += " --max-intron-length {0}".format(opts.intron)
        cmd += " --mate-inner-dist {0}".format(opts.dist)
        cmd += " --mate-std-dev {0}".format(opts.stdev)
        cmd += " -o {0}".format(outdir)
        cmd += " {0} {1} {2}".format(reference, a, b)
        sh(cmd)


def pairs(args):
    """
{'fraction_non_alphanumeric': 0.14029990627928773, 'fraction_numerical': 0.011433926897844424, 'mean_word_length': 2.386543954300222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7619911', 'n_tokens_mistral': 3633, 'n_tokens_neox': 3434, 'n_words': 911}