<commit_before>package org.broad.igv;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author <NAME>
 * @date 11/3/11
 */
public class Launcher {

    public static void main(String[] args) throws IOException {

        String file = null;
        String locus = null;
        String genome = null;
        String user = null;
        String memory = null;
        String index = null;
        boolean newSession = false;

        for (String a : args) {
            String tokens[] = a.split("=");
            if (tokens.length == 2) {
                String key = tokens[0].toLowerCase();
                if (key.equals("file")) {
                    file = tokens[1];
                } else if (key.equals("locus")) {
                    locus = tokens[1];
                } else if (key.equals("genome")) {
                    genome = tokens[1];
                } else if (key.equals("user")) {
                    user = tokens[1];
                } else if (key.equals("maxheapsize")) {
                    memory = tokens[1];
                } else if (key.equals("index")) {
                    index = tokens[1];
                } else if (key.equals("newsession")) {
                    newSession = tokens[1].equals("true");
                }
            }
        }


        // TODO -- read port from igv preferences
        int port = 60151;
        boolean igvIsRunning = loadDirectly(port, file, locus, genome, newSession);

        if (!igvIsRunning) {

            // Launch a new IGV

            StringBuffer buf = new StringBuffer("http://www.broadinstitute.org/igv/projects/dev/igv.php");

            boolean firstArg = true;
            if (file != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("sessionURL=");
                buf.append(file);
                firstArg = false;
            }
            if (locus != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("locus=");
                buf.append(locus);
                firstArg = false;

            }
            if (genome != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("genome=");
                buf.append(genome);
                firstArg = false;

            }
            if (user != null) {

                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("user=");
                buf.append(user);
                firstArg = false;
            }

            File jnlpFile = createJNLP(file, locus, genome, memory, index);
            System.out.println(jnlpFile.getAbsolutePath());

            ProcessBuilder pb = new ProcessBuilder("javaws", jnlpFile.getAbsolutePath());
            Process p = pb.start();
            // TODO -- read from stderr and report any errors to user


        }

        // Cleanup jnlp files
        File desktop = new File(System.getProperty("user.home") + "/Desktop");
        if (desktop.exists() && desktop.isDirectory()) {
            cleanupJnlpFiles(desktop);
        }
        File downloads = new File(System.getProperty("user.home") + "/Downloads");
        if (downloads.exists() && downloads.isDirectory()) {
            cleanupJnlpFiles(downloads);
        }


        System.exit(1);
    }


    private static boolean loadDirectly(int port, String file, String locus, String genome, boolean newSession) throws IOException {
        boolean success;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            socket = new Socket("127.0.0.1", port);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            if (genome != null) {
                out.println("genome " + genome);
                String response = in.readLine();
            }<commit_msg>Launcher update<commit_after>package org.broad.igv;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author <NAME>
 * @date 11/3/11
 */
public class Launcher {

    public static void main(String[] args) throws IOException {

        String file = null;
        String locus = null;
        String genome = null;
        String user = null;
        String memory = null;
        String index = null;

        // First get file
        for (String a : args) {
            String tokens[] = a.split("=");
            if (tokens.length == 2) {
                String key = tokens[0].toLowerCase();
                if (key.equals("file")) {
                    file = tokens[1];
                }
            }
        }

        // Default "newSession" value depends on type -- true for sessions, false for files
        boolean fileIsSession = (file != null && (file.endsWith(".xml") || file.endsWith(".php") || file.endsWith(".php3")));
        boolean newSession = !fileIsSession;

        for (String a : args) {
             String tokens[] = a.split("=");
             if (tokens.length == 2) {
                 String key = tokens[0].toLowerCase();
                  if (key.equals("locus")) {
                    locus = tokens[1];
                } else if (key.equals("genome")) {
                    genome = tokens[1];
                } else if (key.equals("user")) {
                    user = tokens[1];
                } else if (key.equals("maxheapsize")) {
                    memory = tokens[1];
                } else if (key.equals("index")) {
                    index = tokens[1];
                } else if (key.equals("newsession")) {
                    newSession = tokens[1].equals("true");
                }
            }
        }


        // TODO -- read port from igv preferences
        int port = 60151;
        boolean igvIsRunning = loadDirectly(port, file, locus, genome, newSession);

        if (!igvIsRunning) {

            // Launch a new IGV

            StringBuffer buf = new StringBuffer("http://www.broadinstitute.org/igv/projects/dev/igv.php");

            boolean firstArg = true;
            if (file != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("sessionURL=");
                buf.append(file);
                firstArg = false;
            }
            if (locus != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("locus=");
                buf.append(locus);
                firstArg = false;

            }
            if (genome != null) {
                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("genome=");
                buf.append(genome);
                firstArg = false;

            }
            if (user != null) {

                String delim = firstArg ? "?" : "&";
                buf.append(delim);
                buf.append("user=");
                buf.append(user);
                firstArg = false;
            }

            File jnlpFile = createJNLP(file, locus, genome, memory, index);
            System.out.println(jnlpFile.getAbsolutePath());

            ProcessBuilder pb = new ProcessBuilder("javaws", jnlpFile.getAbsolutePath());
            Process p = pb.start();
            // TODO -- read from stderr and report any errors to user


        }

        // Cleanup jnlp files
        File desktop = new File(System.getProperty("user.home") + "/Desktop");
        if (desktop.exists() && desktop.isDirectory()) {
            cleanupJnlpFiles(desktop);
        }
        File downloads = new File(System.getProperty("user.home") + "/Downloads");
        if (downloads.exists() && downloads.isDirectory()) {
            cleanupJnlpFiles(downloads);
        }


        System.exit(1);
    }


    private static boolean loadDirectly(int port, String file, String locus, String genome, boolean newSession) throws IOException {
        boolean success;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            socket = new Socket("127.0.0.1", port);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));



            if (genome != null) {
                out.println("genome " + genome);
                String response = in.readLine();
            }
{'fraction_non_alphanumeric': 0.12495712815822568, 'fraction_numerical': 0.0062878701269006515, 'mean_word_length': 1.489470688673876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7544795', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2231, 'n_words': 559}