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 API documentation.
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, "test_maps/CgviewTest0.png");
//create an SVG file
CgviewIO.writeToSVGFile(cgview, "test_maps/CgviewTest0.svg", false);
}
catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
Below is the SVG map generated by CgviewTest0.java:
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, "test_maps/CgviewTest1.png");
//create an SVG file
CgviewIO.writeToSVGFile(cgview, "test_maps/CgviewTest1.svg", false);
}
catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
Below is the SVG map generated by CgviewTest1.java:
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);
}
//zoom in
cgview.setDesiredZoom(5.0d);
cgview.setDesiredZoomCenter(100);
cgview.setLabelLineLength(50.0d);
try {
//create a PNG file
CgviewIO.writeToPNGFile(cgview, "test_maps/CgviewTest2.png");
//create an SVG file
CgviewIO.writeToSVGFile(cgview, "test_maps/CgviewTest2.svg", false);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
Below is the SVG map file generated by 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("https://www.ncbi.nlm.nih.gov/entrez/query.fcgi");
featureRange.setMouseover("Start = " + j + ", stop = " + (j + 1));
}
try {
//create a PNG map.
CgviewIO.writeToPNGFile(cgview, "test_maps/CgviewTest3.png");
//for PNG and JPG files the mouseover and link information
//can 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 (https://github.com/overlib/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,
"CgviewTest3.png",
"png",
"test_maps/CgviewTest3.html",
useOverlib
);
//create an SVG file. The mouseover and links are included in the SVG.
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,
"test_maps/CgviewTest3.svg",
useCompression,
usePreviouslyDrawnLabels
);
System.exit(0);
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
Click here to view the CgviewTest3.html file generated by CgviewTest3.java. The mouseover and hyperlink information is included in the file.
Click here to view the CgviewTest3.svg file generated by CgviewTest3.java. The mouseover and hyperlink information is included in the file.
Citing CGView:
|