package genediagram;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GeneDiagram {
    
    public static int minstart = -1;
    public static int maxstop = -1;
    public static int numberofcategories;
    public static ArrayList<Gene> genes = new ArrayList();
    public static ArrayList<String> categories = new ArrayList();
    public static ArrayList<Color> colours = new ArrayList();

    public static void main(String[] args) {
        
        //Getting data from file
        BufferedReader fileReader = null;
        
        try {
            fileReader = new BufferedReader(new FileReader(args[0]));
            String line;
 
            while ((line = fileReader.readLine()) != null) {
                
                //Getting details for gene
                String[] details = line.split("\t");
                Gene gene = new Gene(details);
                genes.add(gene);
                
                //Adding category to list of categories
                if (categories.contains(gene.category) == false) {
                    categories.add(gene.category);
                }
                
                //Getting overall start and stop co-ordinates
                if (minstart == -1) {
                    minstart = gene.start;
                } else if (minstart > gene.start) {
                    minstart = gene.start;
                }
                
                if (maxstop == -1) {
                    maxstop = gene.stop;
                } else if (maxstop < gene.start) {
                    maxstop = gene.stop;
                }
            }
            
            fileReader.close();
        
            //Setting category colours
            numberofcategories = categories.size();
            
            //Creating array of colours for first 9 categories
            Color[] standardcolours = {Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE,Color.CYAN,Color.PINK,Color.GRAY};
        
            //Moving pseudogene to the end of the list if present to allow preceding categories to be assigned non-white colours
            if (categories.contains("pseudogene")) {
                categories.remove("pseudogene");
                categories.add("pseudogene");
            }
        
            //Creating list of colours corresponding to categories
            for (int i=0; i<numberofcategories; i++) {
                if (categories.get(i).equals("pseudogene")) {
                    colours.add(Color.WHITE);
                } else if (i < 9) {
                    colours.add(standardcolours[i]);
                } else if (i < 31) {
                    colours.add(new Color(10*i-50,10*i-50,10*i-50));
                } else {
                    System.out.println("Too many categories to display distinct colours");
                }    
            }
          
            SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Determining width of longest text string in legend so that diagram can be sized appropriately
                getStringWidth();
                
                //Creating genes diagram
                createDiagram();
            }
            });
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //Method for determining width of longest text string in legend
    public static void getStringWidth () {
        StringWidth stringwidth = new StringWidth();
        JFrame frame = new JFrame();
        frame.add(stringwidth);
        frame.pack();
        frame.setVisible(true);
        Container content = frame.getContentPane();
        BufferedImage img = new BufferedImage(content.getWidth(), content.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();
        content.printAll(g2d);
        g2d.dispose();
        
    }
    
    //Method for drawing diagram
    public static void createDiagram() {
        
        //Creating genes diagram object
        Diagram diagram = new Diagram(genes, categories, colours);
        
        //Setting dimensions based on size of gene area to be displayed and width of longest text string in legend
        diagram.setPreferredSize(new Dimension(maxstop-minstart+(diagram.height/diagram.colours.size())/2+StringWidth.width+300,diagram.height));
        
        //Displaying diagram on frame
        JFrame frame = new JFrame();
        frame.add(diagram);
        frame.pack();
        frame.setVisible(true);
        
        //Saving contents of frame
        Container content = frame.getContentPane();
        BufferedImage img = new BufferedImage(content.getWidth(), content.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();
        content.printAll(g2d);
        g2d.dispose();
        try {
            ImageIO.write(img, "jpg", new File("gene_diagram.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }   
}

@@ -438,25 +438,25 @@
 atic int min
-s
+S
 tart = -1;%0A 
@@ -475,25 +475,25 @@
 atic int max
-s
+S
 top = -1;%0A  
@@ -511,30 +511,25 @@
 atic int num
-berofc
+C
 ategories;%0A 
@@ -1525,25 +1525,25 @@
      if (min
-s
+S
 tart == -1) 
@@ -1559,33 +1559,33 @@
              min
-s
+S
 tart = gene.star
@@ -1617,17 +1617,17 @@
  if (min
-s
+S
 tart %3E g
@@ -1662,17 +1662,17 @@
      min
-s
+S
 tart = g
@@ -1736,25 +1736,25 @@
      if (max
-s
+S
 top == -1) %7B
@@ -1769,33 +1769,33 @@
              max
-s
+S
 top = gene.stop;
@@ -1825,17 +1825,17 @@
  if (max
-s
+S
 top %3C ge
@@ -1869,17 +1869,17 @@
      max
-s
+S
 top = ge
@@ -2027,22 +2027,17 @@
      num
-berofc
+C
 ategorie
@@ -2159,25 +2159,25 @@
 r%5B%5D standard
-c
+C
 olours = %7BCo
@@ -2693,14 +2693,9 @@
 %3Cnum
-berofc
+C
 ateg
@@ -2892,17 +2892,17 @@
 standard
-c
+C
 olours%5Bi
@@ -4574,17 +4574,17 @@
 (max
-s
+S
 top-min
-s
+S
 tart

Update GeneDiagram.java