Example #1
0
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
Example #2
0
def combine_nexuses(*nexuslist):
    """
    Combines a list of NexusReader instances into a single nexus

    :param nexuslist: A list of NexusReader instances

    :return: A NexusWriter instance
    """
    if len(nexuslist) == 1 and isinstance(nexuslist[0], (list, tuple)):
        nexuslist = nexuslist[0]
    nexuslist = [get_nexus_reader(nex) for nex in nexuslist]
    out = NexusWriter()
    # check they're all nexus instances and get all block types
    blocks = set()
    for nex in nexuslist:
        blocks.update(list(nex.blocks))

    for block in blocks:
        if block == 'data':
            out = combine_datablocks(out, nexuslist)
        elif block == 'trees':
            out = combine_treeblocks(out, nexuslist)
        else:  # pragma: no cover
            raise ValueError("Don't know how to combine %s blocks" % block)
    return out
def snpMatrixGenerator(sourceFile,
                       destFile,
                       recordAll=False,
                       recordRandomSample=True):
    if recordAll == recordRandomSample:
        print "Invalid Options"
        exit()

    destNexus = NexusWriter()

    block = ""
    snpCol = 0
    for line in sourceFile:
        if all(x in line.lower() for x in {"begin", "data"}):
            sourceNexus = NexusReader()
            sourceNexus.read_string(block)
            if "data" in sourceNexus.blocks:
                snpCol = _findDifferences(sourceNexus, destNexus, snpCol,
                                          recordAll, recordRandomSample)
            block = line
        else:
            block += line

    sourceNexus = NexusReader()
    sourceNexus.read_string(block)
    if "data" in sourceNexus.blocks:
        snpCol = _findDifferences(sourceNexus, destNexus, snpCol, recordAll,
                                  recordRandomSample)

    destFile.write(destNexus.make_nexus() + '\n')

    destFile.close()
    sourceFile.close()
Example #4
0
 def test_regression_format_string_has_quoted_symbols(self):
     """Regression: Symbols in the format string should be quoted"""
     nex = NexusWriter()
     for char, b in data.items():
         for taxon, value in b.items():
             nex.add(taxon, char, value)
     out = nex.make_nexus()
     assert 'SYMBOLS="123456"' in out
Example #5
0
 def test_regression_format_string_has_datatype_first(self):
     """Regression: Format string should contain 'datatype' as the first element"""
     # SplitsTree complains otherwise.
     nex = NexusWriter()
     for char, b in data.items():
         for taxon, value in b.items():
             nex.add(taxon, char, value)
     out = nex.make_nexus()
     assert "FORMAT DATATYPE=STANDARD" in out
Example #6
0
def binarise(nexus_obj, one_nexus_per_block=False):
    """
    Returns a binary variant of the given `nexus_obj`.
    If `one_nexus_per_block` then we return a list of NexusWriter instances.

    :param nexus_obj: A `NexusReader` instance
    :type nexus_obj: NexusReader 

    :param one_nexus_per_block: Whether to return a single NexusWriter, or a 
                                list of NexusWriter's (one per character)
    :type one_nexus_per_block: Boolean

    :return: A NexusWriter instance or a list of NexusWriter instances.
    :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'])
    nexuslist = []
    n = NexusWriter()

    for i in sorted(nexus_obj.data.charlabels):
        label = nexus_obj.data.charlabels[i]  # character label
        char = nexus_obj.data.characters[
            label]  # character dict (taxon->state)
        recoding = _recode_to_binary(char)  # recode

        new_char_length = len(recoding[recoding.keys()[0]])

        # loop over recoded data
        for j in range(new_char_length):
            for taxon, state in recoding.items():
                # make new label
                new_label = "%s_%d" % (str(label), j)
                # add to nexus
                n.add(taxon, new_label, state[j])

        if one_nexus_per_block:
            nexuslist.append(n)
            n = NexusWriter()

    if one_nexus_per_block:
        return nexuslist
    else:
        return n
Example #7
0
def nexus(args):  # pragma: no cover
    usage = """
    Convert a lexibank dataset to nexus.

        lexibank nexus <DATASET> --output=...
    """
    get_dataset(args)
    parser = argparse.ArgumentParser(prog='nexus', usage=usage)
    parser.add_argument('--output', help='Nexus output file', default=None)
    xargs = parser.parse_args(args.args[1:])

    writer = NexusWriter()

    if not xargs.output:
        print(writer.write())
    else:
        writer.write_to_file(filename=xargs.output)
Example #8
0
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
Example #9
0
def create_nexus(bt_matrix, sample_list):
    '''
    create NexusWriter object from BedTool matrix
    BedTool object apparently can contain empty lines, a simple length-check skips these

    requires pybedtools, nexus
    '''

    n = NexusWriter()
    if isinstance(bt_matrix, BedTool):
        matrix_lines = str(bt_matrix).split(
            "\n")  # create lines from BedTool object
    else:
        matrix_lines = bt_matrix
    i = 0
    current_chromosome = ""
    for line in matrix_lines:
        line = line.split("\t")

        if len(line) < 4:  #check for empty line, why 4? it works
            print "skipping: line empty:  %s" % ",".join(line)
            continue

        chrom, start, end, samples_present = line[0], line[1], line[2], line[
            3].split(",")  # create locus and presnt_ssample objects
        locus = "%s:%i - %i" % (chrom, int(start), int(end))  # define locus
        i += 1

        if chrom != current_chromosome:
            print chrom
            current_chromosome = chrom
        else:
            pass

        for taxon in sample_list:  # iterate over taxons
            presence = 1 if taxon in samples_present else 0
            n.add(taxon, locus, presence)

    return n
def make_nexus(model_dict):
    sequence = []
    n = NexusWriter()
    dictline = {}
    for dictline in model_dict:
        lang = dictline.get('Language')
        isocode = dictline.get('ISOCODE')
        kvec = ""
        for k in sorted(dictline):
            #print(k)
            if (k == 'Language' or k == 'ISOCODE'):
                continue
            else:
                k = repr(dictline.get(k))
                krem = k.replace("'", "")
                kmod = krem[2:36]
                kvec = kvec + kmod.zfill(35)


# label is number of elements in vector, number of taxa and a random 1
        label = "%s_%s_%d" % ("756", "51", 1)
        n.add(lang, label, kvec)
    return n
Example #11
0
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
Example #12
0
  def read_file(self):
  
    with open( self.input_file, 'r') as f:
      self.first_line = f.readline()
   
      # TODO: Needs work xread / tnt file format is loose

      # xread has some other potential clues
      xread_filename = f.readline().strip().replace("'", "")
      xread_matrix_dimensions = f.readline()
      lines = f.readlines()

      f.close

    if "#NEXUS" == self.first_line.strip():
      print "text file is nexus"
  
      # move to nexus folder
      filename, file_extension = os.path.splitext(self.input_file)
      os.rename(self.input_file, filename + ".nex")

      # send to correct matrix handler
      # TODO: A little dirty, probably should 
      # change mediator's handler param so this flows cleaner
      matrixHandler = NexusHandler( filename + ".nex" )
      return matrixHandler.read_file()

    elif "xread" == self.first_line.strip():
    
      print "xread format found"

      dimensions = str(xread_matrix_dimensions).split(' ')
      self.ncols = int(dimensions[0])
      self.nrows = int(dimensions[1])

      custom_block = "\n\nBEGIN VERIFIED_TAXA;\n"
      custom_block += "Dimensions ntax=" + str(self.nrows) + " nchar=4;\n"

      matrix = ""
      matrix_arr = []
      line_buffer = ""
      row_taxa = []
      for l in lines:
        if ";proc/;" != l.strip():

          if line_buffer:
            line_row = line_buffer + " " + l.strip()
          else:
            line_row = l.strip()
        
          if len(line_row) >= self.ncols:
 
            # reconstitute broken rows, then remove space/tabbing
            line_parts = line_row.split(' ')
            line_parts = list(filter(None, line_parts))

            taxon_name = line_parts[0]
            taxon_chars = line_parts[1]
            #taxon_chars = line_parts[1].replace("[", "(")
            #taxon_chars = taxon_chars.replace("]", ")")
          
            #  verify taxa
            verified_taxa = verifyTaxa(taxon_name)
            verified_name = None

            if verified_taxa:
              for taxa in verified_taxa:

                # We split here to exclude the odd citation on the taxon name ( maybe regex what looks like name & name, year would be better )
                verified_name = taxa['name_string'].lower().split(' ')
                row_taxa.append( verified_name[0] )

                custom_block += taxon_name + "    " + taxa['name_string'] + "    " + taxa['match_value'] + "    " + taxa['datasource'] + "\n"

              matrix += "    " + verified_name[0] + "    " + taxon_chars.strip() + "\n"
              matrix_arr.append(taxon_chars.strip())
            else:

              row_taxa.append( taxon_name )
              custom_block += taxon_name + "\n"
              matrix += "    " + taxon_name + "    " + taxon_chars.strip() + "\n"
              matrix_arr.append(taxon_chars.strip())

            line_buffer = ""
          else:
            line_buffer += l.strip()

      custom_block += ";\n"
      custom_block += "END;\n"

      self.custom_block = custom_block      

      print "matrix array"
      marr = []
      for row in matrix_arr:
        items = list(row)
        marr.append(items)

      m = numpy.matrix(marr)
       
      nw = NexusWriter()
      nw.add_comment("Morphobank generated Nexus from xread .txt file ")

      for rx in range(self.nrows):
        taxon_name = row_taxa[rx] 
        cell_value = m.item(rx)

        for cindex, cv in enumerate(cell_value):
          char_no = cindex + 1
          nw.add(taxon_name, char_no, cv)


      # keep the upload path and filename, but change extension
      file_parts = self.input_file.split('.')
      output_file = file_parts[0] + '.nex'

      nw.write_to_file(filename=output_file, interleave=False, charblock=True)

      # move to nexus folder
      #os.rename(xread_filename + ".nex", "./nexus/" + xread_filename + ".nex")

      # wait for file to move before open and append
      #while not os.path.exists('./nexus/' + xread_filename + '.nex'):
      #  time.sleep(1)
 
      #if os.path.isfile('./nexus/' + xread_filename + '.nex'):

      # Custom Block Section
      nexus_file = codecs.open(output_file, 'a', 'utf-8')
      nexus_file.write(custom_block)
      nexus_file.close()      

      return output_file

    else:
      print "do not know how to process this .txt file"
Example #13
0
def create_nexus(bt_matrix, sample_list, absence_code=0, has_missing=False):
    '''
    create NexusWriter object from BedTool matrix
    BedTool object apparently can contain empty lines, a simple length-check skips these

    :param has_missing indicate whether input BED file should be parsed for uncertainly called loci, that will be coded as "?"
    requires pybedtools, nexus
    '''
    from nexus import NexusWriter
    n = NexusWriter()
    # if isinstance(bt_matrix, BedTool):
    #     matrix_lines = str(bt_matrix).split("\n")  # create lines from BedTool object
    # else:
    #     matrix_lines = str(bt_matrix).split("\n")
    matrix_lines = str(bt_matrix).split("\n")
    current_chrom = ""
    if has_missing:
        print u"[ {} ] adding missing characters".format(modulename)

    for line in matrix_lines:
        line = line.split("\t")

        if len(line) < 4:  # check for incomplete line
            print u"[ {} ]".format(modulename),
            print u"skipping incomplete or empty line:  %s" % ",".join(line)
            continue

        if not has_missing:

            chrom, start, end, samples_present = line[0], line[1], line[
                2], line[3].split(
                    ",")  # create locus and present_sample objects
            locus = "%s_%i_%i" % (chrom, int(start), int(end))  # define locus

            if current_chrom != chrom:
                print u"[ {} ] Operating on".format(modulename),
                print chrom
                current_chrom = chrom
            # add locus to nexus object
            for taxon in sample_list:  # iterate over taxons
                char = 1 if taxon in samples_present else absence_code
                n.add(taxon, locus, char)

        elif has_missing:
            chrom, start, end, samples_present, type, samples_missing = line[
                0], line[1], line[2], line[3].split(
                    ","), line[4], line[5].split(
                        ",")  # create locus and present_sample objects
            locus = "%s_%i_%i" % (chrom, int(start), int(end))  # define locus

            if current_chrom != chrom:
                print u"[ {} ] Operating on".format(modulename),
                print chrom
                current_chrom = chrom

            # add locus to nexus object
            for taxon in sample_list:  # iterate over taxons
                if taxon in samples_present:
                    char = 1
                elif taxon in samples_missing:
                    char = "?"
                else:
                    char = absence_code
                n.add(taxon, locus, char)

    return n
Example #14
0
 def setUp(self):
     self.nex = NexusWriter()
     for char, b in data.items():
         for taxon, value in b.items():
             self.nex.add(taxon, char, value)
Example #15
0
 def setUp(self):
     self.nex = NexusWriter()
Example #16
0
  def read_file(self):

    book = xlrd.open_workbook(self.input_file)
    sh = book.sheet_by_index(0)

    nw = NexusWriter()

    self.nrows = sh.nrows
    self.ncols = sh.ncols

    nw.add_comment("Morphobank generated Nexus File")
  
    custom_block = "\n\nBEGIN VERIFIED_TAXA;\n"
    custom_block += "Dimensions ntax=" + str(self.nrows) + " nchar=4;\n"

    # Species List Taxa
    for rx in range(self.nrows):
      if rx:
    
        taxon_name = str(sh.cell_value(rowx=rx, colx=0)).strip()
   
        verified_taxa = verifyTaxa(taxon_name)
        verified_name = None
        
        if verified_taxa:
          for taxa in verified_taxa:
            verified_name = taxa['name_string'].lower()
            custom_block += taxon_name + "    " + taxa['name_string'] + "    " + taxa['match_value'] + "    " + taxa['datasource'] + "\n"
        else:
          custom_block += taxon_name + "\n"
    
        for cx in range(self.ncols):
          if cx:

            if is_number( sh.cell_value(rowx=rx, colx=cx)):
              cell_value = int(sh.cell_value(rowx=rx, colx=cx))
            else:
              cell_value = sh.cell_value(rowx=rx, colx=cx)
              cell_value = cell_value.replace("{", "(")
              cell_value = cell_value.replace("}", ")")

            if verified_name:
              nw.add(verified_name, cx, cell_value)
            else:
              nw.add(taxon_name, cx, cell_value)
  
    custom_block += ";\n"
    custom_block += "END;\n"

    self.custom_block = custom_block

    # keep the upload path and filename, but change extension
    file_parts = self.input_file.split('.')
    output_file = file_parts[0] + '.nex'
    nw.write_to_file(filename=file_parts[0] + '.nex', interleave=False, charblock=True)

    ## quick append Custom Block 
    nexus_file = open(output_file, 'a')
    nexus_file.write(custom_block)
    nexus_file.close()

    return output_file