|
| 1 | +package net.mlin.genomicsqlite; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.HashMap; |
| 8 | + |
| 9 | +public class GenomicSQLite { |
| 10 | + public static String version(Connection conn) throws SQLException { |
| 11 | + ResultSet row = conn.createStatement().executeQuery("SELECT genomicsqlite_version()"); |
| 12 | + row.next(); |
| 13 | + return row.getString(1); |
| 14 | + } |
| 15 | + |
| 16 | + public static String attachSQL( |
| 17 | + Connection conn, String dbFileName, String schemaName, String configJson) |
| 18 | + throws SQLException { |
| 19 | + PreparedStatement stmt = conn.prepareStatement("SELECT genomicsqlite_attach_sql(?,?,?)"); |
| 20 | + stmt.setString(1, dbFileName); |
| 21 | + stmt.setString(2, schemaName); |
| 22 | + stmt.setString(3, configJson); |
| 23 | + ResultSet row = stmt.executeQuery(); |
| 24 | + row.next(); |
| 25 | + return row.getString(1); |
| 26 | + } |
| 27 | + |
| 28 | + public static String attachSQL(Connection conn, String dbFileName, String schemaName) |
| 29 | + throws SQLException { |
| 30 | + return attachSQL(conn, dbFileName, schemaName, "{}"); |
| 31 | + } |
| 32 | + |
| 33 | + public static String vacuumIntoSQL(Connection conn, String destFileName, String configJson) |
| 34 | + throws SQLException { |
| 35 | + PreparedStatement stmt = conn.prepareStatement("SELECT genomicsqlite_vacuum_into_sql(?,?)"); |
| 36 | + stmt.setString(1, destFileName); |
| 37 | + stmt.setString(2, configJson); |
| 38 | + ResultSet row = stmt.executeQuery(); |
| 39 | + row.next(); |
| 40 | + return row.getString(1); |
| 41 | + } |
| 42 | + |
| 43 | + public static String vacuumIntoSQL(Connection conn, String destFileName) throws SQLException { |
| 44 | + return vacuumIntoSQL(conn, destFileName, "{}"); |
| 45 | + } |
| 46 | + |
| 47 | + public static String createGenomicRangeIndexSQL( |
| 48 | + Connection conn, |
| 49 | + String tableName, |
| 50 | + String rid, |
| 51 | + String beginPosition, |
| 52 | + String endPosition, |
| 53 | + int floor) |
| 54 | + throws SQLException { |
| 55 | + PreparedStatement stmt = |
| 56 | + conn.prepareStatement("SELECT create_genomic_range_index_sql(?,?,?,?,?)"); |
| 57 | + stmt.setString(1, tableName); |
| 58 | + stmt.setString(2, rid); |
| 59 | + stmt.setString(3, beginPosition); |
| 60 | + stmt.setString(4, endPosition); |
| 61 | + stmt.setInt(5, floor); |
| 62 | + ResultSet row = stmt.executeQuery(); |
| 63 | + row.next(); |
| 64 | + return row.getString(1); |
| 65 | + } |
| 66 | + |
| 67 | + public static String createGenomicRangeIndexSQL( |
| 68 | + Connection conn, String tableName, String rid, String beginPosition, String endPosition) |
| 69 | + throws SQLException { |
| 70 | + return createGenomicRangeIndexSQL(conn, tableName, rid, beginPosition, endPosition, -1); |
| 71 | + } |
| 72 | + |
| 73 | + public static String putReferenceAssemblySQL(Connection conn, String assembly) |
| 74 | + throws SQLException { |
| 75 | + PreparedStatement stmt = conn.prepareStatement("SELECT put_genomic_reference_assembly_sql(?)"); |
| 76 | + stmt.setString(1, assembly); |
| 77 | + ResultSet row = stmt.executeQuery(); |
| 78 | + row.next(); |
| 79 | + return row.getString(1); |
| 80 | + } |
| 81 | + |
| 82 | + public static String putReferenceSequenceSQL( |
| 83 | + Connection conn, |
| 84 | + String name, |
| 85 | + long length, |
| 86 | + String assembly, |
| 87 | + String refget_id, |
| 88 | + String meta_json, |
| 89 | + long rid) |
| 90 | + throws SQLException { |
| 91 | + PreparedStatement stmt = |
| 92 | + conn.prepareStatement("SELECT put_genomic_reference_sequence_sql(?,?,?,?,?,?)"); |
| 93 | + stmt.setString(1, name); |
| 94 | + stmt.setLong(2, length); |
| 95 | + stmt.setString(3, assembly); |
| 96 | + stmt.setString(4, refget_id); |
| 97 | + stmt.setString(5, meta_json); |
| 98 | + stmt.setLong(6, rid); |
| 99 | + ResultSet row = stmt.executeQuery(); |
| 100 | + row.next(); |
| 101 | + return row.getString(1); |
| 102 | + } |
| 103 | + |
| 104 | + public static String putReferenceSequenceSQL( |
| 105 | + Connection conn, |
| 106 | + String name, |
| 107 | + long length, |
| 108 | + String assembly, |
| 109 | + String refget_id, |
| 110 | + String meta_json) |
| 111 | + throws SQLException { |
| 112 | + PreparedStatement stmt = |
| 113 | + conn.prepareStatement("SELECT put_genomic_reference_sequence_sql(?,?,?,?,?)"); |
| 114 | + stmt.setString(1, name); |
| 115 | + stmt.setLong(2, length); |
| 116 | + if (assembly != null) stmt.setString(3, assembly); |
| 117 | + if (refget_id != null) stmt.setString(4, refget_id); |
| 118 | + if (meta_json != null) stmt.setString(5, meta_json); |
| 119 | + ResultSet row = stmt.executeQuery(); |
| 120 | + row.next(); |
| 121 | + return row.getString(1); |
| 122 | + } |
| 123 | + |
| 124 | + public static String putReferenceSequenceSQL( |
| 125 | + Connection conn, String name, long length, String assembly, String refget_id) |
| 126 | + throws SQLException { |
| 127 | + return putReferenceSequenceSQL(conn, name, length, assembly, refget_id, null); |
| 128 | + } |
| 129 | + |
| 130 | + public static String putReferenceSequenceSQL( |
| 131 | + Connection conn, String name, long length, String assembly) throws SQLException { |
| 132 | + return putReferenceSequenceSQL(conn, name, length, assembly, null, null); |
| 133 | + } |
| 134 | + |
| 135 | + public static String putReferenceSequenceSQL(Connection conn, String name, long length) |
| 136 | + throws SQLException { |
| 137 | + return putReferenceSequenceSQL(conn, name, length, null, null, null); |
| 138 | + } |
| 139 | + |
| 140 | + public static HashMap<Long, ReferenceSequence> getReferenceSequencesByRid( |
| 141 | + Connection conn, String assembly) throws SQLException { |
| 142 | + String sql = |
| 143 | + "SELECT _gri_rid, gri_refseq_name, gri_refseq_length, gri_assembly, gri_refget_id, gri_refseq_meta_json FROM _gri_refseq"; |
| 144 | + PreparedStatement stmt; |
| 145 | + if (assembly != null) { |
| 146 | + stmt = conn.prepareStatement(sql + " WHERE gri_assembly = ?"); |
| 147 | + stmt.setString(1, assembly); |
| 148 | + } else { |
| 149 | + stmt = conn.prepareStatement(sql); |
| 150 | + } |
| 151 | + ResultSet row = stmt.executeQuery(); |
| 152 | + HashMap<Long, ReferenceSequence> ans = new HashMap<Long, ReferenceSequence>(); |
| 153 | + while (row.next()) { |
| 154 | + ReferenceSequence grs = |
| 155 | + new ReferenceSequence( |
| 156 | + row.getLong(1), |
| 157 | + row.getString(2), |
| 158 | + row.getLong(3), |
| 159 | + row.getString(4), |
| 160 | + row.getString(5), |
| 161 | + row.getString(6)); |
| 162 | + ans.put(grs.rid, grs); |
| 163 | + } |
| 164 | + return ans; |
| 165 | + } |
| 166 | + |
| 167 | + public static HashMap<Long, ReferenceSequence> getReferenceSequencesByRid(Connection conn) |
| 168 | + throws SQLException { |
| 169 | + return getReferenceSequencesByRid(conn, null); |
| 170 | + } |
| 171 | + |
| 172 | + public static HashMap<String, ReferenceSequence> getReferenceSequencesByName( |
| 173 | + Connection conn, String assembly) throws SQLException { |
| 174 | + HashMap<String, ReferenceSequence> ans = new HashMap<String, ReferenceSequence>(); |
| 175 | + for (HashMap.Entry<Long, ReferenceSequence> entry : |
| 176 | + getReferenceSequencesByRid(conn, assembly).entrySet()) { |
| 177 | + ReferenceSequence grs = entry.getValue(); |
| 178 | + if (ans.containsKey(grs.name)) { |
| 179 | + throw new RuntimeException( |
| 180 | + "GenomicSQLite.getReferenceSequencesByName: duplicate name; try filtering by assembly"); |
| 181 | + } |
| 182 | + ans.put(grs.name, grs); |
| 183 | + } |
| 184 | + return ans; |
| 185 | + } |
| 186 | + |
| 187 | + public static HashMap<String, ReferenceSequence> getReferenceSequencesByName(Connection conn) |
| 188 | + throws SQLException { |
| 189 | + return getReferenceSequencesByName(conn, null); |
| 190 | + } |
| 191 | +} |
0 commit comments