SeqViz

repository·develop·Indexed 43 days ago

https://github.com/lattice-automation/seqviz

A specialized sequence viewer for DNA, RNA, and protein sequences supporting linear and circular visualizations. Version 3.10.24 provides a React component and a plain JavaScript implementation to render annotations, primers, enzyme cut sites, and translations. It includes features for sequence searching with Hamming distance, interactive rotation for circular viewers, and custom layout positioning via refs.

Tokens
14.2K
Snippets
40
Records
54
Agent score
55%

What's inside SeqViz

  1. Custom Viewer Positioning with refs

    develop

    You can override the default layout by using the refs prop and the children render pattern. This allows you to render the Linear and Circular components separately in your own layout (e.g., stacking them vertically).

    import { useRef } from "react";
    import { Circular, Linear, SeqViz } from "seqviz";
    
    export default () => {
      const circular = useRef();
      const linearRef = useRef();
    
      return (
        <SeqViz name="J23100" seq="TTGACGGCTAGCTCAGTCCTAGGTACAGTGCTAGC" refs={{ circular, linear }}>
          {({ circularProps, linearProps, ...props }) => (
            <div style={{ display: "flex", flexDirection: "column", width: "100%" }}>
              <div ref={linear} style={{ height: "25%", width: "100%" }}>
                <Linear {...linearProps} {...props} />
              </div>
              <div ref={circular} style={{ height: "75%", width: "100%" }}>
                <Circular {...circularProps} {...props} />
              </div>
            </div>
          )}
        </SeqViz>
      );
    };
  2. Instantiate SeqViz without React

    develop

    For non-React applications, use the seqviz.Viewer constructor. It requires an element ID or HTMLElement and a configuration object. Call .render() to mount the viewer.

    <script>
      window.seqviz
        .Viewer("root", {
          name: "L09136",
          seq: "tcgcgcgtttcggtgatgacggtgaaaacctctgacacatgca",
          style: { height: "100vh", width: "100vw" },
        })
        .render();
    </script>
  3. Instantiate SeqViz in React

    develop

    To use SeqViz in a React application, import the SeqViz component. You must provide at least the seq prop (the DNA, RNA, or protein sequence).

    import { SeqViz } from "seqviz";
    
    export default () => (
      <SeqViz
        name="J23100"
        seq="TTGACGGCTAGCTCAGTCCTAGGTACAGTGCTAGC"
        annotations={[{ name: "promoter", start: 0, end: 34, direction: 1, color: "blue" }]}
      />
    );
  4. Run the seqviz demo for development

    develop

    To run the SeqViz demonstration locally as part of the development workflow, you must install dependencies for both the demo package and the root workspace. Follow these steps from the root of the repository:

    1. Clone the repository.
    2. Navigate to the demo directory and install its dependencies.
    3. Return to the root and install workspace dependencies.
    4. Start the development server.
    git clone https://github.com/Lattice-Automation/seqviz
    cd seqviz/demo
    npm install
    cd ..
    npm install
    npm start
  5. How the Circular viewer handles rotation and scrolling

    develop

    The Circular viewer supports interactive rotation. When rotateOnScroll is enabled, wheel/scroll events on the SVG element trigger a rotation of the plasmid.

    Rotation Logic:

    • The rotation is calculated based on the deltaY of the scroll event.
    • A 'large scroll' (deltaY of 1000) is mapped to rotate through approximately 20% of the plasmid's total sequence length.
    • The rotation updates a centralIndex in the CentralIndexContext, which shifts the visual orientation of all elements (annotations, indices, etc.) around the center point.
    • The getRotation method calculates the specific SVG rotate(degrees, cx, cy) transformation required to align an element at a specific sequence index with the current viewer rotation.
  6. Customize the viewer layout with children

    develop

    If you need to control the positioning of the Linear and Circular viewers, you can use the children prop. This prop accepts a function that receives CustomChildrenProps and returns a React.ReactNode. You can also provide refs via the refs prop to interact with these custom children.

    <SeqViz
      seq="ATGC..."
      children={(props) => (
        <div className="custom-layout">
          <div className="left-side">{props.circular}</div>
          <div className="right-side">{props.linear}</div>
        </div>
      )}
    />
  7. Implement sequence selection with SelectionHandler

    develop

    The SelectionHandler component is a high-order component (HOC) pattern used to manage sequence selection (clicks, drags, shift-clicks) across both Linear and Circular viewers. To use it, wrap your viewer components with SelectionHandler and use the provided render props to register elements and handle mouse events.

    Key Responsibilities:

    • Registration: Use the inputRef callback to register child elements (like SeqBlocks or annotations) so the handler knows their sequence ranges.
    • Event Handling: Pass the handleMouseEvent function to your elements' onMouseDown, onMouseMove, and onMouseUp handlers to enable drag-to-select functionality.
    • Lifecycle: Use onUnmount to clean up registered elements when they are removed from the DOM.

    Render Prop Signature: children: (inputRef: InputRefFunc, handleMouseEvent: (e: SeqVizMouseEvent) => void, onUnmount: (ref: string) => void) => React.ReactNode

    <SelectionHandler
      bpsPerBlock={100}
      center={{ x: 200, y: 200 }}
      centralIndex={0}
      seq="ATGC..."
      setCentralIndex={(viewer, index) => { /* ... */ }}
      setSelection={(selection) => { /* ... */ }}
      yDiff={0}
    >
      {(inputRef, handleMouseEvent, onUnmount) => (
        <div onMouseDown={handleMouseEvent} onMouseMove={handleMouseEvent} onMouseUp={handleMouseEvent}>
          {/* Your SeqBlocks or Annotations here */}
          <div 
            id="block-1" 
            onMouseDown={handleMouseEvent}
            ref={(el) => inputRef("block-1", { start: 0, end: 100, type: "SEQ" })}
          />
        </div>
      )}
    </SelectionHandler>
  8. Handle selection events with onSelection

    develop

    The onSelection callback is triggered by drag/drop selections or clicks on elements like annotations, translations, or enzymes. It returns a Selection object containing the range and metadata about the selected element.

    Example Selection Object:

    {
      "end": 457,
      "length": 324,
      "name": "lacZ fragment",
      "type": "ANNOTATION"
    }
  9. Manage SeqViz state in non-React apps

    develop

    When using the plain JavaScript seqviz.Viewer, you can update the viewer's configuration and trigger re-renders using the setState method on the returned instance.

    const element = document.getElementById("root");
    const viewer = seqviz.Viewer(element, props);
    
    // Render the viewer to the DOM
    viewer.render();
    
    // Update configuration and re-render
    viewer.setState(newProps);
    
    // Render as an HTML string (e.g., for SSR)
    viewer.renderToString();
  10. Perform sequence searches with search and onSearch

    develop

    Use the search prop to find specific sequences within the viewer. You can specify a query and the maximum allowable mismatch (Hamming distance). Supports wildcard symbols (e.g., AANAA).

    Search Configuration:

    search = { query: "aatggtctc", mismatch: 1 };

    onSearch Callback: Triggered on initial render and sequence changes. Returns an array of match objects:

    [
      { "start": 728, "end": 733, "direction": 1, "index": 0 },
      { "start": 1788, "end": 1793, "direction": -1, "index": 1 }
    ]
  11. Reference: SeqViz Props and Options

    develop

    The following props are available for both the React component (seqviz.SeqViz) and the plain JS implementation (seqviz.Viewer()).

    Required

    • seq (string): The DNA, RNA, or amino acid sequence to render.

    Deprecated

    • file: FASTA, GenBank, SnapGene, JBEI, or SBOL file. Use seqparse instead.
    • accession: NCBI accession-ID. Use seqparse instead.

    Optional

    • viewer ('linear' | 'circular' | 'both' | 'both_flip'): Orientation of viewers. Default is 'both' (circular left, linear right).
    • name (string): Name of the sequence, shown in the circular viewer.
    • annotations (Array<Annotation>): Array of objects with start (inclusive), end (exclusive), name, direction (1 for FWD, -1 for REV), and color.
    • primers (Array<Primer>): Array of objects with start, end, name, direction (1 for FWD, -1 for REV), and color.
    • translations (Array<Translation>): Sequence ranges to translate. Requires start, end, and direction (1 or -1).
    • enzymes (Array<string | EnzymeObject>): Restriction enzymes. Can be a name (e.g., 'EcoRI') or an object with name, rseq, fcut, rcut, color, and range.
    • highlights (Array<Highlight>): Ranges to highlight with start, end, and color.
    • zoom ({ linear: number }): Zoom level (0-100) for the linear viewer.
    • colors (Array<string>): Array of colors for annotations/translations/highlights.
    • bpColors (Object): Map base pairs to colors (e.g., { A: '#FF0000', 12: '#00FFFF' }).
    • style (Object): CSS styles for the outer container.
    • selection (Object): Manually set selection state with start, end, and clockwise.
    • onSelection (Function): Callback for selection events. Receives a Selection object.
    • search (Object): Search parameters: { query: string, mismatch: number }.
    • onSearch (Function): Callback for search events.
    • showComplement (boolean): Whether to show the complement sequence. Default true.
    • rotateOnScroll (boolean): Whether the circular viewer rotates on scroll. Default true.
    • disableExternalFonts (boolean): If true, prevents downloading Roboto Mono fonts.