jdna

jdna.viewer

Classes to view sequences.

The viewer can display sequences and annotations, as in the following:

> "Unnamed" (550bp)


                                                            ----------------GFP----------------
                                                            |<START
                                                            ----      -----------RFP-----------
0         CCCAGGACTAGCGACTTTCCGTAACGCGACCTAACACCGGCCGTTCCTTCGAGCCAGGCAAATGTTACGTCACTTCCTTAGATTT
          GGGTCCTGATCGCTGAAAGGCATTGCGCTGGATTGTGGCCGGCAAGGAAGCTCGGTCCGTTTACAATGCAGTGAAGGAATCTAAA

          ------GFP------
          -----------------------------------------RFP-----------------------------------------
85        TGAACAGCGCCGTACCCCGATATGATATTTAGATATATAGCAGTTACACTTGGGGTTGCTATGGACTTAGATCTGCTGTATGTTT
          ACTTGTCGCGGCATGGGGCTATACTATAAATCTATATATCGTCAATGTGAACCCCAACGATACCTGAATCTAGACGACATACAAA

          -----------------------------------------RFP-----------------------------------------
170       TCTTACCTTCCGCATCAGGGGACAATTCGCCAGTAGAATTCAGTTTGTGCGTGAGAACATAAGATTGAATCCCACGCAGGCACAA
          AGAATGGAAGGCGTAGTCCCCTGTTAAGCGGTCATCTTAAGTCAAACACGCACTCTTGTATTCTAACTTAGGGTGCGTCCGTGTT

          ---------------------RFP----------------------
255       GCAGGGCGGGCAGACTCTATAGGTCCTAAGACCCTGAGACTGCGTCCTCAAGATACAGGTTAACAATCCCCGTATGGAGCCGTTC
          CGTCCCGCCCGTCTGAGATATCCAGGATTCTGGGACTCTGACGCAGGAGTTCTATGTCCAATTGTTAGGGGCATACCTCGGCAAG

340       TTAGCATGACCCGACAGGTGGGCTTGGCTCGCGTAAGTTGAGTGTTGCAGATACCTGCTGCTGCGCGGTCTAGGGGGAATCGCCG
          AATCGTACTGGGCTGTCCACCCGAACCGAGCGCATTCAACTCACAACGTCTATGGACGACGACGCGCCAGATCCCCCTTAGCGGC

425       ATTTTGACGTAGGATCGGTAATGGGCAGTAAACCCGCAACTATTTTCAGCACCAGATGCAAGTTTCCCTAGAAAGCGTCATGGTT
          TAAAACTGCATCCTAGCCATTACCCGTCATTTGGGCGTTGATAAAAGTCGTGGTCTACGTTCAAAGGGATCTTTCGCAGTACCAA

510       TGCAATCTCCTTAGGTCACAGCAAACATAGCAGCCCCTGT
          ACGTTAGAGGAATCCAGTGTCGTTTGTATCGTCGGGGACA

Functions

chunkify(iterable, n)

Break an interable into chunks of size at most ‘n’

indent(string, indent)

Indent lines

prepend_lines(lines, label_iterable, indent)

Prepend lines with a label

to_lines(string, width)

Converts a string to lines of length <= width

Classes

FASTAItem(sequence)

SequenceViewer constructor

FASTAViewer(sequences)

SequenceRow(lines, labels, indent, start, end)

A row in a SequenceViewer instance.

SequenceViewer(sequences[, sequence_labels, …])

A class that views longs sets of sequences.

StringColumn([strings, color, background, fill])

Class for managing string columns

ViewerAnnotationFlag

Flags for annotation directions

class jdna.viewer.FASTAItem(sequence)[source]

Bases: jdna.viewer.SequenceViewer

SequenceViewer constructor

Parameters
  • sequences (list) – list of sequences to view

  • sequence_labels (list) – optional labels to apply to sequence. Include the ‘{index}’ to enumerate the base pairs.

  • foreground_colors (list) – optional list base pair foreground colors (hex or common name) to apply to each sequence. If a string is provided, color will be applied to all sequences. If provided with “RANDOM”, a random color will be assigned to each sequence.

  • background_colors (list) – optional list base pair background colors (hex or common name) to apply to each sequence. Usage is analogous to foreground_colors parameter.

  • indent (int) – spacing before start of string and start of base pairs

  • width (string) – width of the view window for the sequences (e.g. width=100 would mean rows of at most len 100 characters

  • spacer (string) – string to apply inbetween rows (default is newline)

  • name (basestring) – optional name for this viewer, to be displayed in the header

  • window (tuple) – tuple of the start and end points of the viewing window

  • description (basestring) – optional description for this viewer

  • metadata (dict) – optional metadata to display in the header

_annotate_rows(rows)

Annotate the rows using the viewer’s annotations

annotate(start, end, label=None, fill=None, color=None, background=None, top=True)

Annotates this viewer object starting from ‘start’ to ‘end’ inclusively.

Parameters
  • start (int) – inclusive start

  • end (int) – inclusive end

  • label (basestring | StringColumn) – optional label to apply to the annotation

  • fill (string) – the fill character to use to (e.g. ‘<’, ‘>’, ‘^’) to fill in whitespace

  • color (string) – the foreground color to apply to the annotation (hex or common name)

  • background (string) – the foreground color to apply to the annotation (hex or common name)

Returns

None

Return type

None

property header

Return the formatted header and metadata

set_window(start, end)

Sets the inclusive viewing window

class jdna.viewer.SequenceRow(lines, labels, indent, start, end, line_colors=None, line_backgrounds=None)[source]

Bases: object

A row in a SequenceViewer instance. Can be comprised of multiple sequences (i.e. lines) and can be annotated with ‘features’.

SequenceRow constructor

Parameters
  • lines (list) – list of lines to display. Lengths of all lines must all be equivalent.

  • labels (list) – list of labels to apply to each line

  • indent (string) – indent to apply to the lines

  • start (int) – start bp of this row

  • end (int) – end bp of this row

absolute_annotate(start, end, fill, label, color=None, background=None, top=True)[source]

Applyt annotation to this row using absolute start and ends for THIS row.

Parameters
  • start (int) – inclusive start

  • end (int) – inclusive end

  • fill (basestring) – what to fill whitespace with

  • label (basestring) – annotation label

Returns

None

Return type

None

annotate(start, end, fill, label='', color=None, background=None, top=True, wrap=False)[source]

Annotate the sequence row. If ‘start’ or ‘end’ is beyond, the expected start or end for this row, the annotation will automatically be truncated.

Parameters
  • start (int) – inclusive start

  • end (int) – inclusive end

  • fill – what to fill whitespace with

  • label (basestring) – optional label to apply to the annotation

Returns

Return type

in_bounds(x)[source]

Checks if the index ‘x’ is in between row start and end (inclusive)

Parameters

x (int) – index

Returns

if in bounds

Return type

bool

static make_annotation(label, span, fill='*', color=None, background=None)[source]

Make an annotation with ‘label’ spanning inclusive base pairs indices ‘span’

Parameters
  • label (basestring) – annotation label

  • span (tuple) – the start and end (inclusive) of the annotation

  • fill (basestring) – what to fill whitespace with

Returns

Return type

class jdna.viewer.SequenceViewer(sequences, sequence_labels=None, apply_indices=[0], foreground_colors=None, background_colors=None, indent=10, width=85, spacer='n', header_spacer='n', name='Unnamed', window=(0, None), description='', metadata=None)[source]

Bases: object

A class that views longs sets of sequences.

SequenceViewer constructor

Parameters
  • sequences (list) – list of sequences to view

  • sequence_labels (list) – optional labels to apply to sequence. Include the ‘{index}’ to enumerate the base pairs.

  • foreground_colors (list) – optional list base pair foreground colors (hex or common name) to apply to each sequence. If a string is provided, color will be applied to all sequences. If provided with “RANDOM”, a random color will be assigned to each sequence.

  • background_colors (list) – optional list base pair background colors (hex or common name) to apply to each sequence. Usage is analogous to foreground_colors parameter.

  • indent (int) – spacing before start of string and start of base pairs

  • width (string) – width of the view window for the sequences (e.g. width=100 would mean rows of at most len 100 characters

  • spacer (string) – string to apply inbetween rows (default is newline)

  • name (basestring) – optional name for this viewer, to be displayed in the header

  • window (tuple) – tuple of the start and end points of the viewing window

  • description (basestring) – optional description for this viewer

  • metadata (dict) – optional metadata to display in the header

_annotate_rows(rows)[source]

Annotate the rows using the viewer’s annotations

annotate(start, end, label=None, fill=None, color=None, background=None, top=True)[source]

Annotates this viewer object starting from ‘start’ to ‘end’ inclusively.

Parameters
  • start (int) – inclusive start

  • end (int) – inclusive end

  • label (basestring | StringColumn) – optional label to apply to the annotation

  • fill (string) – the fill character to use to (e.g. ‘<’, ‘>’, ‘^’) to fill in whitespace

  • color (string) – the foreground color to apply to the annotation (hex or common name)

  • background (string) – the foreground color to apply to the annotation (hex or common name)

Returns

None

Return type

None

property header

Return the formatted header and metadata

set_window(start, end)[source]

Sets the inclusive viewing window

class jdna.viewer.StringColumn(strings=None, color=None, background=None, fill=None)[source]

Bases: object

Class for managing string columns

StringColumn constructor.

Parameters

strings (list) – list of strings

classmethod condense(rows)[source]

Condense a list of StringColumn into the minimum number of StringColumns comprising of columns stripped of white space. Briefly, this is similar to the following procedure:

input = [
    'label         ',
    '       label2 ',
    '      label3  '
]

# >> CONDENSE

output = [
    'label  label2 ',
    '      label3  '
]
Parameters

rows

Returns

Return type

classmethod string_length(string)[source]

String length, ignoring terminal formatting

class jdna.viewer.ViewerAnnotationFlag[source]

Bases: object

Flags for annotation directions

jdna.viewer.chunkify(iterable, n)[source]

Break an interable into chunks of size at most ‘n’

jdna.viewer.indent(string, indent)[source]

Indent lines

jdna.viewer.prepend_lines(lines, label_iterable, indent, fill=' ', align='<')[source]

Prepend lines with a label

Parameters
  • lines (list) – lines to prepend

  • indent (int) – number of spaces between start of label and start of line

  • fill (what to fill the spaces) – default ‘ ‘

  • align (string) – either left “<”, center “^” or right “>”

Returns

new prepended lines

Return type

list

jdna.viewer.to_lines(string, width)[source]

Converts a string to lines of length <= width