<commit_before><commit_msg>renamed the class, and added some comments (work in progress)<commit_after>// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2007 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
//  
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.

// import the things needed to run this java file.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.AbstractButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import org.pathvisio.data.DataException;
import org.pathvisio.model.ConverterException;


public class GoTermDistributionGUI {
	
	// make 2 private variables. The first one a set of strings "genidInPway",
	// which will be loaded with all the (ensemble formatted) genid's found
	// in the loaded pathways.
	// The second one, a map with a String as key and a GoTerm as value "treeString_GoTerm"
	// will be loaded with the Strings that represent the tree values as a key, and the
	// corresponding GoTerm.
	private static Set<String> genidInPway = new HashSet<String>();
	private static Map<String, GoTerm> treeString_GoTerm = new HashMap<String, GoTerm>();

	/**
	 * This program shows the distribution of GO Terms. It also shows how much
	 * genes it contains; and how much of these genes are found in the pathways.
	 * 
	 * The program requires 4 args:
	 * Gene ontology database (e.g. "C:\\gene_ontology.obo")
	 * Pathway database (e.g. "C:\\databases\\Rn_39_34i.pgdb")
	 * Pathways (e.g. "C:\\WPClient\\Rattus_norvegicus")
	 * Table from GOid to Ensembl (e.g. C:\\mart_export1.txt")	 * 
	 */
	public static void main(String[] args) throws DataException, ConverterException{
		// create a new frame
		JFrame frame = new JFrame("GOTerm Distribution");

		// When click on exit, exit the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// set the size of the frame
		frame.setSize(350,570);
		
		// create a new panel
		JPanel canvasButtons = new JPanel();
		
		// create the tree, using the TreeReader method
		DefaultMutableTreeNode top = TreeReader(args[0],args[1],args[2],args[3]);
		final JTree tree = new JTree(top);
		
		// set the selectionmodel (only one branch can be selected at one time)
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
				
		// create two new buttons, using the makeButton method
		JButton calcButton = makeButton("Calculate");
		JButton closeButton = makeButton("Close");
		
		// add the functionality to the close button
		closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent ae){
						// close the program when the button is clicked
						System.exit(0);
						}
					}
				);
		
		// add the functionality to the calculate button
		calcButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent ae){
						// get the string which represent the name of the branch selected
						String selectedString = tree.getSelectionPath().getLastPathComponent().toString();
						
						// get the goterm belonging to the branch
						GoTerm goterm = treeString_GoTerm.get(selectedString);
						
						// get the number of genes in this term
						String m = goterm.getNumberOfGenes()+"";
						
						// get the number of genes the term overlaps with all the pathway genes
						String n = goterm.getOverlapGenes(genidInPway)+"";
						
						// create a string with the goterm name, and the n and m values 
						// (see above) and print it to the console
						String calcString = goterm.getName() + " " + "("+n+"/"+m+")";
						System.out.println(calcString);
						}
					}
				);
		
		// add the buttons to the canvas
		canvasButtons.add(calcButton);
		canvasButtons.add(closeButton);	
		
		// add the canvas to the frame
		frame.add(canvasButtons, BorderLayout.SOUTH);
		
		// create a new panel
		JPanel canvasTree = new JPanel();
		
		// create a scroll pane containing the tree, set the preferred size and add it to the canvas
        JScrollPane scrollPane = new JScrollPane(tree);
        Dimension scrollPaneSize = new Dimension(315,485);
        scrollPane.setPreferredSize(scrollPaneSize);
		canvasTree.add(scrollPane);
		
		// add the canvas to the frame
		frame.add(canvasTree, BorderLayout.NORTH);
		
		// Show the frame
		frame.setVisible(true);
	}
	
	/**
	 * create a new JButton of a preferred size, and the text centered.
	 */
		public static JButton makeButton(String name){
			// create a new button
			JButton button = new JButton(name);
			
			// set the size of the button
			Dimension sizeButton = new Dimension(130,30);
			button.setPreferredSize(sizeButton);
			
			// center the text (horizontally and vertically) in the button
			button.setVerticalTextPosition(AbstractButton.CENTER);
			button.setHorizontalTextPosition(AbstractButton.CENTER);
			
			// return the button
			return button;
		}
		
		/**
		 * read goterms and genids, and create the tree
		 */
		public static DefaultMutableTreeNode TreeReader(String obo, String pgdb, String pathwayroot, String martexport) throws DataException, ConverterException{
			// create the top of the tree
			DefaultMutableTreeNode top = new DefaultMutableTreeNode("GOTerm Distribution");

			// read all GoTerms, using method readGoDatabase from the GoReader class
			Set<GoTerm> terms = new HashSet<GoTerm>();
			terms = GoReader.readGoDatabase(obo);
			
			// get the Roots of the GoTerms
			Set<GoTerm> roots= new HashSet<GoTerm>();
			roots=GoReader.getRoots(terms);
			
			// add the genes to the terms, using the addGenes method
			terms = addGenes(terms,martexport);
			
			// read all the pathways, extract the genId and
			genidInPway = getN.getSetGenIdsInPways(pgdb,pathwayroot);
			System.out.println("Pathways read");
			
			top = makeTree(roots, terms, top);

			return top;
		}
		
		public static DefaultMutableTreeNode makeTree(Set<GoTerm> parents, Set<GoTerm> allTerms, DefaultMutableTreeNode top){
			// loop trough all given GoTerms
			for(GoTerm parent : parents){
				
				// create a new parent branch
				String m = parent.getNumberOfGenes()+"";
				String n = parent.getOverlapGenes(genidInPway)+"";
				//String n = "n";
				String treeString = parent.getName() + " " + "("+n+"/"+m+")";
				
				DefaultMutableTreeNode par = new DefaultMutableTreeNode(treeString);
				treeString_GoTerm.put(treeString, parent);
				
				// add the new parent to the top structure
				top.add(par);
		
				// if a children list is not empty, set the children as new parents, and put them in
				// this method again
				if(parent.hasChildren()){
				
					// make a list of all children
					Set<GoTerm> children = new HashSet<GoTerm>();
					children = parent.getChildren();
					
					// create a new tree and add it; when an error occures, catch it
					if (par.getLevel() < 4){
						DefaultMutableTreeNode childrenNodes = makeTree(children, allTerms, par);
					
						try{
							par.add(childrenNodes);
						}
						catch(IllegalArgumentException e) {
							//System.out.println("kind is voorouder"); // deze error komt steeds als je een stapje naar beneden gaat.
						}	
					}
				}
			}
			return top;	
		}
		
		public static Set<GoTerm> addGenes(Set<GoTerm> terms, String martexport){
			// create a new map; the key is the GoTerm's id, the set of strings are the gene strings
			Map<String,Set<String>> geneByGO=genesGOid.geneByGO(genesGOid.goByGene(genesGOid.readDatabase(martexport)));
			
			for (GoTerm term: terms){

				Set<String> genes = new HashSet<String>();
				try{
					genes = geneByGO.get(term.getId());
					for (String gene : genes){
						term.addGene(gene);
					}
				}
				catch (NullPointerException e){
					//System.out.println("set is leeg");
				}
			}
						 
			return terms;
		}
}

		
{'fraction_non_alphanumeric': 0.09717068645640074, 'fraction_numerical': 0.00487012987012987, 'mean_word_length': 3.26980198019802, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6637240', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2472, 'n_words': 853}