use of com.github.lindenb.jvarkit.jexl.JexlToString in project jvarkit by lindenb.
the class VcfUcscGdb method readRemoteResources.
private List<RemoteBigFile> readRemoteResources(final Path path) throws IOException {
final List<RemoteBigFile> remoteBigFiles = new ArrayList<>();
IOUtil.assertFileIsReadable(path);
try (BufferedReader br = IOUtil.openFileForBufferedReading(path)) {
final HashMap<String, String> hash = new HashMap<>();
final Function<String, String> required = (K) -> {
if (!hash.containsKey(K))
throw new RuntimeIOException("Key \"" + K + "\" missing. Found: " + hash.keySet());
final String v = hash.get(K).trim();
if (StringUtils.isBlank(v))
throw new RuntimeIOException("Key \"" + K + "\" is empty");
return v;
};
try (LineIterator iter = new LineIterator(br)) {
for (; ; ) {
final String line = (iter.hasNext() ? iter.next() : null);
if (StringUtils.isBlank(line)) {
if (hash.getOrDefault("enabled", "true").equals("false")) {
hash.clear();
}
if (!hash.isEmpty()) {
final RemoteBigFile bf = new RemoteBigFile();
bf.url = required.apply("url");
if (hash.containsKey("name")) {
bf.name = hash.get("name");
} else {
bf.name = bf.url;
int slah = bf.name.lastIndexOf('/');
bf.name = bf.name.substring(slah + 1);
int dot = bf.name.lastIndexOf('.');
bf.name = bf.name.substring(0, dot).replace('.', '_').replace('-', '_').replace(',', '_');
}
if (remoteBigFiles.stream().anyMatch(R -> R.name.equals(bf.name))) {
bf.close();
throw new RuntimeIOException("Duplicate remote resource: " + hash);
}
if (hash.containsKey("accept")) {
bf.accept = new JexlPredicate(hash.get("accept"));
}
if (hash.containsKey("tostring")) {
bf.converter = new JexlToString(hash.get("tostring"));
}
if (hash.containsKey("desc")) {
bf.description = hash.get("desc");
} else if (hash.containsKey("description")) {
bf.description = hash.get("description");
} else {
bf.description = "Data from " + bf.url;
}
if (hash.containsKey("limit")) {
bf.limit = Integer.parseInt(hash.get("limit"));
}
if (hash.containsKey("fractV")) {
bf.fractionOfVariant = Double.parseDouble(hash.get("fractV"));
}
if (hash.containsKey("fractF")) {
bf.fractionOfVariant = Double.parseDouble(hash.get("fractF"));
}
if (hash.containsKey("aggregate")) {
bf.wigAggregate = hash.get("aggregate");
if (!(bf.wigAggregate.equals("min") || bf.wigAggregate.equals("max"))) {
bf.close();
throw new RuntimeIOException("Bad value for aggregate accepted:(min/max))");
}
}
remoteBigFiles.add(bf);
}
if (line == null)
break;
hash.clear();
continue;
}
if (line.startsWith("#"))
continue;
int sep = line.indexOf(':');
if (sep == -1)
sep = line.indexOf('=');
if (sep == -1)
throw new RuntimeIOException("Cannot find ':' or '=' in " + line);
final String key = line.substring(0, sep).toLowerCase().trim();
if (hash.containsKey(key))
throw new RuntimeIOException("Duplicate key " + key + " in resource: " + hash);
final String value = line.substring(sep + 1).trim();
hash.put(key, value);
}
}
}
return remoteBigFiles;
}
use of com.github.lindenb.jvarkit.jexl.JexlToString in project jvarkit by lindenb.
the class VCFBed method beforeVcf.
@Override
protected int beforeVcf() {
if (this.inputBedFile == null) {
LOG.error("Undefined bed file file");
return -1;
}
if (StringUtil.isBlank(this.infoName)) {
LOG.error("Undefined INFO name.");
return -1;
}
if (min_overlap_both_fraction != null) {
if (this.min_overlap_both_fraction <= 0.0) {
LOG.error("bad value for min_overlap_both_fraction");
return -1;
}
this.min_overlap_bed_fraction = min_overlap_both_fraction;
this.min_overlap_vcf_fraction = min_overlap_both_fraction;
}
if (this.min_overlap_bed_fraction != null && this.min_overlap_bed_fraction <= 0.0) {
LOG.error("bad value for min_overlap_bed_fraction");
return -1;
}
if (this.min_overlap_vcf_fraction != null && this.min_overlap_vcf_fraction <= 0.0) {
LOG.error("bad value for min_overlap_vcf_fraction");
return -1;
}
this.bedJexlToString = new JexlToString(this.formatPattern);
if (!this.in_memory) {
try {
this.bedReader = new IndexedBedReader(this.inputBedFile.toString());
this.contigNameConverter = ContigNameConverter.fromContigSet(this.bedReader.getContigs());
this.intervalTreeMap = null;
} catch (final IOException err) {
LOG.error(err);
return -1;
}
} else {
try {
this.bedReader = null;
this.intervalTreeMap = this.readBedFileAsIntervalTreeMap(this.inputBedFile);
this.contigNameConverter = ContigNameConverter.fromIntervalTreeMap(this.intervalTreeMap);
} catch (final Exception err) {
LOG.error(err);
return -1;
}
}
return 0;
}
use of com.github.lindenb.jvarkit.jexl.JexlToString in project jvarkit by lindenb.
the class VcfUcsc method beforeVcf.
@Override
protected int beforeVcf() {
int max_column_index = 0;
try {
if (StringUtil.isBlank(this.table)) {
LOG.error("Table undefined.");
return -1;
}
if (!StringUtil.isBlank(this.filterIn) && !StringUtil.isBlank(this.filterOut)) {
LOG.error("both filters in/out defined.");
return -1;
}
if (!StringUtil.isBlank(this.acceptExpr)) {
this.acceptRowFunc = new JexlPredicate(this.acceptExpr);
}
if (!StringUtil.isBlank(this.convertToStrExpr)) {
this.toStringFunc = new JexlToString(this.convertToStrExpr);
}
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
try {
LOG.info("Getting jdbc-driver");
Class.forName("com.mysql.jdbc.Driver");
this.connection = DriverManager.getConnection(jdbcuri + "/" + database + "?user=genome&password=");
LOG.info("Getting jdbc-driver: Done.");
final Statement stmt = this.connection.createStatement();
final ResultSet row = stmt.executeQuery("describe " + this.database + "." + this.table);
final Set<String> cols = new HashSet<String>();
while (row.next()) {
final String colName = row.getString("Field");
if (StringUtil.isBlank(colName)) {
LOG.error("empty field in " + this.database + "." + this.table);
return -1;
}
cols.add(colName);
}
row.close();
stmt.close();
this.has_bin_column = cols.contains("bin");
if (max_column_index > cols.size()) {
LOG.error("No column index[" + max_column_index + "] for " + cols + " N=" + cols.size());
return -1;
}
for (final String col : new String[] { "chrom" }) {
if (this.chromColumn == null && cols.contains(col)) {
this.chromColumn = col;
}
}
if (this.chromColumn == null) {
LOG.error("cannot find 'chrom' in the columns of '" + this.database + "." + this.table + "' : " + cols);
return -1;
}
for (final String col : new String[] { "txStart", "cdsStart", "chromStart" }) {
if (this.startColumn == null && cols.contains(col)) {
this.startColumn = col;
}
}
if (this.startColumn == null) {
LOG.error("cannot find startColumn in " + cols);
return -1;
}
for (final String col : new String[] { "txEnd", "cdsEnd", "chromEnd" }) {
if (this.endColumn == null && cols.contains(col)) {
this.endColumn = col;
}
}
if (this.endColumn == null) {
LOG.error("cannot find endColumn in " + cols);
return -1;
}
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
}
Aggregations