def new_nexus_without_sites(nexus_obj, sites_to_remove): """ Returns a new NexusReader instance with the sites in `sites_to_remove` removed. :param nexus_obj: A `NexusReader` instance :type nexus_obj: NexusReader :param sites_to_remove: A list of site numbers :type sites_to_remove: List :return: A NexusWriter instance :raises AssertionError: if nexus_obj is not a nexus :raises NexusFormatException: if nexus_obj does not have a `data` block """ # make new nexus nexout = NexusWriter() nexout.add_comment( "Removed %d sites: %s" % (len(sites_to_remove), ",".join(["%s" % s for s in sites_to_remove]))) new_sitepos = 0 for sitepos in range(nexus_obj.data.nchar): if sitepos in sites_to_remove: continue # skip! for taxon, data in nexus_obj.data: nexout.add(taxon, new_sitepos, data[sitepos]) new_sitepos += 1 return nexout
def new_nexus_without_sites(nexus_obj, sites_to_remove): """ Returns a new NexusReader instance with the sites in `sites_to_remove` removed. :param nexus_obj: A `NexusReader` instance :type nexus_obj: NexusReader :param sites_to_remove: A list of site numbers :type sites_to_remove: List :return: A NexusWriter instance :raises AssertionError: if nexus_obj is not a nexus :raises NexusFormatException: if nexus_obj does not have a `data` block """ check_for_valid_NexusReader(nexus_obj, required_blocks=['data']) # make new nexus nexout = NexusWriter() nexout.add_comment( "Removed %d sites: %s" % (len(sites_to_remove), ",".join(["%s" % s for s in sites_to_remove])) ) new_sitepos = 0 for sitepos in range(nexus_obj.data.nchar): if sitepos in sites_to_remove: continue # skip! for taxon, data in nexus_obj.data: nexout.add(taxon, new_sitepos, data[sitepos]) new_sitepos += 1 return nexout
def shufflenexus(nexus_obj, resample=False): """ Shuffles the characters between each taxon to create a new nexus :param nexus_obj: A `NexusReader` instance :type nexus_obj: NexusReader :param resample: The number of characters to resample. If set to False, then the number of characters will equal the number of characters in the original data file. :type resample: Integer :return: A shuffled NexusReader instance :raises AssertionError: if nexus_obj is not a nexus :raises ValueError: if resample is not False or a positive Integer :raises NexusFormatException: if nexus_obj does not have a `data` block """ check_for_valid_NexusReader(nexus_obj, required_blocks=['data']) if resample is False: resample = nexus_obj.data.nchar try: resample = int(resample) except ValueError: raise ValueError('resample must be a positive integer or False!') if resample < 1: raise ValueError('resample must be a positive integer or False!') newnexus = NexusWriter() newnexus.add_comment( "Randomised Nexus generated from %s" % nexus_obj.filename ) for i in range(resample): # pick existing character character = randrange(0, nexus_obj.data.nchar) chars = nexus_obj.data.characters[character] site_values = [chars[taxon] for taxon in nexus_obj.data.taxa] shuffle(site_values) for taxon in nexus_obj.data.taxa: newnexus.add(taxon, i, site_values.pop(0)) return newnexus
def shufflenexus(nexus_obj, resample=False): """ Shuffles the characters between each taxon to create a new nexus :param nexus_obj: A `NexusReader` instance :type nexus_obj: NexusReader :param resample: The number of characters to resample. If set to False, then the number of characters will equal the number of characters in the original data file. :type resample: Integer :return: A shuffled NexusReader instance :raises AssertionError: if nexus_obj is not a nexus :raises ValueError: if resample is not False or a positive Integer :raises NexusFormatException: if nexus_obj does not have a `data` block """ check_for_valid_NexusReader(nexus_obj, required_blocks=['data']) if resample is False: resample = nexus_obj.data.nchar try: resample = int(resample) except ValueError: raise ValueError('resample must be a positive integer or False!') if resample < 1: raise ValueError('resample must be a positive integer or False!') newnexus = NexusWriter() newnexus.add_comment("Randomised Nexus generated from %s" % nexus_obj.filename) for i in range(resample): # pick existing character character = randrange(0, nexus_obj.data.nchar) chars = nexus_obj.data.characters[character] site_values = [chars[taxon] for taxon in nexus_obj.data.taxa] shuffle(site_values) for taxon in nexus_obj.data.taxa: newnexus.add(taxon, i, site_values.pop(0)) return newnexus
def combine_nexuses(nexuslist): """ Combines a list of NexusReader instances into a single nexus :param nexuslist: A list of NexusReader instances :type nexuslist: List :return: A NexusWriter instance :raises TypeError: if nexuslist is not a list of NexusReader instances :raises IOError: if unable to read an file in nexuslist :raises NexusFormatException: if a nexus file does not have a `data` block """ out = NexusWriter() charpos = 0 for nex_id, nex in enumerate(nexuslist, 1): check_for_valid_NexusReader(nex, required_blocks=['data']) if hasattr(nex, 'short_filename'): nexus_label = os.path.splitext(nex.short_filename)[0] elif hasattr(nex, 'label'): nexus_label = nex.label else: nexus_label = str(nex_id) out.add_comment( "%d - %d: %s" % (charpos, charpos + nex.data.nchar - 1, nexus_label) ) for site_idx, site in enumerate(sorted(nex.data.characters), 0): data = nex.data.characters.get(site) charpos += 1 # work out character label charlabel = nex.data.charlabels.get(site_idx, site_idx + 1) label = '%s.%s' % (nexus_label, charlabel) for taxon, value in data.items(): out.add(taxon, label, value) return out
def combine_nexuses(nexuslist): """ Combines a list of NexusReader instances into a single nexus :param nexuslist: A list of NexusReader instances :type nexuslist: List :return: A NexusWriter instance :raises TypeError: if nexuslist is not a list of NexusReader instances :raises IOError: if unable to read an file in nexuslist :raises NexusFormatException: if a nexus file does not have a `data` block """ out = NexusWriter() charpos = 0 for nex_id, nex in enumerate(nexuslist, 1): check_for_valid_NexusReader(nex, required_blocks=['data']) if hasattr(nex, 'short_filename'): nexus_label = os.path.splitext(nex.short_filename)[0] else: nexus_label = str(nex_id) out.add_comment("%d - %d: %s" % (charpos, charpos + nex.data.nchar - 1, nexus_label)) for site_idx, site in enumerate(sorted(nex.data.characters), 0): data = nex.data.characters.get(site) charpos += 1 # work out character label charlabel = nex.data.charlabels.get(site_idx, site_idx + 1) label = '%s.%s' % (nexus_label, charlabel) for taxon, value in data.items(): out.add(taxon, label, value) return out