CGView - Circular Genome Viewer
CGView API
CGView API
The CGView API can be used by other Java applications to create maps. The code examples below are meant to illustrate the steps involved in using the CGView API to generate maps of circular DNA sequences. For more details about the methods available for altering map components, see the Javadocs. To use the CGView API, download the cgview.zip archive and extract it. A directory called cgview will be created. Add the cgview.jar file in this directory to your Classpath. You may also need to add the jar files in the lib directory to your Classpath.

import ca.ualberta.stothard.cgview.*; import java.io.*; public class CgviewTest0 implements CgviewConstants { public static void main( String args[] ) { int length = 9000; Cgview cgview = new Cgview(length); //some optional settings cgview.setWidth(600); cgview.setHeight(600); cgview.setBackboneRadius(160.0f); cgview.setTitle("Example"); cgview.setLabelPlacementQuality(10); cgview.setShowWarning(true); cgview.setLabelLineLength(8.0d); cgview.setLabelLineThickness(0.5f); //create a FeatureSlot to hold sequence features FeatureSlot featureSlot = new FeatureSlot(cgview, DIRECT_STRAND); //create random sequence features for (int i = 1; i <= 100; i = i + 1) { int j = Math.round((float)((float)(length - 2) * Math.random())) + 1; //a Feature to add to our FeatureSlot Feature feature = new Feature(featureSlot, "label"); //a single FeatureRange to add the Feature FeatureRange featureRange = new FeatureRange (feature, j, j + 1); featureRange.setDecoration(DECORATION_CLOCKWISE_ARROW); } try { //create a PNG file CgviewIO.writeToPNGFile(cgview, "random_1.png"); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } } }

Below is the map generated by CgviewTest0.java:

output of above code example

import ca.ualberta.stothard.cgview.*; import java.io.*; import java.awt.*; public class CgviewTest1 implements CgviewConstants { public static void main( String args[] ) { int length = 12078; Cgview cgview = new Cgview(length); //some optional settings cgview.setWidth(600); cgview.setHeight(600); cgview.setBackboneRadius(140.0f); cgview.setTitle("Example 2"); cgview.setLabelPlacementQuality(5); cgview.setShowWarning(false); cgview.setLabelLineLength(15.0d); cgview.setLabelLineThickness(1.0f); cgview.setUseInnerLabels(INNER_LABELS_SHOW); cgview.setMoveInnerLabelsToOuter(true); cgview.setMinimumFeatureLength(1.0d); Legend legend = new Legend(cgview); legend.setPosition(LEGEND_UPPER_CENTER); LegendItem legendItem = new LegendItem(legend); legendItem.setLabel("An example"); legendItem.setFont(new Font("SansSerif", Font.BOLD + Font.ITALIC, 22)); //create FeatureSlots to hold sequence features FeatureSlot directSlot0 = new FeatureSlot(cgview, DIRECT_STRAND); FeatureSlot directSlot1 = new FeatureSlot(cgview, DIRECT_STRAND); FeatureSlot reverseSlot0 = new FeatureSlot(cgview, REVERSE_STRAND); //Features to add to the FeatureSlots Feature feature0 = new Feature(directSlot0, "A"); feature0.setColor(Color.blue); Feature feature1 = new Feature(directSlot1, "B"); feature1.setColor(Color.red); Feature feature2 = new Feature(reverseSlot0, "C"); feature2.setColor(Color.green); //create random sequence features for (int i = 1; i <= 100; i = i + 1) { int j = Math.round((float)((float)(length - 2) * Math.random())) + 1; int k = Math.round((float)((float)(length - 2) * Math.random())) + 1; int l = Math.round((float)((float)(length - 2) * Math.random())) + 1; //a single FeatureRange to add the Feature FeatureRange featureRange0 = new FeatureRange (feature0, j, j + 1); FeatureRange featureRange1 = new FeatureRange (feature1, k, k + 1); FeatureRange featureRange2 = new FeatureRange (feature2, l, l + 1); } try { //create a PNG file CgviewIO.writeToPNGFile(cgview, "random_2.png"); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } } }

Below is the map generated by CgviewTest1.java:

output of above code example

import ca.ualberta.stothard.cgview.*; import java.io.*; import java.awt.*; public class CgviewTest2 implements CgviewConstants { public static void main( String args[] ) { int length = 9000; Cgview cgview = new Cgview(length); //some optional settings cgview.setWidth(600); cgview.setHeight(600); cgview.setBackboneRadius(140.0f); cgview.setBackboneColor(Color.red); cgview.setTitle("Example"); cgview.setLabelPlacementQuality(10); cgview.setShowWarning(true); cgview.setLabelLineLength(8.0d); cgview.setLabelLineThickness(1.5f); cgview.setRulerTextPadding(6.0f); //create a FeatureSlot to hold sequence features FeatureSlot featureSlot = new FeatureSlot(cgview, DIRECT_STRAND); //create random sequence features for (int i = 1; i <= 100; i = i + 1) { int j = Math.round((float)((float)(length - 2) * Math.random())) + 1; //a Feature to add to our FeatureSlot Feature feature = new Feature(featureSlot, "label"); feature.setColor(Color.black); //a single FeatureRange to add the Feature FeatureRange featureRange = new FeatureRange (feature, j, j + 1); featureRange.setDecoration(DECORATION_CLOCKWISE_ARROW); } try { //create a PNG file CgviewIO.writeToPNGFile(cgview, "output.png"); //create a zoomed in PNG file //to draw a zoomed in version, use the setDesiredZoom(double) //and setDesiredZoomCenter(int) methods before drawing. cgview.setDesiredZoom(5.0d); cgview.setDesiredZoomCenter(100); cgview.setLabelLineLength(50.0d); CgviewIO.writeToPNGFile(cgview, "output_zoomed.png"); //create a zoomed in SVG file cgview.setDesiredZoom(5.0d); cgview.setDesiredZoomCenter(4500); cgview.setLabelLineLength(50.0d); cgview.setWidth(750); boolean embedFonts = true; boolean useCompression = false; //creates SVG instead of SVGZ CgviewIO.writeToSVGFile(cgview, "output_zoomed.svg", embedFonts, useCompression); } catch (Exception e) { e.printStackTrace(System.err); System.exit(1); } } }

Below is the output.png file generated by CgviewTest2.java:

output of above code example

Below is the output_zoomed.png file generated by CgviewTest2.java:

output of above code example

Click here to view the output_zoomed.svg file generated by the CgviewTest2.java:

import ca.ualberta.stothard.cgview.*; import java.io.*; import java.awt.*; public class CgviewTest3 implements CgviewConstants { public static void main( String args[] ) { int length = 9000; Cgview cgview = new Cgview(length); //some optional settings cgview.setWidth(750); cgview.setHeight(750); cgview.setBackboneRadius(150.0f); cgview.setBackboneColor(Color.blue); cgview.setTitle("Example"); cgview.setLabelPlacementQuality(5); cgview.setShowWarning(true); cgview.setLabelLineThickness(1.5f); cgview.setRulerTextPadding(6.0f); Legend legend = new Legend(cgview); legend.setPosition(LEGEND_UPPER_CENTER); LegendItem legendItem = new LegendItem(legend); legendItem.setLabel("Point to labels to view mouseover information."); legendItem.setFont(new Font("SansSerif", Font.PLAIN, 20)); legendItem.setTextAlignment(LEGEND_ITEM_ALIGN_CENTER); legendItem = new LegendItem(legend); legendItem.setLabel("Click on labels to test hyperlinks."); legendItem.setFont(new Font("SansSerif", Font.PLAIN, 20)); legendItem.setTextAlignment(LEGEND_ITEM_ALIGN_CENTER); //create a FeatureSlot to hold sequence features FeatureSlot featureSlot = new FeatureSlot(cgview, DIRECT_STRAND); //create random sequence features for (int i = 1; i <= 50; i = i + 1) { int j = Math.round((float)((float)(length - 2) * Math.random())) + 1; //a Feature to add to our FeatureSlot Feature feature = new Feature(featureSlot, "label"); feature.setColor(Color.gray); //a single FeatureRange to add the Feature FeatureRange featureRange = new FeatureRange (feature, j, j + 1); featureRange.setDecoration(DECORATION_CLOCKWISE_ARROW); //add a link and mouseover. Normally you would use a link to a specific //gene or protein featureRange.setHyperlink("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi"); featureRange.setMouseover("Start = " + j + ", stop = " + (j + 1)); } try { //create a PNG map. CgviewIO.writeToPNGFile(cgview, "mouseover_links.png"); //for PNG and JPG files the mouseover and link information //must be placed in an image map. To generate the image map, //use the CgviewIO.writeHTMLFile() method after the image //has been written to file. //the overLIB javascript library (http://www.bosrup.com/web/overlib/) //can be used for displaying mouseovers. You will need to make overlib.js //available to the html file by placing overlib.js inside an 'includes' //directory boolean useOverlib = true; CgviewIO.writeHTMLFile(cgview, "mouseover_links.png", "png", "mouseover_links.html", useOverlib); //create an SVG file. The mouseover and links are included in the SVG. boolean embedFonts = true; boolean useCompression = false; //The previous call to writeToPNGFile generated and positioned //the feature labels in a non-overlapping arrangement. //Label positioning can be slow. Supplying usePreviousDrawnLabels //set to true tells CgviewIO to use the previously arranged labels //instead of generating and positioning new labels. boolean usePreviouslyDrawnLabels = true; CgviewIO.writeToSVGFile(cgview, "mouseover_links.svg", embedFonts, useCompression, usePreviouslyDrawnLabels); } catch (Exception e) { e.printStackTrace(System.err); System.exit(1); } } }

Click here to view the mouseover_links.html file generated by CgviewTest3.java. The mouseover and hyperlink information is included in the html.

Click here to view the mouseover_links.svg file generated by CgviewTest3.java. The mouseover and hyperlink information is included in the SVG.

Citing CGView:
  • Stothard P, Wishart DS. Circular genome visualization and exploration using CGView. Bioinformatics 21:537-539.
Valid XHTML 1.0!Valid CSS!