Search in sources :

Example 81 with ContextMenu

use of javafx.scene.control.ContextMenu in project jvarkit by lindenb.

the class BamStage method makeRecordTable.

private TableView<SAMRecord> makeRecordTable() {
    final Font font1 = new Font("Courier", 9);
    /* get max SEQ length displayed */
    int prefnum = 0;
    try {
        prefnum = Integer.parseInt(this.owner.preferences.get(this.owner.pref_bam_max_seq_length_displayed.key, "500"));
    } catch (final NumberFormatException err) {
        prefnum = 500;
    }
    final int max_seq_length = prefnum;
    /* get MAX number of items in cigar */
    try {
        prefnum = Integer.parseInt(this.owner.preferences.get(this.owner.pref_bam_max_cigar_items_displayed.key, "50"));
    } catch (final NumberFormatException err) {
        prefnum = 50;
    }
    final int max_items_in_cigar = prefnum;
    /* utility function to make column specific of paired data */
    final Function<TableColumn<SAMRecord, ?>, TableColumn<SAMRecord, ?>> insertPairdColumn = TC -> {
        this.pairedEndColumns.add(TC);
        return TC;
    };
    final TableView<SAMRecord> table = new TableView<SAMRecord>();
    table.getColumns().add(makeColumn("Read-Name", REC -> REC.getReadName()));
    table.getColumns().add(makeColumn("Flag", REC -> REC.getFlags()));
    table.getColumns().add(makeColumn("Ref", REC -> REC.getReferenceName()));
    table.getColumns().add(formatIntegerColumn(makeColumn("UStart", REC -> {
        if (REC.getReadUnmappedFlag() || REC.getCigar() == null)
            return null;
        final int upos = REC.getUnclippedStart();
        if (upos == REC.getAlignmentStart())
            return null;
        return upos;
    })));
    table.getColumns().add(formatIntegerColumn(makeColumn("Start", REC -> {
        final int pos = REC.getAlignmentStart();
        if (pos == SAMRecord.NO_ALIGNMENT_START)
            return null;
        return pos;
    })));
    table.getColumns().add(formatIntegerColumn(makeColumn("End", REC -> {
        final int pos = REC.getAlignmentEnd();
        if (pos == SAMRecord.NO_ALIGNMENT_START)
            return null;
        return pos;
    })));
    table.getColumns().add(formatIntegerColumn(makeColumn("UEnd", REC -> {
        if (REC.getReadUnmappedFlag() || REC.getCigar() == null)
            return null;
        final int upos = REC.getUnclippedEnd();
        if (upos == REC.getAlignmentEnd())
            return null;
        return upos;
    })));
    table.getColumns().add(makeColumn("MAPQ", REC -> REC.getMappingQuality()));
    TableColumn<SAMRecord, Cigar> tc0 = makeColumn("CIGAR", REC -> REC.getCigar());
    tc0.setCellFactory(tv -> new TableCell<SAMRecord, Cigar>() {

        final TextFlow textFlow = new TextFlow();

        @Override
        protected void updateItem(final Cigar cigar, boolean empty) {
            super.updateItem(cigar, empty);
            setText(null);
            setTextOverrun(OverrunStyle.CLIP);
            if (cigar == null || cigar.isEmpty()) {
                // setText(null);
                setGraphic(null);
                return;
            }
            if (cigar.numCigarElements() >= max_items_in_cigar) {
                setGraphic(null);
                setText(" num(items) > " + max_items_in_cigar);
                return;
            }
            final List<Text> L = new ArrayList<>(cigar.numCigarElements());
            for (final CigarElement ce : cigar.getCigarElements()) {
                final Text txt = new Text(String.valueOf(ce.getLength()) + (char) CigarOperator.enumToCharacter(ce.getOperator()));
                txt.setFont(font1);
                switch(ce.getOperator()) {
                    case H:
                    case S:
                        txt.setStroke(Color.ORANGE);
                        break;
                    case X:
                    case N:
                    case D:
                    case I:
                        txt.setStroke(Color.RED);
                        break;
                    case M:
                        txt.setStroke(Color.BLUE);
                        break;
                    case EQ:
                        txt.setStroke(Color.GREEN);
                        break;
                    case P:
                        txt.setStroke(Color.GRAY);
                        break;
                    default:
                        txt.setStroke(Color.BLACK);
                        break;
                }
                L.add(txt);
            }
            this.textFlow.setLineSpacing(0.1);
            this.textFlow.setMaxHeight(10);
            this.textFlow.setPrefHeight(10);
            this.textFlow.getChildren().setAll(L);
            this.setGraphic(textFlow);
        }
    });
    table.getColumns().add(tc0);
    table.getColumns().add(insertPairdColumn.apply(formatIntegerColumn(makeColumn("LEN", REC -> !REC.getReadPairedFlag() || REC.getMateUnmappedFlag() ? null : REC.getInferredInsertSize()))));
    table.getColumns().add(insertPairdColumn.apply(makeColumn("Mate-Ref", REC -> !REC.getReadPairedFlag() || REC.getMateUnmappedFlag() ? null : REC.getMateReferenceName())));
    table.getColumns().add(insertPairdColumn.apply(formatIntegerColumn(makeColumn("Mate-Pos", REC -> !REC.getReadPairedFlag() || REC.getMateUnmappedFlag() ? null : REC.getMateAlignmentStart()))));
    TableColumn<SAMRecord, String> tc = makeColumn("SEQ", REC -> REC.getReadString());
    // http://stackoverflow.com/questions/42187987/
    tc.setCellFactory(tv -> new TableCell<SAMRecord, String>() {

        final TextFlow textFlow = new TextFlow();

        @Override
        protected void updateItem(final String item, boolean empty) {
            super.updateItem(item, empty);
            setText(null);
            setTextOverrun(OverrunStyle.CLIP);
            if (item == null) {
                // setText(null);
                setGraphic(null);
                return;
            }
            if (item.length() >= max_seq_length) {
                setGraphic(null);
                setText("len > " + max_seq_length);
                return;
            }
            final List<Text> L = new ArrayList<>(item.length());
            for (int i = 0; i < item.length(); ++i) {
                final Text txt = new Text(String.valueOf(item.charAt(i)));
                txt.setFont(font1);
                txt.setStroke(JfxNgs.BASE2COLOR.apply(item.charAt(i)));
                L.add(txt);
            }
            this.textFlow.setLineSpacing(0.1);
            this.textFlow.setMaxHeight(10);
            this.textFlow.setPrefHeight(10);
            this.textFlow.getChildren().setAll(L);
            this.setGraphic(textFlow);
        }
    });
    table.getColumns().add(tc);
    tc = makeColumn("QUAL", REC -> REC.getBaseQualityString());
    tc.setCellFactory(tv -> new TableCell<SAMRecord, String>() {

        final TextFlow textFlow = new TextFlow();

        @Override
        protected void updateItem(final String item, boolean empty) {
            super.updateItem(item, empty);
            setTextOverrun(OverrunStyle.CLIP);
            setText(null);
            if (item == null) {
                // setText(null);
                setGraphic(null);
                return;
            }
            if (item.length() >= max_seq_length) {
                setGraphic(null);
                setText("len > " + max_seq_length);
                return;
            }
            final List<Text> L = new ArrayList<>(item.length());
            for (int i = 0; i < item.length(); ++i) {
                char c = item.charAt(i);
                int ch = (int) c;
                double qual = 93.0;
                if (!(ch < 33 || ch > 126)) {
                    qual = SAMUtils.fastqToPhred(c);
                }
                final Text txt = new Text(String.valueOf(c));
                txt.setFont(font1);
                txt.setStroke(Color.gray(qual / 93.0));
                L.add(txt);
            }
            this.textFlow.setLineSpacing(0.1);
            this.textFlow.setMaxHeight(10);
            this.textFlow.setPrefHeight(10);
            this.textFlow.getChildren().setAll(L);
            this.setGraphic(textFlow);
        }
    });
    table.getColumns().add(tc);
    final short SA = SAMTagUtil.getSingleton().makeBinaryTag("SA");
    table.getColumns().add(makeColumn("SA", REC -> REC.getAttribute(SA) == null ? null : "*"));
    table.getColumns().add(makeColumn("NM", REC -> REC.getIntegerAttribute("NM")));
    table.setPlaceholder(new Label("No Read."));
    final ContextMenu ctxMenu = new ContextMenu();
    ctxMenu.getItems().addAll(super.buildItemsForContextMenu());
    table.setContextMenu(ctxMenu);
    return table;
}
Also used : Arrays(java.util.Arrays) VCFHeader(htsjdk.variant.vcf.VCFHeader) CigarOperator(htsjdk.samtools.CigarOperator) ChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.ChartFactory) VariantContextChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.VariantContextChartFactory) ScrollPane(javafx.scene.control.ScrollPane) TabPane(javafx.scene.control.TabPane) Map(java.util.Map) Hershey(com.github.lindenb.jvarkit.util.Hershey) CigarOpPerPositionChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.CigarOpPerPositionChartFactory) ScriptException(javax.script.ScriptException) CloserUtil(htsjdk.samtools.util.CloserUtil) Rectangle2D(javafx.geometry.Rectangle2D) SplitPane(javafx.scene.control.SplitPane) SAMTagUtil(htsjdk.samtools.SAMTagUtil) GraphicsContext(javafx.scene.canvas.GraphicsContext) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) Event(javafx.event.Event) Set(java.util.Set) SAMFileWriter(htsjdk.samtools.SAMFileWriter) Screen(javafx.stage.Screen) SAMTagAndValue(htsjdk.samtools.SAMRecord.SAMTagAndValue) FastqRecord(htsjdk.samtools.fastq.FastqRecord) Platform(javafx.application.Platform) Separator(javafx.scene.control.Separator) Stream(java.util.stream.Stream) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) FlowPane(javafx.scene.layout.FlowPane) SAMFlag(htsjdk.samtools.SAMFlag) BorderPane(javafx.scene.layout.BorderPane) SamFlagsChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.SamFlagsChartFactory) CloseableIterator(htsjdk.samtools.util.CloseableIterator) FXCollections(javafx.collections.FXCollections) LogCloseableIterator(com.github.lindenb.jvarkit.tools.vcfviewgui.NgsStage.LogCloseableIterator) TextFlow(javafx.scene.text.TextFlow) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Color(javafx.scene.paint.Color) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) StringWriter(java.io.StringWriter) CheckBox(javafx.scene.control.CheckBox) IOException(java.io.IOException) File(java.io.File) Menu(javafx.scene.control.Menu) FileChooser(javafx.stage.FileChooser) SamInputResource(htsjdk.samtools.SamInputResource) TreeMap(java.util.TreeMap) Tab(javafx.scene.control.Tab) CompiledScript(javax.script.CompiledScript) ObservableValue(javafx.beans.value.ObservableValue) EventHandler(javafx.event.EventHandler) Button(javafx.scene.control.Button) CigarElement(htsjdk.samtools.CigarElement) CheckMenuItem(javafx.scene.control.CheckMenuItem) OverrunStyle(javafx.scene.control.OverrunStyle) VBox(javafx.scene.layout.VBox) SAMFileHeader(htsjdk.samtools.SAMFileHeader) BasesPerPositionChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.BasesPerPositionChartFactory) ComboBox(javafx.scene.control.ComboBox) AlertType(javafx.scene.control.Alert.AlertType) ContextMenu(javafx.scene.control.ContextMenu) WindowEvent(javafx.stage.WindowEvent) TableView(javafx.scene.control.TableView) ReadLengthChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.ReadLengthChartFactory) Orientation(javafx.geometry.Orientation) Alert(javafx.scene.control.Alert) MenuItem(javafx.scene.control.MenuItem) Predicate(java.util.function.Predicate) Logger(com.github.lindenb.jvarkit.util.log.Logger) Font(javafx.scene.text.Font) Chart(javafx.scene.chart.Chart) Collectors(java.util.stream.Collectors) SAMRecord(htsjdk.samtools.SAMRecord) SeparatorMenuItem(javafx.scene.control.SeparatorMenuItem) Text(javafx.scene.text.Text) SimpleBindings(javax.script.SimpleBindings) QualityPerPositionChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.QualityPerPositionChartFactory) List(java.util.List) SAMProgramRecord(htsjdk.samtools.SAMProgramRecord) Optional(java.util.Optional) VariantContext(htsjdk.variant.variantcontext.VariantContext) Pattern(java.util.regex.Pattern) SamReaderFactory(htsjdk.samtools.SamReaderFactory) FastqReader(htsjdk.samtools.fastq.FastqReader) ReadQualityChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.ReadQualityChartFactory) Cigar(htsjdk.samtools.Cigar) Scene(javafx.scene.Scene) SequenceUtil(htsjdk.samtools.util.SequenceUtil) SAMUtils(htsjdk.samtools.SAMUtils) TextArea(javafx.scene.control.TextArea) ButtonType(javafx.scene.control.ButtonType) HashMap(java.util.HashMap) GCPercentChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.GCPercentChartFactory) MapqChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.MapqChartFactory) Function(java.util.function.Function) ValidationStringency(htsjdk.samtools.ValidationStringency) SAMTextHeaderCodec(htsjdk.samtools.SAMTextHeaderCodec) TableColumn(javafx.scene.control.TableColumn) HashSet(java.util.HashSet) Interval(htsjdk.samtools.util.Interval) TableCell(javafx.scene.control.TableCell) Insets(javafx.geometry.Insets) Tooltip(javafx.scene.control.Tooltip) Locatable(htsjdk.samtools.util.Locatable) Label(javafx.scene.control.Label) MenuBar(javafx.scene.control.MenuBar) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) ReadChartFactory(com.github.lindenb.jvarkit.tools.vcfviewgui.chart.ReadChartFactory) SamReader(htsjdk.samtools.SamReader) ScrollEvent(javafx.scene.input.ScrollEvent) ActionEvent(javafx.event.ActionEvent) Stage(javafx.stage.Stage) SpinnerValueFactory(javafx.scene.control.SpinnerValueFactory) ChangeListener(javafx.beans.value.ChangeListener) ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) Collections(java.util.Collections) ScrollBar(javafx.scene.control.ScrollBar) Label(javafx.scene.control.Label) ContextMenu(javafx.scene.control.ContextMenu) Font(javafx.scene.text.Font) TextFlow(javafx.scene.text.TextFlow) ArrayList(java.util.ArrayList) List(java.util.List) TableView(javafx.scene.control.TableView) Text(javafx.scene.text.Text) TableColumn(javafx.scene.control.TableColumn) CigarElement(htsjdk.samtools.CigarElement) Cigar(htsjdk.samtools.Cigar) SAMRecord(htsjdk.samtools.SAMRecord)

Aggregations

ContextMenu (javafx.scene.control.ContextMenu)81 MenuItem (javafx.scene.control.MenuItem)69 ActionEvent (javafx.event.ActionEvent)21 SeparatorMenuItem (javafx.scene.control.SeparatorMenuItem)20 Menu (javafx.scene.control.Menu)18 Label (javafx.scene.control.Label)15 List (java.util.List)13 Collectors (java.util.stream.Collectors)13 File (java.io.File)12 VBox (javafx.scene.layout.VBox)11 ObservableList (javafx.collections.ObservableList)9 ArrayList (java.util.ArrayList)8 EventHandler (javafx.event.EventHandler)8 FXCollections (javafx.collections.FXCollections)7 Optional (java.util.Optional)6 Node (javafx.scene.Node)6 Scene (javafx.scene.Scene)6 Text (javafx.scene.text.Text)6 IOException (java.io.IOException)5 Arrays (java.util.Arrays)5