|
| 1 | +package objects; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.awt.Graphics; |
| 6 | +import java.awt.Polygon; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | + |
| 10 | +import javax.swing.JPanel; |
| 11 | + |
| 12 | +/** |
| 13 | + * Component that displays a graphic of a sequenced DNA fragment with annotated |
| 14 | + * Read1, Read2, and dynamically updating encoding reference points marked by |
| 15 | + * arrows. |
| 16 | + * |
| 17 | + * @author Olivia Lang |
| 18 | + * |
| 19 | + */ |
| 20 | +public class ReadFragmentCartoon extends JPanel { |
| 21 | + |
| 22 | + int readHeight = 20; |
| 23 | + int readWidth = 100; |
| 24 | + public int cartoonHeight = 80; |
| 25 | + public int arrowHeight = 15; |
| 26 | + public int arrowWidth = 6; |
| 27 | + int fragmentLength = 480; |
| 28 | + int fragmentThickness = 3; |
| 29 | + int edgePad = 20; |
| 30 | + int verticalPad; |
| 31 | + |
| 32 | + public ArrayList<Polygon> arrows = new ArrayList<Polygon>(6); |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void paintComponent(Graphics g) { |
| 36 | + super.paintComponent(g); |
| 37 | + |
| 38 | + // Determine distance from top of graphic |
| 39 | + verticalPad = (cartoonHeight - 2*(readHeight) - fragmentThickness - arrowHeight) / 2 + arrowHeight; |
| 40 | + |
| 41 | + // Draw Read 1 |
| 42 | + g.setColor(Color.CYAN); |
| 43 | + g.fillRect(edgePad, verticalPad, readWidth, readHeight); |
| 44 | + |
| 45 | + // Draw Read 2 |
| 46 | + g.setColor(Color.ORANGE); |
| 47 | + g.fillRect(edgePad+fragmentLength - readWidth, verticalPad + readHeight + fragmentThickness, readWidth, readHeight); |
| 48 | + |
| 49 | + // Draw Fragment |
| 50 | + g.setColor(Color.BLACK); |
| 51 | + g.fillRect(edgePad, verticalPad + readHeight, fragmentLength, fragmentThickness); |
| 52 | + |
| 53 | + // Label Reads |
| 54 | + g.drawString("Read1", edgePad, verticalPad + readHeight - 1); |
| 55 | + g.drawString("Read2", edgePad + fragmentLength - 37, verticalPad + 2*readHeight + fragmentThickness - 1); |
| 56 | + |
| 57 | + // Draw arrows |
| 58 | + for (int p=0; p<arrows.size(); p++) { |
| 59 | + g.fillPolygon(arrows.get(p)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Update graphic to reflect changes in aspect and read encodings. |
| 65 | + * |
| 66 | + * @param aspect |
| 67 | + * @param read |
| 68 | + */ |
| 69 | + public void redrawArrows(int aspect, int read) { |
| 70 | + verticalPad = (cartoonHeight - (2 * readHeight) - fragmentThickness - arrowHeight) / 2 + arrowHeight; |
| 71 | + arrows = new ArrayList<Polygon>(2); |
| 72 | + int topY = verticalPad - arrowHeight; |
| 73 | + if (aspect == PileupParameters.FRAGMENT) { // handle fragment |
| 74 | +// arrows.add(bigArrow()); |
| 75 | + for (int i = 0; i < fragmentLength; i += 6) { |
| 76 | + arrows.add(arrow(edgePad + i, topY)); |
| 77 | + } |
| 78 | + } else if (aspect == PileupParameters.MIDPOINT) { // handle midpoint |
| 79 | + arrows.add(arrow(edgePad + fragmentLength / 2, topY)); |
| 80 | + } else if (aspect == PileupParameters.THREE) { // handle 3 prime ends |
| 81 | + if (read == PileupParameters.READ1 || read == PileupParameters.ALLREADS) { |
| 82 | + arrows.add(arrow(edgePad + readWidth, topY)); |
| 83 | + } |
| 84 | + if (read == PileupParameters.READ2 || read == PileupParameters.ALLREADS) { |
| 85 | + arrows.add(arrow(edgePad + fragmentLength - readWidth, topY)); |
| 86 | + } |
| 87 | + } else if (aspect == PileupParameters.FIVE) { // handle 5 prime ends |
| 88 | + if (read == PileupParameters.READ1 || read == PileupParameters.ALLREADS) { |
| 89 | + arrows.add(arrow(edgePad, topY)); |
| 90 | + } |
| 91 | + if (read == PileupParameters.READ2 || read == PileupParameters.ALLREADS) { |
| 92 | + arrows.add(arrow(edgePad + fragmentLength, topY)); |
| 93 | + } |
| 94 | + } |
| 95 | + repaint(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Unused legacy code for how Full Fragment was initially represented: a |
| 100 | + * rightward-pointing arrow across length of DNA fragment. |
| 101 | + * @return |
| 102 | + */ |
| 103 | + public Polygon bigArrow() { |
| 104 | + int arrowThickness = 5; |
| 105 | + int arrowHead = 20; |
| 106 | + int arrowflank = (arrowHeight - arrowThickness)/2; |
| 107 | + |
| 108 | + int xPoly[] = {edgePad, edgePad+fragmentLength-arrowHead, edgePad+fragmentLength-arrowHead, |
| 109 | + edgePad+fragmentLength, edgePad +fragmentLength -arrowHead, edgePad+fragmentLength-arrowHead, edgePad}; |
| 110 | + int yPoly[] = {verticalPad-arrowflank, verticalPad-arrowflank, verticalPad, verticalPad-arrowHeight/2, |
| 111 | + verticalPad-arrowHeight, verticalPad-arrowHeight+arrowflank, verticalPad-arrowHeight+arrowflank}; |
| 112 | + return(new Polygon(xPoly,yPoly,xPoly.length)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Draw downward-pointing arrow at (x,y) coordinate (upper-center) for marking |
| 117 | + * where on the DNA fragment coverage is tallied on this graphic. Arrow size is |
| 118 | + * arrowHeight tall (15px) and flank*2 (6px) wide. |
| 119 | + * |
| 120 | + * (x,y) |
| 121 | + * || |
| 122 | + * || |
| 123 | + * || |
| 124 | + * || |
| 125 | + * \ / |
| 126 | + * \/ |
| 127 | + * |
| 128 | + * @param x |
| 129 | + * @param y |
| 130 | + * @return |
| 131 | + */ |
| 132 | + public Polygon arrow(int x,int y) { |
| 133 | + |
| 134 | + // arrowhead flank size |
| 135 | + int flank = arrowWidth/2; |
| 136 | + // arrow thickness |
| 137 | + int thick = 2; |
| 138 | + // arrowhead height |
| 139 | + int head = 5; |
| 140 | + |
| 141 | + x = x-thick/2; |
| 142 | + |
| 143 | + int xPoly[] = {x, x, x-flank, x+thick/2, x+thick+flank, x+thick, x+thick}; |
| 144 | + int yPoly[] = {y, y+arrowHeight-head, y+arrowHeight-head, y+arrowHeight, y+arrowHeight-head, y+arrowHeight-head, y}; |
| 145 | + return(new Polygon(xPoly,yPoly,xPoly.length)); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Dimension getPreferredSize() { |
| 150 | + return new Dimension(800,cartoonHeight); |
| 151 | + } |
| 152 | +} |
0 commit comments