def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception as e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        input_filename = sys.argv[2]
    except Exception as e:
        maf_utilities.tool_fail("Error reading MAF filename: %s" % e)
    try:
        file_out = open(sys.argv[3], 'w')
    except Exception as e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)

    if species:
        print "Restricted to species: %s" % ', '.join(species)
    else:
        print "Not restricted to species."

    if not species:
        try:
            species = maf_utilities.get_species_in_maf(input_filename)
        except Exception as e:
            maf_utilities.tool_fail(
                "Error determining species in input MAF: %s" % e)

    for spec in species:
        file_out.write(">" + spec + "\n")
        try:
            for start_block in maf.Reader(open(input_filename, 'r')):
                for block in maf_utilities.iter_blocks_split_by_species(
                        start_block):
                    block.remove_all_gap_columns()  # remove extra gaps
                    component = block.get_component_by_src_start(
                        spec
                    )  # blocks only have one occurrence of a particular species, so this is safe
                    if component:
                        file_out.write(component.text)
                    else:
                        file_out.write("-" * block.text_size)
        except Exception as e:
            maf_utilities.tool_fail(
                "Your MAF file appears to be malformed: %s" % e)
        file_out.write("\n")
    file_out.close()
def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception as e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        input_filename = sys.argv[2]
    except Exception as e:
        maf_utilities.tool_fail("Error reading MAF filename: %s" % e)
    try:
        file_out = open(sys.argv[3], 'w')
    except Exception as e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)

    if species:
        print("Restricted to species: %s" % ', '.join(species))
    else:
        print("Not restricted to species.")

    if not species:
        try:
            species = maf_utilities.get_species_in_maf(input_filename)
        except Exception as e:
            maf_utilities.tool_fail("Error determining species in input MAF: %s" % e)

    for spec in species:
        file_out.write(">" + spec + "\n")
        try:
            for start_block in maf.Reader(open(input_filename, 'r')):
                for block in maf_utilities.iter_blocks_split_by_species(start_block):
                    block.remove_all_gap_columns()  # remove extra gaps
                    component = block.get_component_by_src_start(spec)  # blocks only have one occurrence of a particular species, so this is safe
                    if component:
                        file_out.write(component.text)
                    else:
                        file_out.write("-" * block.text_size)
        except Exception as e:
            maf_utilities.tool_fail("Your MAF file appears to be malformed: %s" % e)
        file_out.write("\n")
    file_out.close()
def __main__():
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception as e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
    try:
        out = maf.Writer( open( sys.argv[2], "w") )
    except Exception as e:
        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
    try:
        collapse_columns = string_as_bool( sys.argv[3] )
    except Exception as e:
        maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )

    start_count = 0
    end_count = 0
    for start_count, start_block in enumerate( maf_reader ):
        for block in maf_utilities.iter_blocks_split_by_species( start_block ):
            if collapse_columns:
                block.remove_all_gap_columns()
            out.write( block )
            end_count += 1
    out.close()

    if end_count:
        print "%i alignment blocks created from %i original blocks." % ( end_count, start_count + 1 )
    else:
        print "No alignment blocks were created."
def __main__():
    try:
        maf_reader = maf.Reader(open(sys.argv[1]))
    except Exception as e:
        maf_utilities.tool_fail("Error opening input MAF: %s" % e)
    try:
        file_out = open(sys.argv[2], 'w')
    except Exception as e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)
    try:
        species = maf_utilities.parse_species_option(sys.argv[3])
        if species:
            num_species = len(species)
        else:
            num_species = 0
    except Exception as e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        partial = sys.argv[4]
    except Exception as e:
        maf_utilities.tool_fail("Error determining keep partial value: %s" % e)

    if species:
        print("Restricted to species: %s" % ', '.join(species))
    else:
        print("Not restricted to species.")

    for block_num, block in enumerate(maf_reader):
        if species:
            block = block.limit_to_species(species)
            if len(maf_utilities.get_species_in_block(
                    block)) < num_species and partial == "partial_disallowed":
                continue
        spec_counts = {}
        for component in block.components:
            spec, chrom = maf_utilities.src_split(component.src)
            if spec not in spec_counts:
                spec_counts[spec] = 0
            else:
                spec_counts[spec] += 1
            file_out.write("%s\n" % maf_utilities.get_fasta_header(
                component, {
                    'block_index': block_num,
                    'species': spec,
                    'sequence_index': spec_counts[spec]
                },
                suffix="%s_%i_%i" % (spec, block_num, spec_counts[spec])))
            file_out.write("%s\n" % component.text)
        file_out.write("\n")
    file_out.close()
def __main__():
    try:
        maf_reader = maf.Reader(open(sys.argv[1]))
    except Exception as e:
        maf_utilities.tool_fail("Error opening input MAF: %s" % e)
    try:
        file_out = open(sys.argv[2], 'w')
    except Exception as e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)
    try:
        species = maf_utilities.parse_species_option(sys.argv[3])
        if species:
            num_species = len(species)
        else:
            num_species = 0
    except Exception as e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        partial = sys.argv[4]
    except Exception as e:
        maf_utilities.tool_fail("Error determining keep partial value: %s" % e)

    if species:
        print("Restricted to species: %s" % ', '.join(species))
    else:
        print("Not restricted to species.")

    for block_num, block in enumerate(maf_reader):
        if species:
            block = block.limit_to_species(species)
            if len(maf_utilities.get_species_in_block(block)) < num_species and partial == "partial_disallowed":
                continue
        spec_counts = {}
        for component in block.components:
            spec, chrom = maf_utilities.src_split(component.src)
            if spec not in spec_counts:
                spec_counts[spec] = 0
            else:
                spec_counts[spec] += 1
            d = OrderedDict([('block_index', block_num), ('species', spec), ('sequence_index', spec_counts[spec])])
            file_out.write("%s\n" % maf_utilities.get_fasta_header(component, d, suffix="%s_%i_%i" % (spec, block_num, spec_counts[spec])))
            file_out.write("%s\n" % component.text)
        file_out.write("\n")
    file_out.close()
def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception, e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
pkg_resources.require("bx-python")
from bx.align import maf
from galaxy.tools.util import maf_utilities

assert sys.version_info[:2] >= (2, 4)


def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception, e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        input_filename = sys.argv[2]
    except Exception, e:
        maf_utilities.tool_fail("Error reading MAF filename: %s" % e)
    try:
        file_out = open(sys.argv[3], "w")
    except Exception, e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)

    if species:
        print "Restricted to species: %s" % ", ".join(species)
    else:
        print "Not restricted to species."

    if not species:
        try:
            species = maf_utilities.get_species_in_maf(input_filename)
        except Exception, e:
            maf_utilities.tool_fail("Error determining species in input MAF: %s" % e)
def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception, e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
pkg_resources.require("bx-python")
from bx.align import maf
from galaxy.tools.util import maf_utilities

assert sys.version_info[:2] >= (2, 4)


def __main__():
    try:
        species = maf_utilities.parse_species_option(sys.argv[1])
    except Exception, e:
        maf_utilities.tool_fail("Error determining species value: %s" % e)
    try:
        input_filename = sys.argv[2]
    except Exception, e:
        maf_utilities.tool_fail("Error reading MAF filename: %s" % e)
    try:
        file_out = open(sys.argv[3], 'w')
    except Exception, e:
        maf_utilities.tool_fail("Error opening file for output: %s" % e)

    if species:
        print "Restricted to species: %s" % ', '.join(species)
    else:
        print "Not restricted to species."

    if not species:
        try:
            species = maf_utilities.get_species_in_maf(input_filename)
        except Exception, e:
            maf_utilities.tool_fail(
def __main__():
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
from bx.align import maf

from galaxy.tools.util import maf_utilities
from galaxy.util import string_as_bool


def __main__():
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
    try:
        out = maf.Writer( open( sys.argv[2], "w") )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
    try:
        collapse_columns = string_as_bool( sys.argv[3] )
    except Exception, e:
        maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )

    start_count = 0
    end_count = 0
    for start_count, start_block in enumerate( maf_reader ):
        for block in maf_utilities.iter_blocks_split_by_species( start_block ):
            if collapse_columns:
                block.remove_all_gap_columns()
            out.write( block )
            end_count += 1
    out.close()
Exemple #12
0
def __main__():
    index = index_filename = None

    # Parse Command Line
    options, args = doc_optparse.parse(__doc__)

    if options.dbkey:
        dbkey = options.dbkey
    else:
        dbkey = None
    if dbkey in [None, "?"]:
        maf_utilities.tool_fail(
            "You must specify a proper build in order to extract alignments. You can specify your genome build by clicking on the pencil icon associated with your interval file."
        )

    species = maf_utilities.parse_species_option(options.species)

    if options.chromCol:
        chromCol = int(options.chromCol) - 1
    else:
        maf_utilities.tool_fail(
            "Chromosome column not set, click the pencil icon in the history item to set the metadata attributes."
        )

    if options.startCol:
        startCol = int(options.startCol) - 1
    else:
        maf_utilities.tool_fail(
            "Start column not set, click the pencil icon in the history item to set the metadata attributes."
        )

    if options.endCol:
        endCol = int(options.endCol) - 1
    else:
        maf_utilities.tool_fail(
            "End column not set, click the pencil icon in the history item to set the metadata attributes."
        )

    if options.strandCol:
        strandCol = int(options.strandCol) - 1
    else:
        strandCol = -1

    if options.interval_file:
        interval_file = options.interval_file
    else:
        maf_utilities.tool_fail("Input interval file has not been specified.")

    if options.output_file:
        output_file = options.output_file
    else:
        maf_utilities.tool_fail("Output file has not been specified.")

    split_blocks_by_species = remove_all_gap_columns = False
    if options.split_blocks_by_species and options.split_blocks_by_species == 'split_blocks_by_species':
        split_blocks_by_species = True
        if options.remove_all_gap_columns and options.remove_all_gap_columns == 'remove_all_gap_columns':
            remove_all_gap_columns = True
    else:
        remove_all_gap_columns = True
    # Finish parsing command line

    # Open indexed access to MAFs
    if options.mafType:
        if options.indexLocation:
            index = maf_utilities.maf_index_by_uid(options.mafType,
                                                   options.indexLocation)
        else:
            index = maf_utilities.maf_index_by_uid(options.mafType,
                                                   options.mafIndexFile)
        if index is None:
            maf_utilities.tool_fail(
                "The MAF source specified (%s) appears to be invalid." %
                (options.mafType))
    elif options.mafFile:
        index, index_filename = maf_utilities.open_or_build_maf_index(
            options.mafFile, options.mafIndex, species=[dbkey])
        if index is None:
            maf_utilities.tool_fail("Your MAF file appears to be malformed.")
    else:
        maf_utilities.tool_fail(
            "Desired source MAF type has not been specified.")

    # Create MAF writter
    out = bx.align.maf.Writer(open(output_file, "w"))

    # Iterate over input regions
    num_blocks = 0
    num_regions = None
    for num_regions, region in enumerate(
            bx.intervals.io.NiceReaderWrapper(
                open(interval_file),
                chrom_col=chromCol,
                start_col=startCol,
                end_col=endCol,
                strand_col=strandCol,
                fix_strand=True,
                return_header=False,
                return_comments=False)):  # noqa: B007
        src = maf_utilities.src_merge(dbkey, region.chrom)
        for block in index.get_as_iterator(src, region.start, region.end):
            if split_blocks_by_species:
                blocks = [
                    new_block for new_block in
                    maf_utilities.iter_blocks_split_by_species(block)
                    if maf_utilities.component_overlaps_region(
                        new_block.get_component_by_src_start(dbkey), region)
                ]
            else:
                blocks = [block]
            for block in blocks:
                block = maf_utilities.chop_block_by_region(block, src, region)
                if block is not None:
                    if species is not None:
                        block = block.limit_to_species(species)
                    block = maf_utilities.orient_block_by_region(
                        block, src, region)
                    if remove_all_gap_columns:
                        block.remove_all_gap_columns()
                    out.write(block)
                    num_blocks += 1

    # Close output MAF
    out.close()

    # remove index file if created during run
    maf_utilities.remove_temp_index_file(index_filename)

    if num_blocks:
        print("%i MAF blocks extracted for %i regions." % (num_blocks,
                                                           (num_regions + 1)))
    elif num_regions is not None:
        print("No MAF blocks could be extracted for %i regions." %
              (num_regions + 1))
    else:
        print("No valid regions have been provided.")
Exemple #13
0
def __main__():    
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
Exemple #14
0
import pkg_resources; pkg_resources.require( "bx-python" )
from bx.align import maf
from galaxy.tools.util import maf_utilities
from galaxy.util import string_as_bool

assert sys.version_info[:2] >= ( 2, 4 )

def __main__():    
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening MAF: %s" % e )
    try:
        out = maf.Writer( open( sys.argv[2], "w") )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
    try:
        collapse_columns = string_as_bool( sys.argv[3] )
    except Exception, e:
        maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )
    
    start_count = 0
    end_count = 0
    for start_count, start_block in enumerate( maf_reader ):
        for block in maf_utilities.iter_blocks_split_by_species( start_block ):
            if collapse_columns:
                block.remove_all_gap_columns()
            out.write( block )
            end_count += 1
    out.close()
    
Exemple #15
0
def __main__():
    index = index_filename = None

    # Parse Command Line
    options, args = doc_optparse.parse(__doc__)

    if options.dbkey:
        dbkey = options.dbkey
    else:
        dbkey = None
    if dbkey in [None, "?"]:
        maf_utilities.tool_fail("You must specify a proper build in order to extract alignments. You can specify your genome build by clicking on the pencil icon associated with your interval file.")

    species = maf_utilities.parse_species_option(options.species)

    if options.chromCol:
        chromCol = int(options.chromCol) - 1
    else:
        maf_utilities.tool_fail("Chromosome column not set, click the pencil icon in the history item to set the metadata attributes.")

    if options.startCol:
        startCol = int(options.startCol) - 1
    else:
        maf_utilities.tool_fail("Start column not set, click the pencil icon in the history item to set the metadata attributes.")

    if options.endCol:
        endCol = int(options.endCol) - 1
    else:
        maf_utilities.tool_fail("End column not set, click the pencil icon in the history item to set the metadata attributes.")

    if options.strandCol:
        strandCol = int(options.strandCol) - 1
    else:
        strandCol = -1

    if options.interval_file:
        interval_file = options.interval_file
    else:
        maf_utilities.tool_fail("Input interval file has not been specified.")

    if options.output_file:
        output_file = options.output_file
    else:
        maf_utilities.tool_fail("Output file has not been specified.")

    split_blocks_by_species = remove_all_gap_columns = False
    if options.split_blocks_by_species and options.split_blocks_by_species == 'split_blocks_by_species':
        split_blocks_by_species = True
        if options.remove_all_gap_columns and options.remove_all_gap_columns == 'remove_all_gap_columns':
            remove_all_gap_columns = True
    else:
        remove_all_gap_columns = True
    # Finish parsing command line

    # Open indexed access to MAFs
    if options.mafType:
        if options.indexLocation:
            index = maf_utilities.maf_index_by_uid(options.mafType, options.indexLocation)
        else:
            index = maf_utilities.maf_index_by_uid(options.mafType, options.mafIndexFile)
        if index is None:
            maf_utilities.tool_fail("The MAF source specified (%s) appears to be invalid." % (options.mafType))
    elif options.mafFile:
        index, index_filename = maf_utilities.open_or_build_maf_index(options.mafFile, options.mafIndex, species=[dbkey])
        if index is None:
            maf_utilities.tool_fail("Your MAF file appears to be malformed.")
    else:
        maf_utilities.tool_fail("Desired source MAF type has not been specified.")

    # Create MAF writter
    out = bx.align.maf.Writer(open(output_file, "w"))

    # Iterate over input regions
    num_blocks = 0
    num_regions = None
    for num_regions, region in enumerate(bx.intervals.io.NiceReaderWrapper(open(interval_file, 'r'), chrom_col=chromCol, start_col=startCol, end_col=endCol, strand_col=strandCol, fix_strand=True, return_header=False, return_comments=False)):
        src = maf_utilities.src_merge(dbkey, region.chrom)
        for block in index.get_as_iterator(src, region.start, region.end):
            if split_blocks_by_species:
                blocks = [new_block for new_block in maf_utilities.iter_blocks_split_by_species(block) if maf_utilities.component_overlaps_region(new_block.get_component_by_src_start(dbkey), region)]
            else:
                blocks = [block]
            for block in blocks:
                block = maf_utilities.chop_block_by_region(block, src, region)
                if block is not None:
                    if species is not None:
                        block = block.limit_to_species(species)
                    block = maf_utilities.orient_block_by_region(block, src, region)
                    if remove_all_gap_columns:
                        block.remove_all_gap_columns()
                    out.write(block)
                    num_blocks += 1

    # Close output MAF
    out.close()

    # remove index file if created during run
    maf_utilities.remove_temp_index_file(index_filename)

    if num_blocks:
        print("%i MAF blocks extracted for %i regions." % (num_blocks, (num_regions + 1)))
    elif num_regions is not None:
        print("No MAF blocks could be extracted for %i regions." % (num_regions + 1))
    else:
        print("No valid regions have been provided.")
from galaxy import eggs
import pkg_resources; pkg_resources.require( "bx-python" )
from bx.align import maf
from galaxy.tools.util import maf_utilities

assert sys.version_info[:2] >= ( 2, 4 )

def __main__():
    try:
        maf_reader = maf.Reader( open( sys.argv[1] ) )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening input MAF: %s" % e )
    try:
        file_out = open( sys.argv[2], 'w' )
    except Exception, e:
        maf_utilities.tool_fail( "Error opening file for output: %s" % e )
    try:
        species = maf_utilities.parse_species_option( sys.argv[3] )
        if species:
            num_species = len( species )
        else:
            num_species = 0
    except Exception, e:
        maf_utilities.tool_fail( "Error determining species value: %s" % e )
    try:
        partial = sys.argv[4]
    except Exception, e:
        maf_utilities.tool_fail( "Error determining keep partial value: %s" % e )
    
    if species:
        print "Restricted to species: %s" % ', '.join( species )