From 0725af253ee4f4e70e232cfdb673f6ccffbdf2df Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Tue, 20 Mar 2018 16:29:25 +0000 Subject: [PATCH] =?utf8?q?JAL-2909=20recognise=20.bam=20extension=20in=20URL?= =?utf8?q?=20and=20allow=20location=20spec=20as=20suffix=20-=20e.g.=20file:/?= =?utf8?q?//=E2=80=A6mybam.bam#2:121212-150000?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/jalview/io/BamFile.java | 51 +++++++++++++++++++++++++++++++++++++- src/jalview/io/FileLoader.java | 45 ++++++++++++++++++--------------- src/jalview/io/FileParse.java | 3 ++- src/jalview/io/IdentifyFile.java | 5 ++-- 4 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/jalview/io/BamFile.java b/src/jalview/io/BamFile.java index c8bac62..8a04867 100644 --- a/src/jalview/io/BamFile.java +++ b/src/jalview/io/BamFile.java @@ -21,20 +21,24 @@ package jalview.io; import jalview.datamodel.CigarParser; +import jalview.datamodel.Range; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.PrimitiveIterator.OfInt; import java.util.SortedMap; import htsjdk.samtools.SAMRecord; import htsjdk.samtools.SAMRecordIterator; import htsjdk.samtools.SAMSequenceRecord; +import htsjdk.samtools.SamInputResource; import htsjdk.samtools.SamReader; import htsjdk.samtools.SamReaderFactory; import htsjdk.samtools.ValidationStringency; @@ -94,13 +98,24 @@ public class BamFile extends AlignFile public BamFile(FileParse source) throws IOException { super(true, source); + parseSuffix(); final SamReaderFactory factory = SamReaderFactory.makeDefault() .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS) .validationStringency(ValidationStringency.SILENT); // File-based bam - fileReader = factory.open(source.inFile); + if (source.getDataSourceType() == DataSourceType.FILE) + { + fileReader = factory.open(source.inFile); + } + else + { + // locate index ? + String index = source.getDataName() + ".bai"; + fileReader = factory.open(SamInputResource.of(source.getDataName()) + .index(new URL(index))); + } } @Override @@ -187,5 +202,39 @@ public class BamFile extends AlignFile chromosome = chr; start = s; end = e; + suffix = chromosome + ":" + start + "-" + end; + } + + public boolean parseSuffix() + { + if (suffix == null) + { + return false; + } + int csep = suffix.indexOf(":"); + int rsep = suffix.indexOf("-", csep); + if (csep < 0 || rsep < 0 || suffix.length() - rsep <= 1) + { + return false; + } + String chr, p1, p2; + chr = suffix.substring(0, csep); + p1 = suffix.substring(csep + 1, rsep); + p2 = suffix.substring(rsep + 1); + int cstart, cend; + try + { + cstart = Integer.parseInt(p1); + cend = Integer.parseInt(p2); + } catch (Exception e) + { + warningMessage = (warningMessage == null ? "" : warningMessage + "\n") + + "Couldn't parse range from " + suffix; + return false; + } + chromosome = chr; + start = cstart; + end = cend; + return true; } } diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index 96ab8bb..8eb60e5 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -317,28 +317,33 @@ public class FileLoader implements Runnable fa.prepareFileReader(file, protocol, format); source = fa.getAlignFile(); } - - BamFileOptionsChooser bamoptions = new BamFileOptionsChooser( - source); - - // ask the user which bit of the bam they want to load - int confirm = JvOptionPane.showConfirmDialog(null, - bamoptions, - MessageManager.getString("label.bam_file_options"), - JvOptionPane.OK_CANCEL_OPTION, - JvOptionPane.PLAIN_MESSAGE); - - if (confirm == JvOptionPane.CANCEL_OPTION - || confirm == JvOptionPane.CLOSED_OPTION) - { - Desktop.instance.stopLoading(); - return; - } - else + if (!((BamFile) source).parseSuffix()) { - bamoptions.update(source); - al = fa.readFile(file, protocol, format); + // configure a window + BamFileOptionsChooser bamoptions = new BamFileOptionsChooser( + source); + // ask the user which bit of the bam they want to load + int confirm = JvOptionPane.showConfirmDialog(null, bamoptions, + MessageManager.getString("label.bam_file_options"), + JvOptionPane.OK_CANCEL_OPTION, + JvOptionPane.PLAIN_MESSAGE); + + if (confirm == JvOptionPane.CANCEL_OPTION + || confirm == JvOptionPane.CLOSED_OPTION) + { + Desktop.instance.stopLoading(); + return; + } + else + { + bamoptions.update(source); + if (file.indexOf("#") == -1) + { + file = file + "#" + ((BamFile) source).suffix; + } + } } + al = fa.readFile(file, protocol, format); } if (FileFormat.Jalview.equals(format)) diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index c0328d5..f077e21 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -344,7 +344,8 @@ public class FileParse checkURLSource(fileStr); if (suffixSeparator == '#') { - extractSuffix(fileStr); // URL lref is stored for later reference. + dataName = extractSuffix(fileStr); // URL lref is stored for later + // reference. } } catch (IOException e) { diff --git a/src/jalview/io/IdentifyFile.java b/src/jalview/io/IdentifyFile.java index 38414e3..e65101d 100755 --- a/src/jalview/io/IdentifyFile.java +++ b/src/jalview/io/IdentifyFile.java @@ -127,9 +127,10 @@ public class IdentifyFile { // jar files are special - since they contain all sorts of random // characters. - if (source.inFile != null) + if (source.inFile != null || source.getDataName() != null) { - String fileStr = source.inFile.getName(); + String fileStr = source.inFile == null ? source.getDataName() + : source.inFile.getName(); // possibly a Jalview archive. if (fileStr.lastIndexOf(".jar") > -1 || fileStr.lastIndexOf(".zip") > -1) -- 1.7.10.2