All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geneweaver.query.ui.InputFilePage Maven / Gradle / Ivy

package org.geneweaver.query.ui;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.geneweaver.query.dao.MappedParameter;
import org.geneweaver.query.dao.QueryBuildRequest;
import org.geneweaver.query.service.InputParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class InputFilePage extends WizardPage {


	private QueryBuildRequest request;
	private static final Logger logger = LoggerFactory.getLogger(InputFilePage.class);
	private CheckboxTableViewer ctv;
	private InputParser parser;
	private PathChoice fchoice;
	
	private static final Map delimiters;
	static {
		delimiters = new LinkedHashMap<>();
		delimiters.put(",", ",");
		delimiters.put("TAB", "\t");
		delimiters.put("WHITESPACE", "\\d+");
		delimiters.put(";", ";");
		delimiters.put(":", ":");
		delimiters.put("/", "/");
	}

	protected InputFilePage(QueryBuildRequest request, String pageName) {
		super(pageName);
		this.request = request;
		this.parser = new InputParser();
		
		setPageComplete(false);
		setTitle("Data Input");
		setImageDescriptor(ImageDescriptor.createFromURL(getClass().getClassLoader().getResource("GW2-logo-blue.png")));
	}

	@Override
	public void createControl(Composite parent) {
		
		Composite main = new Composite(parent, SWT.NONE);
	    main.setLayout(new GridLayout(1, false));
	    
	    Consumer consumer = pnew->{
	    	boolean changedFile = false;
	    	try {
	    		Path orig = request.getInputFile();
	    		changedFile = pnew!=null ? !pnew.equals(orig) : pnew!=orig;
	    		request.setInputFile(pnew);
	    		if (changedFile) {
	    			request.getMappedParameters().clear();
	    		}
    		} finally {
    			update(changedFile, true);
    		}
	    };
		this.fchoice = new PathChoice(main, SWT.NONE, consumer, PathChoice.Type.File);
		fchoice.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fchoice.createControls();
	    
		Composite choicePanel = new Composite(main, SWT.NONE);
		choicePanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

	    // See https://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm
		choicePanel.setLayout(new GridLayout(2, false));

	    Label dlabel = new Label(choicePanel, SWT.LEFT);
	    dlabel.setText("Delimiter");
	    dlabel.setToolTipText("Choose the delimiter for the file. Type 'TAB' for a tab character.");

	    Combo dcombo = new Combo(choicePanel, SWT.READ_ONLY);
	    dcombo.setItems(delimiters.keySet().toArray(new String[delimiters.size()]));
	    dcombo.select(0);
	    dcombo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
	    
	    
	    dcombo.addSelectionListener(new SelectionAdapter() {
	    	public void widgetSelected(SelectionEvent e) {
	    		try {
		    		String label = dcombo.getItems()[dcombo.getSelectionIndex()];
		    		String delim = delimiters.get(label);
		    		request.setDelimiter(delim);
	    		} finally {
	    			update(true);
	    		}
	    	}
	    });

	    createTable(main);
	    
	    setControl(main);
	    update(false);
	}

	private void createTable(Composite main) {
	    this.ctv = CheckboxTableViewer.newCheckList(main, SWT.BORDER);
	    ctv.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
	    ctv.getTable().setToolTipText("Choose all columns on which you later want to match, the others will be ignored.");
	    ctv.setContentProvider(createQueryContentProvider());
	    ctv.setLabelProvider(createQueryLabelProvider());
	    ctv.setInput(request);
	    ctv.addCheckStateListener(new ICheckStateListener() {
			@Override
			public void checkStateChanged(CheckStateChangedEvent event) {
				Object[] eles = ctv.getCheckedElements();
				try {
					if (eles==null || eles.length<1) {
						request.getMappedParameters().clear();
						return;
					}
					List items = new LinkedList<>();
					for (Object item : eles) {
						items.add(parser.createMappedParameter(item.toString()));
					}
					request.overrideMappedParameters(items);
				} finally {
					update(false);
				}
			}
		});
	}

	private ILabelProvider createQueryLabelProvider() {
		return new LabelProvider() {
			@Override
			public String getText(Object label) {
				return label.toString();
			}
		};
	}

	private IStructuredContentProvider createQueryContentProvider() {
		return new IStructuredContentProvider() {

			@Override
			public Object[] getElements(Object inputElement) {
				if (request.getInputNames()==null) return new Object[] {};
				Collection names = request.getInputNames();
				return names.toArray(new Object[names.size()]);
			}
			
		};
	}
	
	protected void update(boolean defaultSelections) {
		update(defaultSelections, false);
	}
	
	protected void update(boolean defaultSelections, boolean updateLabel) {
		try {
			setMessage("Choose the exported data with which we want to search the graph.");
			setErrorMessage(null);
			setPageComplete(false);
			
			boolean noPreviousSelections = request.getMappedParameters()==null || request.getMappedParameters().isEmpty();
			
			if (request.getInputFile()!=null) {
				ctv.getTable().setEnabled(Files.exists(request.getInputFile()));
				parser.parse(request, defaultSelections);
				ctv.refresh();
				
				if (defaultSelections && updateLabel) {
		    		String ftxt = request.getInputFile()==null
			    				? ""
			    				: request.getInputFile().getFileName().toString()+" ("+request.getCount()+" rows)";
		    		fchoice.setFileText(ftxt);
				}
			}
			
			if (defaultSelections && noPreviousSelections) {
				List likelySuspects = request.getMappedParameters().stream()
						                         .map(p->p.getInputName())
						                         .collect(Collectors.toList());
				ctv.setCheckedElements(likelySuspects.toArray(new String[likelySuspects.size()]));
			}
			
			if (request.getInputFile()!=null && request.getInputNames()!=null && request.getInputNames().size()>0) {
				setMessage("Choose the input columns to use in the database query. Tip: If you are trying to match variants choose the rsId column; and/or gene names, choose the gene name.");
				
				if (request.getMappedParameters()!=null && request.getMappedParameters().size()>0) {
					setPageComplete(true);
				}
			}
		} catch (Exception ne) {
			logger.error("Cannot update using file "+request.getInputFile(), ne);
			ne.printStackTrace();
			setErrorMessage(ne.getMessage());
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy