Skip to content

Commit

Permalink
Simplified code
Browse files Browse the repository at this point in the history
(accidentally was on sort-by-ref branch when making documentation, so I rewrote some tools which turned out to be better than the code on this branch)
  • Loading branch information
benjaminbeer256 committed Oct 10, 2023
1 parent 9e250c7 commit 441fd0f
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String validateInput() throws IOException {
}
//set default output filename
if(output==null && !stdout){
String NAME = ExtensionFileFilter.stripExtension(bedFile) + ".gff";
String NAME = ExtensionFileFilter.stripExtensionIgnoreGZ(bedFile) + ".gff";
NAME += gzOutput ? ".gz" : "";
output = new File(NAME);
//check stdout and output not both selected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class SortBEDCLI implements Callable<Integer> {

@Option(names = {"-o", "--output"}, description = "specify output file basename with no .cdt/.bed/.jtv extension (default=<bedFile>_SORT")
private String outputBasename = null;
@Option(names = {"-c", "--center"}, description = "sort by center on the input size of expansion in bins (default=100)")
private int center = -999;
@Option(names = {"-z", "--gzip"}, description = "gzip output (default=false)")
private boolean gzOutput = false;
@Option(names = {"-c", "--center"}, description = "sort by center on the input size of expansion in bins (default=100)")
private int center = -999;
@Option(names = {"-x", "--index"}, description = "sort by index from the specified start to the specified stop (0-indexed and half-open interval)",
arity = "2")
private int[] index = {-999, -999};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ExpandGFFCLI implements Callable<Integer> {

@Option(names = {"-o", "--output"}, description = "specify output filename (name will be same as original with coordinate info appended)")
private File output = null;
@Option(names = {"-s", "--stdout"}, description = "output bed to STDOUT")
@Option(names = {"-s", "--stdout"}, description = "output gff to STDOUT")
private boolean stdout = false;
@Option(names = {"-z", "--gzip"}, description = "gzip output (default=false)")
private boolean gzOutput = false;
Expand Down Expand Up @@ -72,7 +72,7 @@ private String validateInput() throws IOException {
return(r);
}
//check input extensions
if(!"gff".equals(ExtensionFileFilter.getExtension(gffFile))){
if(!"gff".equals(ExtensionFileFilter.getExtensionIgnoreGZ(gffFile))){
r += "(!)Is this a GFF file? Check extension: " + gffFile.getName() + "\n";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private String validateInput() throws IOException {
}
//set default output filename
if(output==null && !stdout){
output = new File(ExtensionFileFilter.stripExtension(gffFile) + ".bed");
output = new File(ExtensionFileFilter.stripExtensionIgnoreGZ(gffFile) + ".bed");
//check stdout and output not both selected
}else if(stdout){
if(output!=null){ r += "(!)Cannot use -s flag with -o.\n"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SortGFFCLI implements Callable<Integer> {

@Option(names = {"-o", "--output"}, description = "specify output file basename with no .cdt/.gff/.jtv extension (default=<gffFile>_SORT")
private String outputBasename = null;
@Option(names = {"-z", "--compression"}, description = "Output compressed GFF file" )
@Option(names = {"-z", "--gzip"}, description = "gzip output (default=false)")
private boolean gzOutput = false;
@Option(names = {"-c", "--center"}, description = "sort by center on the input size of expansion in bins (default=100)")
private int center = -999;
Expand Down Expand Up @@ -93,7 +93,7 @@ private String validateInput() throws IOException {

//set default output filename
if(outputBasename==null){
outputBasename = ExtensionFileFilter.stripExtension(gffFile) + "_SORT";
outputBasename = ExtensionFileFilter.stripExtensionIgnoreGZ(gffFile) + "_SORT";
//check output filename is valid
}else{
//no extension check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ private String validateInput() throws IOException {
if (output == null) {
String NAME = motif + "_" + Integer.toString(ALLOWED_MISMATCH) + "Mismatch_"
+ ExtensionFileFilter.stripExtension(fastaFile) + ".bed";
NAME += gzOutput ? ".gz" : "";
output = new File(NAME);
// check output filename is valid
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package scriptmanager.scripts.Coordinate_Manipulation.BED_Manipulation;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import scriptmanager.util.GZipUtilities;

Expand All @@ -35,19 +29,10 @@ public static void convertBEDtoGFF(File outpath, File input, boolean gzOutput) t
// Initialize output writer
PrintStream OUT = System.out;
if (outpath != null) {
if (gzOutput) {
OUT = new PrintStream(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outpath))));
} else {
OUT = new PrintStream(new BufferedOutputStream(new FileOutputStream(outpath)));
}
OUT = GZipUtilities.makePrintStream(outpath, gzOutput);
}
// Check if file is gzipped and instantiate appropriate BufferedReader
BufferedReader br;
if(GZipUtilities.isGZipped(input)) {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(input)), "UTF-8"));
} else {
br = new BufferedReader(new InputStreamReader(new FileInputStream(input), "UTF-8"));
}
BufferedReader br = GZipUtilities.makeReader(input);
// Initialize line variable to loop through
String line = br.readLine();
while (line != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static void sortBEDbyCDT(String outbase, File bed, File cdt, int START_IN
if (!ID.contains("YORF") && !ID.contains("NAME")) {
BEDFile.put(ID, line);
}
line = br.readLine();
}
br.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import scriptmanager.util.GZipUtilities;

import scriptmanager.util.GZipUtilities;

public class GFFtoBED {
public static void convertGFFtoBED(File out_filepath, File input, boolean outputGzip) throws IOException {
public static void convertGFFtoBED(File out_filepath, File input, boolean gzOutput) throws IOException {
// GFF: chr22 TeleGene enhancer 10000000 10001000 500 + . touch1
// BED: chr12 605113 605120 region_0 0 +

PrintStream OUT = System.out;
if (out_filepath != null)
OUT = GZipUtilities.makePrintStream(out_filepath, outputGzip);
OUT = GZipUtilities.makePrintStream(out_filepath, gzOutput);

// Checks if file is gzipped and instantiate appropriate BufferedReader
String line;
BufferedReader br = GZipUtilities.makeReader(input);
while ((line = br.readLine()) != null) {
// Initialize line variable to loop through
String line = br.readLine();
while (line != null) {
String[] temp = line.split("\t");
if (temp[0].toLowerCase().contains("track") || temp[0].startsWith("#")) {
OUT.println(String.join("\t", temp));
Expand All @@ -42,6 +42,7 @@ public static void convertGFFtoBED(File out_filepath, File input, boolean output
System.out.println("Invalid Coordinate in File!!!\n" + Arrays.toString(temp));
}
}
line = br.readLine();
}
br.close();
OUT.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ public static void sortGFFbyCDT(String outname, File gff, File cdt, int START_IN
ArrayList<GFFCoord> SORT = new ArrayList<GFFCoord>();
HashMap<String, String> CDTFile = new HashMap<String, String>();
String CDTHeader = "";
// Parse CDT File first
String line;
// Check if file is gzipped and instantiate appropriate BufferedReader
BufferedReader br = GZipUtilities.makeReader(cdt);
while ((line = br.readLine()) != null) {
// Parse CDT File first
String line = br.readLine();
while (line != null) {
String[] ID = line.split("\t");
if (!ID[0].contains("YORF") && !ID[0].contains("NAME")) {
double count = 0;
Expand All @@ -32,14 +33,14 @@ public static void sortGFFbyCDT(String outname, File gff, File cdt, int START_IN
} else {
CDTHeader = line;
}
line = br.readLine();
}
br.close();
// Sort by score
Collections.sort(SORT, GFFCoord.ScoreComparator);

// Output sorted CDT File
String newCDT = outname + ".cdt";
PrintStream OUT = GZipUtilities.makePrintStream(new File(newCDT), gzOutput);
PrintStream OUT = GZipUtilities.makePrintStream(new File(outname + ".cdt"), gzOutput);
OUT.println(CDTHeader);
for (int x = 0; x < SORT.size(); x++) {
OUT.println(CDTFile.get(SORT.get(x).getName()));
Expand All @@ -49,18 +50,21 @@ public static void sortGFFbyCDT(String outname, File gff, File cdt, int START_IN

// Match to gff file after
HashMap<String, String> GFFFile = new HashMap<String, String>();
line = null;
// Check if file is gzipped and instantiate appropriate BufferedReader
br = GZipUtilities.makeReader(gff);
while ((line = br.readLine()) != null) {
String ID = line.split("\t")[8].split(";")[0];
// Initialize line variable to loop through
line = br.readLine();
while (line != null) {
String ID = line.split("\t")[3];
if (!ID.contains("YORF") && !ID.contains("NAME")) {
GFFFile.put(ID, line);
}
line = br.readLine();
}
br.close();

// Output sorted GFF File
String newGFF = outname + ".gff";
OUT = GZipUtilities.makePrintStream(new File(newGFF), gzOutput);
OUT = GZipUtilities.makePrintStream(new File(outname + ".gff"), gzOutput);
for (int x = 0; x < SORT.size(); x++) {
OUT.println(GFFFile.get(SORT.get(x).getName()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package scriptmanager.scripts.Sequence_Analysis;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;
Expand All @@ -12,7 +10,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import scriptmanager.util.GZipUtilities;

Expand Down Expand Up @@ -126,12 +123,7 @@ public void run() throws IOException, InterruptedException {
// Initialize output writer
PrintStream OUT = System.out;
if (out_filepath != null) {
if (gzOutput) {
OUT = new PrintStream(
new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(out_filepath))));
} else {
OUT = new PrintStream(new BufferedOutputStream(new FileOutputStream(out_filepath)));
}
OUT = GZipUtilities.makePrintStream(out_filepath, gzOutput);
}

// Check if file is gzipped and instantiate appropriate BufferedReader
Expand Down
1 change: 1 addition & 0 deletions src/main/java/scriptmanager/util/BEDUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;

import scriptmanager.objects.CoordinateObjects.BEDCoord;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ public Void doInBackground() throws IOException {
for (int x = 0; x < BEDFiles.size(); x++) {
File XBED = BEDFiles.get(x);
// Set outfilepath
String OUTPUT = ExtensionFileFilter.stripExtension(XBED) + ".gff";
String OUTPUT = ExtensionFileFilter.stripExtensionIgnoreGZ(XBED) + ".gff";
if (OUT_DIR != null) {
OUTPUT = OUT_DIR + File.separator + OUTPUT;
}
OUTPUT += chckbxGzipOutput.isSelected() ? ".gz" : "";
// Initialize LogItem
String command = BEDtoGFFCLI.getCLIcommand(new File(OUTPUT), XBED, chckbxGzipOutput.isSelected());
LogItem new_li = new LogItem(command);
Expand Down Expand Up @@ -203,8 +202,8 @@ public void actionPerformed(ActionEvent e) {
btnConvert.addActionListener(this);

chckbxGzipOutput = new JCheckBox("Output GZIP");
sl_contentPane.putConstraint(SpringLayout.NORTH, chckbxGzipOutput, 0, SpringLayout.NORTH, btnOutput);
sl_contentPane.putConstraint(SpringLayout.EAST, chckbxGzipOutput, -10, SpringLayout.EAST, contentPane);
sl_contentPane.putConstraint(SpringLayout.NORTH, chckbxGzipOutput, 0, SpringLayout.NORTH, btnConvert);
sl_contentPane.putConstraint(SpringLayout.WEST, chckbxGzipOutput, 36, SpringLayout.WEST, contentPane);
contentPane.add(chckbxGzipOutput);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public Void doInBackground() throws IOException {
STOP_INDEX = Integer.parseInt(txtStop.getText());
}

String OUTPUT = ExtensionFileFilter.stripExtension(txtOutput.getText());
OUTPUT = txtOutput.getText().endsWith(".gz") ? ExtensionFileFilter.stripExtension(OUTPUT) : OUTPUT;
String OUTPUT = ExtensionFileFilter.stripExtensionIgnoreGZ(BED_File);
if (OUT_DIR != null) {
OUTPUT = OUT_DIR.getCanonicalPath() + File.separator + OUTPUT;
}
Expand Down Expand Up @@ -328,11 +327,13 @@ public void actionPerformed(ActionEvent e) {
lblBEDFile.setText(BED_File.getName());
txtOutput.setEnabled(true);
// Set default output filename
String NAME = ExtensionFileFilter.stripExtension(BED_File.getName());
NAME = BED_File.getName().endsWith(".bed.gz") ? ExtensionFileFilter.stripExtension(NAME) : NAME;
NAME += "_SORT.bed";
NAME += chckbxGzipOutput.isSelected() ? ".gz" : "";
txtOutput.setText(NAME);
String sortName = "";
try {
sortName = ExtensionFileFilter.stripExtensionIgnoreGZ(BED_File) + "_SORT.bed";
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Invalid BED");
};
txtOutput.setText(sortName);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public class GFFtoBEDWindow extends JFrame implements ActionListener, PropertyCh
private JButton btnLoad;
private JButton btnRemoveGFF;
private JButton btnConvert;
private JCheckBox chckbxGzipOutput;

private JProgressBar progressBar;
public Task task;
private JLabel lblCurrent;
private JLabel lblDefaultToLocal;
private JButton btnOutput;
private static JCheckBox chckbxGzipOutput;

class Task extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() throws IOException {
setProgress(0);
for (int x = 0; x < BEDFiles.size(); x++) {
File XGFF = BEDFiles.get(x);
// Check if file is gzipped and assigns appropriate file name
// Set outfilepath
String OUTPUT = ExtensionFileFilter.stripExtensionIgnoreGZ(XGFF) + ".bed";
if (OUT_DIR != null) {
OUTPUT = OUT_DIR + File.separator + OUTPUT;
Expand Down Expand Up @@ -172,13 +172,12 @@ public void actionPerformed(ActionEvent e) {
sl_contentPane.putConstraint(SpringLayout.WEST, btnOutput, 150, SpringLayout.WEST, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, btnOutput, -150, SpringLayout.EAST, contentPane);
contentPane.add(btnOutput);
btnConvert.addActionListener(this);

chckbxGzipOutput = new JCheckBox("Output GZIP");
sl_contentPane.putConstraint(SpringLayout.NORTH, chckbxGzipOutput, 0, SpringLayout.NORTH, btnOutput);
sl_contentPane.putConstraint(SpringLayout.EAST, chckbxGzipOutput, -10, SpringLayout.EAST, contentPane);
sl_contentPane.putConstraint(SpringLayout.NORTH, chckbxGzipOutput, 0, SpringLayout.NORTH, btnConvert);
sl_contentPane.putConstraint(SpringLayout.WEST, chckbxGzipOutput, 36, SpringLayout.WEST, contentPane);
contentPane.add(chckbxGzipOutput);

btnConvert.addActionListener(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Void doInBackground() throws IOException {
STOP_INDEX = Integer.parseInt(txtStop.getText());
}

String OUTPUT = txtOutput.getText();
String OUTPUT = ExtensionFileFilter.stripExtensionIgnoreGZ(GFF_File);
if (OUT_DIR != null) {
OUTPUT = OUT_DIR.getCanonicalPath() + File.separator + OUTPUT;
}
Expand Down Expand Up @@ -172,6 +172,22 @@ public void actionPerformed(ActionEvent e) {
sl_contentPane.putConstraint(SpringLayout.EAST, chckbxGzipOutput, -10, SpringLayout.EAST, contentPane);
contentPane.add(chckbxGzipOutput);

chckbxGzipOutput.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String NAME = txtOutput.getText();
if (chckbxGzipOutput.isSelected()) {
if (!NAME.endsWith(".gz")) {
txtOutput.setText(NAME + ".gz");
}
} else {
if (NAME.endsWith(".gz")) {
NAME = ExtensionFileFilter.stripExtension(NAME);
txtOutput.setText(NAME);
}
}
}
});

rdbtnSortbyCenter = new JRadioButton("Sort by Center");
sl_contentPane.putConstraint(SpringLayout.NORTH, rdbtnSortbyCenter, 129, SpringLayout.NORTH, contentPane);
contentPane.add(rdbtnSortbyCenter);
Expand Down Expand Up @@ -293,11 +309,12 @@ public void actionPerformed(ActionEvent e) {
GFF_File = newGFFFile;
lblGFFFile.setText(GFF_File.getName());
txtOutput.setEnabled(true);
// Set default output filename
String sortName = "";
try {
sortName = ExtensionFileFilter.stripExtensionIgnoreGZ(GFF_File) + "_SORT";
sortName = ExtensionFileFilter.stripExtensionIgnoreGZ(GFF_File) + "_SORT.gff";
} catch (IOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Invalid GFF");
}
txtOutput.setText(sortName);
}
Expand Down
Loading

0 comments on commit 441fd0f

Please sign in to comment.