Beispiel #1
0
def _do_water_alignment(seq1,
                        seq2,
                        out_fhand,
                        gap_open=10.0,
                        gap_extend=0.5,
                        out_fmt='markx10',
                        reverse2=False):
    seq1_fhand = NamedTemporaryFile()
    seq2_fhand = NamedTemporaryFile()

    write_seqrecs(seq1, seq1_fhand, 'fasta')
    write_seqrecs(seq2, seq2_fhand, 'fasta')
    seq1_fhand.flush()
    seq2_fhand.flush()
    cmd = [
        'water', '-asequence', seq1_fhand.name, '-bsequence', seq2_fhand.name,
        '-outfile', out_fhand.name, '-gapopen',
        str(gap_open), '-gapextend',
        str(gap_extend), '-aformat3', out_fmt
    ]
    if reverse2:
        cmd.append('-sreverse2')
    stdout = open(os.devnull, 'w')
    stderr = open(os.devnull, 'w')
    subprocess.check_call(cmd, stdout=stdout, stderr=stderr)
Beispiel #2
0
def fastaqual_to_fasta(seq_fhand, qual_fhand, out_fhand):
    'It converts a fasta and a qual file into a fastq format file'
    seqrecords = PairedFastaQualIterator(seq_fhand, qual_fhand)
    try:
        write_seqrecs(seqrecords, out_fhand.name, 'fastq')
    except ValueError, error:
        if error_quality_disagree(error):
            raise MalformedFile(str(error))
        raise
Beispiel #3
0
def fastaqual_to_fasta(seq_fhand, qual_fhand, out_fhand):
    'It converts a fasta and a qual file into a fastq format file'
    seqrecords = PairedFastaQualIterator(seq_fhand, qual_fhand)
    try:
        write_seqrecs(seqrecords, out_fhand.name, 'fastq')
    except ValueError, error:
        if error_quality_disagree(error):
            raise MalformedFile(str(error))
        raise
Beispiel #4
0
def _write_seqrecords(seqs, fhand=None, file_format='fastq'):
    'It writes a stream of sequences to a file'
    if fhand is None:
        fhand = NamedTemporaryFile(suffix='.' + file_format.replace('-', '_'))
    seqs = _clean_seqrecord_stream(seqs)
    try:
        write_seqrecs(seqs, fhand, file_format)
    except IOError, error:
        # The pipe could be already closed
        if not 'Broken pipe' in str(error):
            raise
Beispiel #5
0
def _write_seqrecords(seqs, fhand=None, file_format='fastq'):
    'It writes a stream of sequences to a file'
    if fhand is None:
        fhand = NamedTemporaryFile(suffix='.' + file_format.replace('-', '_'))
    seqs = _clean_seqrecord_stream(seqs)
    try:
        write_seqrecs(seqs, fhand, file_format)
    except IOError, error:
        # The pipe could be already closed
        if not 'Broken pipe' in str(error):
            raise
Beispiel #6
0
def _do_water_alignment(seq1, seq2,  out_fhand, gap_open=10.0, gap_extend=0.5,
                        out_fmt='markx10', reverse2=False):
    seq1_fhand = NamedTemporaryFile()
    seq2_fhand = NamedTemporaryFile()

    write_seqrecs(seq1, seq1_fhand, 'fasta')
    write_seqrecs(seq2, seq2_fhand, 'fasta')
    seq1_fhand.flush()
    seq2_fhand.flush()
    cmd = ['water', '-asequence', seq1_fhand.name, '-bsequence',
           seq2_fhand.name, '-outfile', out_fhand.name, '-gapopen',
           str(gap_open), '-gapextend', str(gap_extend), '-aformat3', out_fmt]
    if reverse2:
        cmd.append('-sreverse2')
    stdout = open(os.devnull, 'w')
    stderr = open(os.devnull, 'w')
    subprocess.check_call(cmd, stdout=stdout, stderr=stderr)
Beispiel #7
0
def seqio(in_fhands, out_fhand, out_format, copy_if_same_format=True):
    'It converts sequence files between formats'
    if out_format not in get_setting('SUPPORTED_OUTPUT_FORMATS'):
        raise IncompatibleFormatError("This output format is not supported")

    in_formats = [remove_multiline(guess_format(fhand)) for fhand in in_fhands]

    if len(in_fhands) == 1 and in_formats[0] == out_format:
        if copy_if_same_format:
            copyfileobj(in_fhands[0], out_fhand)
        else:
            rel_symlink(in_fhands[0].name, out_fhand.name)
    else:
        seqs = _read_seqrecords(in_fhands)
        try:
            write_seqrecs(seqs, out_fhand, out_format)
        except ValueError, error:
            if error_quality_disagree(error):
                raise MalformedFile(str(error))
            if 'No suitable quality scores' in str(error):
                msg = 'No qualities available to write output file'
                raise IncompatibleFormatError(msg)
            raise
Beispiel #8
0
def seqio(in_fhands, out_fhand, out_format, copy_if_same_format=True):
    'It converts sequence files between formats'
    if out_format not in get_setting('SUPPORTED_OUTPUT_FORMATS'):
        raise IncompatibleFormatError("This output format is not supported")

    in_formats = [get_format(fhand) for fhand in in_fhands]

    if len(in_fhands) == 1 and in_formats[0] == out_format:
        if copy_if_same_format:
            copyfileobj(in_fhands[0], out_fhand)
        else:
            rel_symlink(in_fhands[0].name, out_fhand.name)
    else:
        seqs = _read_seqrecords(in_fhands)
        try:
            write_seqrecs(seqs, out_fhand, out_format)
        except ValueError, error:
            if error_quality_disagree(error):
                raise MalformedFile(str(error))
            if 'No suitable quality scores' in str(error):
                msg = 'No qualities available to write output file'
                raise IncompatibleFormatError(msg)
            raise