<commit_before>package org.cytoscape.kegg.browser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import giny.view.NodeView;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.cytoscape.data.reader.kgml.KEGGEntryType;

import cytoscape.Cytoscape;
import cytoscape.data.CyAttributes;
import cytoscape.view.CyNetworkView;

import ding.view.NodeContextMenuListener;

/**
 * Add context menu for KEGG pathways
 * 
 * @author kono
 * 
 */
public class KEGGNodeContextMenuListener implements NodeContextMenuListener {
	private static final String COMPOUND_URL = "http://www.kegg.jp/Fig/compound/";
	private static final String REACTION_URL = "http://www.kegg.jp/Fig/reaction_small/";
	private static final String MAP_URL = "http://www.genome.jp/tmp/pathway_thumbnail/";

	private static final String COMPOUND_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?cpd:";
	private static final String REACTION_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?";
	private static final String MAP_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?pathway+";

	private CyAttributes nodeAttr = Cytoscape.getNodeAttributes();
	private final CyNetworkView view;

	public KEGGNodeContextMenuListener(CyNetworkView view) {
		this.view = view;
	}

	@Override
	public void addNodeContextMenuItems(NodeView nv, JPopupMenu menu) {
		if (menu == null)
			return;

		final JMenu keggMenu = new JMenu("KEGG Options");

		final String attrValue = nodeAttr.getStringAttribute(nv.getNode()
				.getIdentifier(), "KEGG.entry");
		if (attrValue == null)
			return;

		final KEGGEntryType entryType = KEGGEntryType.getType(attrValue);

		if (entryType == null)
			return;

		JMenuItem item = new JMenuItem();

		if (entryType.equals(KEGGEntryType.COMPOUND)) {
			try {
				final String compoundID = nodeAttr.getStringAttribute(nv
						.getNode().getIdentifier(), "KEGG.label");
				URL image = new URL(COMPOUND_URL + compoundID + ".gif");
				item.setIcon(new ImageIcon(image));
				item.setText("Compound: " + compoundID);
				item.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						cytoscape.util.OpenBrowser.openURL(COMPOUND_DBGET_URL
								+ compoundID);
					}
				});
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}

		} else if (entryType.equals(KEGGEntryType.MAP)) {
			final String mapID = nodeAttr.getStringAttribute(
					nv.getNode().getIdentifier(), "KEGG.name").split(":")[1];
			URL image = null;
			try {
				image = new URL(MAP_URL + mapID + ".png");
				item.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						cytoscape.util.OpenBrowser.openURL(MAP_DBGET_URL
								+ mapID);
					}
				});
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			item.setIcon(new ImageIcon(image));
			item.setText("Pathway: " + mapID);
		} else if (entryType.equals(KEGGEntryType.GENE)
				|| entryType.equals(KEGGEntryType.ORTHOLOG)) {
			try {
				final String reactionID = nodeAttr.getStringAttribute(nv.getNode()
						.getIdentifier(), "KEGG.reaction");<commit_msg>import maplink pathway from ftp site<commit_after>package org.cytoscape.kegg.browser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import giny.view.NodeView;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.cytoscape.data.reader.kgml.KEGGEntryType;

import cytoscape.Cytoscape;
import cytoscape.actions.LoadNetworkTask;
import cytoscape.data.CyAttributes;
import cytoscape.view.CyNetworkView;

import ding.view.NodeContextMenuListener;
import cytoscape.actions.LoadNetworkTask;

/**
 * Add context menu for KEGG pathways
 * 
 * @author kono
 * 
 */
public class KEGGNodeContextMenuListener implements NodeContextMenuListener {
	private static final String COMPOUND_URL = "http://www.kegg.jp/Fig/compound/";
	private static final String REACTION_URL = "http://www.kegg.jp/Fig/reaction_small/";
	private static final String MAP_URL = "http://www.genome.jp/tmp/pathway_thumbnail/";

	private static final String COMPOUND_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?cpd:";
	private static final String REACTION_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?";
	private static final String MAP_DBGET_URL = "http://www.genome.jp/dbget-bin/www_bget?pathway+";
	
	private static final String KGML_URL = "ftp://ftp.genome.jp/pub/kegg/xml/kgml/metabolic/organisms/";

	private CyAttributes nodeAttr = Cytoscape.getNodeAttributes();
	private final CyNetworkView view;

	public KEGGNodeContextMenuListener(CyNetworkView view) {
		this.view = view;
	}
	
	private URL convertToFTP(String mapID) throws MalformedURLException {
		return new URL(KGML_URL + mapID.substring(0, 3) + "/" + mapID + ".xml");
	}

	@Override
	public void addNodeContextMenuItems(NodeView nv, JPopupMenu menu) {
		if (menu == null)
			return;

		final JMenu keggMenu = new JMenu("KEGG Options");

		final String attrValue = nodeAttr.getStringAttribute(nv.getNode()
				.getIdentifier(), "KEGG.entry");
		if (attrValue == null)
			return;

		final KEGGEntryType entryType = KEGGEntryType.getType(attrValue);

		if (entryType == null)
			return;

		JMenuItem item = new JMenuItem();

		if (entryType.equals(KEGGEntryType.COMPOUND)) {
			try {
				final String compoundID = nodeAttr.getStringAttribute(nv
						.getNode().getIdentifier(), "KEGG.label");
				URL image = new URL(COMPOUND_URL + compoundID + ".gif");
				item.setIcon(new ImageIcon(image));
				item.setText("Compound: " + compoundID);
				item.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						cytoscape.util.OpenBrowser.openURL(COMPOUND_DBGET_URL
								+ compoundID);
					}
				});
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}

		} else if (entryType.equals(KEGGEntryType.MAP)) {
			final String mapID = nodeAttr.getStringAttribute(
					nv.getNode().getIdentifier(), "KEGG.name").split(":")[1];
			URL image = null;
			try {
				image = new URL(MAP_URL + mapID + ".png");
				item.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
//						cytoscape.util.OpenBrowser.openURL(MAP_DBGET_URL
//								+ mapID);
						try {
							LoadNetworkTask.loadURL(convertToFTP(mapID), true);
						} catch (MalformedURLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				});
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			item.setIcon(new ImageIcon(image));
			item.setText("Pathway: " + mapID);
		} else if (entryType.equals(KEGGEntryType.GENE)
				|| entryType.equals(KEGGEntryType.ORTHOLOG)) {
			try {
				final String reactionID = nodeAttr.getStringAttribute(nv.getNode()
						.getIdentifier(), "KEGG.reaction");
{'fraction_non_alphanumeric': 0.12038207613428853, 'fraction_numerical': 0.0008428150021070375, 'mean_word_length': 4.151953690303907, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 18, 'xml': 2}, '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': '4742543', 'n_tokens_mistral': 2691, 'n_tokens_neox': 2275, 'n_words': 456}