<commit_before>#!/usr/bin/env python
"""
Merges a (cs)fasta file and a qual file into a FASTQ file (on stdout)

This assumes that the fasta and qual files have the same reads in the same
order (so we don't have to load all the data and merge it).
"""

import sys
import os
from support.eta import ETA


def usage():
    print __doc__
    print """
Usage: fastqutils fromfasta {opts} filename.[cs]fasta [filename.qual]

Options:
  -q val       Use a constant value for quality (Phred char; e.g. '*' = 10)
  -tag tag     add a tag as a suffix to all read names like: readname:suffix
"""
    sys.exit(-1)


def qual_to_phred(qual):
    if type(qual) is str:
        qual = qual.strip().split()
    elif type(qual) is not type([]):
        qual = [qual, ]

    quals = []
    for q in qual:
        if not q:
            q = 0
        else:
            try:
                q = int(q)
            except ValueError, e:
                print e
                print "Line: %s" % qual
                sys.exit(2)
            if q > 93:
                q = 93
            elif q < 0:
                q = 0
        quals.append(q)

    return ''.join(['%c' % (q + 33) for q in quals])


def getline(fs):
    line = fs.readline()
    while line and line.startswith('#'):
        line = fs.readline()
    if line:
        line = line.strip()
    return line


def merge_files(fasta, qual, suffix=None, common_qual=None):
    sys.stderr.write('Merging %s and %s\n' % (os.path.basename(fasta), os.path.basename(qual) if qual else common_qual))
    f = open(fasta)

    if qual:
        q = open(qual)
    elif common_qual:
        q = None
    else:
        sys.stderr.write('Must specify a common qual value or a qual file!')
        sys.exit(-1)

    f_line = getline(f)
    if q:<commit_msg>added support for gzipped fasta and qual files<commit_after>#!/usr/bin/env python
"""
Merges a (cs)fasta file and a qual file into a FASTQ file (on stdout)

This assumes that the fasta and qual files have the same reads in the same
order (so we don't have to load all the data and merge it).
"""

import sys
import os
import gzip
from support.eta import ETA


def usage():
    print __doc__
    print """
Usage: fastqutils fromfasta {opts} filename.[cs]fasta [filename.qual]

Options:
  -q val       Use a constant value for quality (Phred char; e.g. '*' = 10)
  -tag tag     add a tag as a suffix to all read names like: readname:suffix
"""
    sys.exit(-1)


def qual_to_phred(qual):
    if type(qual) is str:
        qual = qual.strip().split()
    elif type(qual) is not type([]):
        qual = [qual, ]

    quals = []
    for q in qual:
        if not q:
            q = 0
        else:
            try:
                q = int(q)
            except ValueError, e:
                print e
                print "Line: %s" % qual
                sys.exit(2)
            if q > 93:
                q = 93
            elif q < 0:
                q = 0
        quals.append(q)

    return ''.join(['%c' % (q + 33) for q in quals])


def getline(fs):
    line = fs.readline()
    while line and line.startswith('#'):
        line = fs.readline()
    if line:
        line = line.strip()
    return line


def merge_files(fasta, qual, suffix=None, common_qual=None):
    sys.stderr.write('Merging %s and %s\n' % (os.path.basename(fasta), os.path.basename(qual) if qual else common_qual))
    if fasta.lower()[-3:] == '.gz':
        f = gzip.open(fasta)
    else:
        f = open(fasta)

    if qual:
        if qual.lower()[-3:] == '.gz':
            q = gzip.open(qual)
        else:
            q = open(qual)
    elif common_qual:
        q = None
    else:
        sys.stderr.write('Must specify a common qual value or a qual file!')
        sys.exit(-1)

    f_line = getline(f)
    if q:
{'fraction_non_alphanumeric': 0.11755361397934869, 'fraction_numerical': 0.007942811755361398, 'mean_word_length': 1.8708206686930091, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6249900', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1234, 'n_words': 432}