Java JDBC MySQL Connection computeAverageGeneIdsPerName()

Here you can find the source of computeAverageGeneIdsPerName()

Description

compute Average Gene Ids Per Name

License

Open Source License

Declaration

public static void computeAverageGeneIdsPerName() throws SQLException 

Method Source Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void computeAverageGeneIdsPerName() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://wanaheim:3306/user_cplake", "cplake",
                "cplake");

        Map<String, Set<Integer>> name2geneIdCount = new HashMap<String, Set<Integer>>();

        Statement statement = connection.createStatement();

        ResultSet resultSet = statement.executeQuery(
                "SELECT gene.ID, gene.name, protein.name FROM GR_Names gene, GR_ProteinNames protein");
        //ResultSet resultSet = statement.executeQuery("SELECT gene.ID, gene.name FROM GR_Names gene");
        while (resultSet.next()) {
            Integer geneId = resultSet.getInt(1);
            String geneName = resultSet.getString(2);
            String proteinName = resultSet.getString(3);

            Set<Integer> geneIds = name2geneIdCount.get(geneName);
            if (geneIds == null) {
                geneIds = new HashSet<Integer>();
                name2geneIdCount.put(geneName, geneIds);
            }//from w w  w  . ja  v a 2 s .  c om
            geneIds.add(geneId);

            geneIds = name2geneIdCount.get(proteinName);
            if (geneIds == null) {
                geneIds = new HashSet<Integer>();
                name2geneIdCount.put(proteinName, geneIds);
            }
            geneIds.add(geneId);
        }
        resultSet.close();
        statement.close();
        connection.close();

        int averageIdsPerName = 0;
        for (String name : name2geneIdCount.keySet()) {
            Integer idCount = name2geneIdCount.get(name).size();
            averageIdsPerName += idCount;
        }

        System.out.println(
                "Average Gene IDs per Name: " + ((double) averageIdsPerName / (double) name2geneIdCount.size()));

        System.out.println("finished");
    }
}

Related

  1. addItem(int itemId, String itemName, int page, String tradeable)
  2. checkConnection(String dbFile)
  3. createConnection()
  4. createConnection(String hostname, String port, String database, String username, String password)
  5. createConnection(String url, String user, String password)
  6. createConnectionUtf8(String connString)