/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.awt.Desktop; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @author Devin */ public class findSeqs extends Application { private Desktop desktop = Desktop.getDesktop(); /** * This method was written by CC and modified by DJC, based upon code from StackOverflow user Kip. * It takes a string and writes it to a specified file, appending said file. * The purpose of this method is to save information and results as they are generated, reducing memory load. * * @param content The string to be written * @param file The destination file for the string output */ private void writeFile(String content, File file){ try (FileWriter fw = new FileWriter(file, true)) { fw.write(content);// } catch (IOException ex) { Logger.getLogger(findSeqs.class.getName()).log(Level.SEVERE, null, ex); } } /** * A timestamp, used for generating unique file IDs */ final long timeUnique = System.currentTimeMillis(); @Override public void start(Stage primaryStage) { final GridPane inputGridPane = new GridPane(); final ComboBox comboBox1; comboBox1 = new ComboBox(); comboBox1.getItems().addAll("nuccore", "protein", "gene"); comboBox1.setMinWidth(100); comboBox1.setMinHeight(25); comboBox1.setValue("protein"); HBox comboBoxSection1; comboBoxSection1 = new HBox(); comboBoxSection1.setSpacing(10); comboBoxSection1.getChildren().addAll(comboBox1); TextField txtField1; txtField1 = new TextField(); txtField1.setText("esearch.fcgi? term"); txtField1.setMinWidth(200); TextField txtField2; txtField2 = new TextField(); txtField2.setText("number to retrieve"); txtField2.setMinWidth(200); Button btn = new Button(); btn.setText("Grab sequences"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e2) { System.out.println("Hello World!"); System.out.println(txtField1.getText()); /** * E-utility URL, built from nucleotide accession, start, and stop values. */ URL url; ArrayList retRecords = new ArrayList(); try { // get URL content String a = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=" + comboBox1.getValue() + "&term=" + txtField1.getText() + "&retmax=" + txtField2.getText(); System.out.println("URL: " + a); url = new URL(a); URLConnection conn = url.openConnection(); // open the stream and put it into BufferedReader BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = br.readLine()) != null) { if(line.contains("")) { line = line.replaceAll("", ""); line = line.replaceAll("", ""); System.out.println(line); retRecords.add(line); } } br.close(); System.out.println("Done"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("First Block completed"); String b = ""; for (int i = 0; i < retRecords.size(); i++) { b += retRecords.get(i); if (i + 1 < retRecords.size()) { b+= ","; } } File file1 = new File("output.txt"); writeFile(b, file1); } }); GridPane.setConstraints(txtField1, 0, 0); GridPane.setConstraints(txtField2, 0, 1); GridPane.setConstraints(comboBox1, 0, 2); GridPane.setConstraints(btn, 0, 3); inputGridPane.setHgap(50); inputGridPane.setVgap(50); inputGridPane.getChildren().addAll(txtField1, txtField2, comboBox1, btn); StackPane root = new StackPane(); root.getChildren().add(inputGridPane); Scene scene = new Scene(root, 200, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }