use of com.github.lindenb.jvarkit.util.tabix.TabixFileReader in project jvarkit by lindenb.
the class VcfCadd method doWork.
@Override
public int doWork(final List<String> args) {
try {
if (this.CADD_FLAG_PHRED.equals(CADD_FLAG_SCORE)) {
LOG.error("tag phred same as tag score");
return -1;
}
if (this.ccaduri == null || !this.ccaduri.endsWith(".gz")) {
LOG.error("CCAD uri should end with gz. got " + this.ccaduri);
return -1;
}
LOG.info("Loading index for " + this.ccaduri + ". Please wait...");
this.tabix = new TabixFileReader(this.ccaduri);
this.convertToCaddContigs = ContigNameConverter.fromContigSet(this.tabix.getChromosomes());
LOG.info("End loading index");
for (; ; ) {
final String head = this.tabix.readLine();
if (!head.startsWith("#"))
break;
if (head.startsWith("#Chrom\tPos\tRef") || head.startsWith("#Chr\tPos\tRef")) {
this.headerLabel = Arrays.asList(TAB.split(head));
break;
}
}
if (this.headerLabel == null || this.headerLabel.isEmpty()) {
LOG.error("Cannot read tabix file header");
return -1;
}
this.headerLabel2column = new HashMap<>(this.headerLabel.size());
for (int i = 0; i < this.headerLabel.size(); i++) {
this.headerLabel2column.put(this.headerLabel.get(i), i);
}
Integer col = this.headerLabel2column.get("#Chrom");
if (col == null)
col = this.headerLabel2column.get("#Chr");
if (col == null || col.intValue() != 0) {
LOG.error("Illegal Header: Cannot get column index of #Chrom at 0");
return -1;
}
col = this.headerLabel2column.get("Pos");
if (col == null || col.intValue() != 1) {
LOG.error("Illegal Header: Cannot get column index of 'Pos' at 1");
return -1;
}
col = this.headerLabel2column.get("Ref");
if (col == null || col.intValue() != 2) {
LOG.error("Illegal Header: Cannot get column index of 'Ref' at 2");
return -1;
}
col = this.headerLabel2column.get("Alt");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'Alt'");
return -1;
}
this.column_index_for_Alt = col.intValue();
col = this.headerLabel2column.get("RawScore");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'RawScore'");
return -1;
}
this.column_index_for_RawScore = col.intValue();
col = this.headerLabel2column.get("PHRED");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'PHRED'");
return -1;
}
this.column_index_for_PHRED = col.intValue();
for (final String field : this.otherFieldsStr.split("[ \t,;]+")) {
if (StringUtil.isBlank(field))
continue;
col = this.headerLabel2column.get(field);
if (col == null) {
LOG.error("Illegal Header: Cannot find user's field " + field + "in " + this.headerLabel);
return -1;
}
this.userFields.add(field);
}
return doVcfToVcfPath(args, this.writingVariantsDelegate, this.outputFile);
} catch (final Throwable err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(tabix);
tabix = null;
}
}
use of com.github.lindenb.jvarkit.util.tabix.TabixFileReader in project jvarkit by lindenb.
the class SwingIndexCov method buildDefaultDict.
private static SAMSequenceDictionary buildDefaultDict(final Component owner, final String uri) {
LOG.warn("building default dict for " + uri);
final Map<String, Integer> contig2len = new LinkedHashMap<>();
try (TabixFileReader tbr = new TabixFileReader(uri)) {
for (; ; ) {
final String line = tbr.readLine();
if (line == null)
break;
if (line.startsWith("#"))
continue;
final String[] tokens = CharSplitter.TAB.split(line, 4);
final String ctg = tokens[0];
final int len = Math.max(contig2len.getOrDefault(ctg, 0), Integer.parseInt(tokens[2]));
contig2len.put(ctg, len);
}
return new SAMSequenceDictionary(contig2len.entrySet().stream().map(KV -> new SAMSequenceRecord(KV.getKey(), KV.getValue())).collect(Collectors.toList()));
} catch (final IOException err) {
throw new RuntimeIOException(err);
}
}
use of com.github.lindenb.jvarkit.util.tabix.TabixFileReader in project jvarkit by lindenb.
the class VcfCadd method beforeVcf.
@Override
protected int beforeVcf() {
try {
if (this.CADD_FLAG_PHRED.equals(CADD_FLAG_SCORE)) {
LOG.error("tag phred same as tag score");
return -1;
}
if (StringUtils.isBlank(this.ccaduri) || !this.ccaduri.endsWith(".gz")) {
LOG.error("CCAD uri should end with gz. got " + this.ccaduri);
return -1;
}
this.tabix = new TabixFileReader(this.ccaduri);
this.convertToCaddContigs = ContigNameConverter.fromContigSet(this.tabix.getChromosomes());
for (; ; ) {
final String head = this.tabix.readLine();
if (!head.startsWith("#"))
break;
if (head.startsWith("#Chrom\tPos\tRef") || head.startsWith("#Chr\tPos\tRef")) {
this.headerLabel = Arrays.asList(TAB.split(head));
break;
}
}
if (this.headerLabel == null || this.headerLabel.isEmpty()) {
LOG.error("Cannot read tabix file header");
return -1;
}
this.headerLabel2column = new HashMap<>(this.headerLabel.size());
for (int i = 0; i < this.headerLabel.size(); i++) {
this.headerLabel2column.put(this.headerLabel.get(i), i);
}
Integer col = this.headerLabel2column.get("#Chrom");
if (col == null)
col = this.headerLabel2column.get("#Chr");
if (col == null || col.intValue() != 0) {
LOG.error("Illegal Header: Cannot get column index of #Chrom at 0");
return -1;
}
col = this.headerLabel2column.get("Pos");
if (col == null || col.intValue() != 1) {
LOG.error("Illegal Header: Cannot get column index of 'Pos' at 1");
return -1;
}
col = this.headerLabel2column.get("Ref");
if (col == null || col.intValue() != 2) {
LOG.error("Illegal Header: Cannot get column index of 'Ref' at 2");
return -1;
}
col = this.headerLabel2column.get("Alt");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'Alt'");
return -1;
}
this.column_index_for_Alt = col.intValue();
col = this.headerLabel2column.get("RawScore");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'RawScore'");
return -1;
}
this.column_index_for_RawScore = col.intValue();
col = this.headerLabel2column.get("PHRED");
if (col == null) {
LOG.error("Illegal Header: Cannot get column index of 'PHRED'");
return -1;
}
this.column_index_for_PHRED = col.intValue();
for (final String field : this.otherFieldsStr.split("[ \t,;]+")) {
if (StringUtil.isBlank(field))
continue;
col = this.headerLabel2column.get(field);
if (col == null) {
LOG.error("Illegal Header: Cannot find user's field " + field + "in " + this.headerLabel);
return -1;
}
this.userFields.add(field);
}
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
}
Aggregations