Skip to content

Commit

Permalink
Add sessionDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
dariober committed Aug 4, 2024
1 parent 5d34b56 commit b1bac93
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ New in 1.19.0

* `sessionList`

* `sessionDelete`

* Move ASCIIGenome files and settings to `~/.asciigenome` directory.

* `print` command does not decode the URL escapes for line feed and carriage
Expand Down
1 change: 1 addition & 0 deletions src/main/java/commandHelp/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum Command {
sessionOpen("sessionOpen"),
sessionSave("sessionSave"),
sessionList("sessionList"),
sessionDelete("sessionDelete"),
reload("reload"),
recentlyOpened("recentlyOpened"),
dropTracks("dropTracks"),
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/commandHelp/CommandList.java
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,19 @@ public static final List<CommandHelp> commandHelpList()
+ "* :code:`-n` List up to this many sessions. Default 10");
cmdList.add(cmd);

cmd = new CommandHelp();
cmd.setName("sessionDelete");
cmd.setArgs("[-f session.yml] <sessionName>");
cmd.inSection = Section.SESSION;
cmd.setBriefDescription("Delete session by name.");
cmd.setAdditionalDescription(
"\n"
+ "* :code:`-f` List sessions in this file. Default: "
+ " :code:`\\~/.asciigenome/session.yaml`\n"
+ "\n"
+ "* :code:`sessionName` Session name to delete");
cmdList.add(cmd);

cmd = new CommandHelp();
cmd.setName("reload");
cmd.setArgs("[track_regex = .*]...");
Expand Down Expand Up @@ -1774,6 +1787,7 @@ public static final List<String> cmds() {
paramList.add("sessionOpen");
paramList.add("sessionSave");
paramList.add("sessionList");
paramList.add("sessionDelete");
paramList.add("reload");
paramList.add("recentlyOpened");
paramList.add("dropTracks");
Expand Down
61 changes: 60 additions & 1 deletion src/main/java/samTextViewer/InteractiveInput.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package samTextViewer;

import static session.SessionHandler.writeSessions;

import colouring.Config;
import colouring.ConfigKey;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -180,7 +182,14 @@ protected TrackProcessor processInput(String cmdConcatInput, TrackProcessor proc
System.out.print("\033[0m");
console.clearScreen();
console.flush();
SessionHandler.saveAs(SessionHandler.DEFAULT_SESSION_FILE, proc.toSession(), "");
try {
SessionHandler.saveAs(SessionHandler.DEFAULT_SESSION_FILE, proc.toSession(), "");
} catch (Exception e) {
System.err.println(
"Error saving session to file: " + SessionHandler.DEFAULT_SESSION_FILE);
System.err.println(e.getMessage());
System.exit(1);
}
System.exit(0);

// * These commands change the GenomicCoordinates (navigate) but do not touch the tracks.
Expand Down Expand Up @@ -290,6 +299,11 @@ protected TrackProcessor processInput(String cmdConcatInput, TrackProcessor proc
args.remove(0);
this.setInteractiveInputExitCode(this.listSessions(args, proc));

} else if (cmdTokens.get(0).equals("sessionDelete")) {
List<String> args = new ArrayList<>(cmdTokens);
args.remove(0);
this.setInteractiveInputExitCode(this.deleteSession(args, proc));

} else if (cmdTokens.get(0).equals("setGenome")) {
this.setGenome(cmdTokens, proc);

Expand Down Expand Up @@ -891,6 +905,51 @@ private ExitCode listSessions(List<String> args, TrackProcessor proc)
return ExitCode.CLEAN_NO_FLUSH;
}

private ExitCode deleteSession(List<String> args, TrackProcessor proc)
throws InvalidGenomicCoordsException, IOException, InvalidCommandLineException {
File sessionYamlFile =
new File(
Utils.getArgForParam(
args, "-f", SessionHandler.DEFAULT_SESSION_FILE.getAbsolutePath()));
SessionHandler sh;
if (this.sessionHandler == null) {
try {
sh = new SessionHandler(sessionYamlFile);
} catch (Exception e) {
this.messages.add(
"Failed to process session file '" + sessionYamlFile + "':\n" + e.getMessage());
return ExitCode.ERROR;
}
} else {
sh = this.sessionHandler;
}
if (args.size() == 0) {
this.messages.add("Please provide the name of the session to delete");
return ExitCode.ERROR;
}
if (args.size() > 1) {
this.messages.add("Please provide only one session to delete");
return ExitCode.ERROR;
}
String sessionName = args.get(0);
boolean found = sh.hasSessionName(sessionName);
if (!found) {
this.messages.add(
"No session with name '" + sessionName + "' found in file '" + sessionYamlFile + "'");
return ExitCode.ERROR;
}
boolean deleted = sh.deleteSession(sessionName);
if (deleted) {
System.err.println(
Utils.padEndMultiLine("Session '" + sessionName + "' deleted", proc.getWindowSize()));
} else {
this.messages.add("Failed to delete session '" + sessionName + "'");
return ExitCode.ERROR;
}
writeSessions(sh.getSessions(), sessionYamlFile);
return ExitCode.CLEAN_NO_FLUSH;
}

private void openSession(List<String> args, TrackProcessor proc)
throws IOException, InvalidCommandLineException {
File sessionYamlFile =
Expand Down
43 changes: 32 additions & 11 deletions src/main/java/session/SessionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.google.common.base.Joiner;
import exceptions.SessionException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -75,6 +72,16 @@ private static void addOrReplaceSession(List<Session> sessions, Session session)
sortSessionsByLastRead(sessions);
}

public static void writeSessions(List<Session> sessions, File sessionFile) throws IOException {
YAMLFactory yf = new YAMLFactory();
yf.configure(WRITE_DOC_START_MARKER, false);
ObjectMapper mapper = new ObjectMapper(yf);
FileOutputStream fos = new FileOutputStream(sessionFile);
SequenceWriter sw = mapper.writerWithDefaultPrettyPrinter().writeValues(fos);
sw.write(sessions);
sw.close();
}

public static void saveAs(File sessionFile, Session session, String sessionName)
throws IOException, SessionException {
session.setSessionName(sessionName);
Expand All @@ -83,13 +90,7 @@ public static void saveAs(File sessionFile, Session session, String sessionName)
sessions = read(sessionFile);
}
addOrReplaceSession(sessions, session);
YAMLFactory yf = new YAMLFactory();
yf.configure(WRITE_DOC_START_MARKER, false);
ObjectMapper mapper = new ObjectMapper(yf);
FileOutputStream fos = new FileOutputStream(sessionFile);
SequenceWriter sw = mapper.writerWithDefaultPrettyPrinter().writeValues(fos);
sw.write(sessions);
sw.close();
writeSessions(sessions, sessionFile);
}

private static void sortSessionsByLastRead(List<Session> sessions) {
Expand Down Expand Up @@ -134,6 +135,15 @@ public void setSessionFile(File sessionFile) {
this.sessionFile = sessionFile;
}

public boolean hasSessionName(String sessionName) {
for (Session x : this.getSessions()) {
if (x.getSessionName().equals(sessionName)) {
return true;
}
}
return false;
}

public String print(int upto, boolean mostRecentLast) throws IOException {
List<String> out = new ArrayList<>();
int i = 0;
Expand All @@ -155,4 +165,15 @@ public String print(int upto, boolean mostRecentLast) throws IOException {
out.add("Session file: " + this.getSessionFile());
return Joiner.on("\n").join(out);
}

public boolean deleteSession(String sessionName) {
Session ss;
try {
ss = this.get(sessionName);
} catch (SessionException e) {
return false;
}
this.getSessions().remove(ss);
return true;
}
}
66 changes: 42 additions & 24 deletions src/test/java/samTextViewer/InteractiveInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ public static class ProcessInput {
}

public ProcessInput processInput(InteractiveInput ip, String cmd, TrackProcessor p)
throws SQLException,
InvalidGenomicCoordsException,
InvalidCommandLineException,
IOException,
ClassNotFoundException,
InvalidRecordException {
throws InvalidGenomicCoordsException, IOException {
ByteArrayOutputStream err = new ByteArrayOutputStream();
System.setErr(new PrintStream(err));
ip.processInput(cmd, p);
Expand Down Expand Up @@ -116,6 +111,42 @@ public void canListCurrentSessionNameAndFile()
assertTrue(pi.stderr.contains("Current session: no-fastafile"));
}

@Test
public void canDeleteSession()
throws IOException,
InvalidConfigException,
SQLException,
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);
InteractiveInput ip = new InteractiveInput(new ConsoleReader(), 1);

ProcessInput pi = processInput(ip, "sessionDelete", proc);
pi = processInput(ip, "sessionDelete -f test_data/session.yaml", proc);
assertEquals(ExitCode.ERROR, ip.getInteractiveInputExitCode());
assertEquals("Please provide the name of the session to delete", pi.stderr.trim());

pi = processInput(ip, "sessionDelete", proc);
assertEquals(ExitCode.ERROR, ip.getInteractiveInputExitCode());
assertEquals("Please provide the name of the session to delete", pi.stderr.trim());

File f = new File("tmp.yml");
f.delete();
Files.copy(Paths.get("test_data/session.yaml"), Paths.get("tmp.yml"));

processInput(ip, "sessionDelete -f tmp.yml newSession", proc);
assertEquals(ExitCode.CLEAN_NO_FLUSH, ip.getInteractiveInputExitCode());
String txt = new String(Files.readAllBytes(Paths.get("tmp.yml")));
assertTrue(!txt.contains("newSession"));

processInput(ip, "sessionDelete -f tmp.yml newSession", proc);
assertEquals(ExitCode.ERROR, ip.getInteractiveInputExitCode());
f.delete();
}

@Test
public void canSaveCurrentSessionInPlace()
throws IOException,
Expand All @@ -124,7 +155,6 @@ public void canSaveCurrentSessionInPlace()
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException,
SessionException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);
Expand Down Expand Up @@ -165,7 +195,6 @@ public void canReplaceGenome()
IOException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException,
InvalidConfigException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);
Expand All @@ -187,8 +216,7 @@ public void canOpenSessionWithFileMissing()
SQLException,
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException {
InvalidRecordException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);

Expand All @@ -213,8 +241,7 @@ public void canOpenSessionWithFastaFileMissing()
SQLException,
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException {
InvalidRecordException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);

Expand Down Expand Up @@ -275,8 +302,7 @@ public void canOpenSession()
SQLException,
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException {
InvalidRecordException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);
InteractiveInput ip = new InteractiveInput(new ConsoleReader(), 1);
Expand Down Expand Up @@ -321,7 +347,6 @@ public void canSaveSession()
InvalidGenomicCoordsException,
ClassNotFoundException,
InvalidRecordException,
InvalidCommandLineException,
SessionException {
new Config(null);
TrackProcessor proc = gimmeTrackProcessor("chr7:1001-1800", 80);
Expand All @@ -341,8 +366,7 @@ public void canPrintGenome()
ClassNotFoundException,
InvalidGenomicCoordsException,
InvalidRecordException,
SQLException,
InvalidCommandLineException {
SQLException {
new Config(null);
TrackProcessor proc;
InteractiveInput ip = new InteractiveInput(new ConsoleReader(), 1);
Expand All @@ -358,7 +382,6 @@ public void canMovePositionByColumn()
InvalidRecordException,
ClassNotFoundException,
SQLException,
InvalidCommandLineException,
InvalidConfigException {

new Config(null);
Expand Down Expand Up @@ -422,12 +445,7 @@ public void canMovePositionByColumn()

@Test
public void canPrintHelp()
throws InvalidGenomicCoordsException,
IOException,
ClassNotFoundException,
InvalidRecordException,
SQLException,
InvalidCommandLineException {
throws InvalidGenomicCoordsException, IOException, InvalidRecordException {

InteractiveInput ip = new InteractiveInput(null, 1);

Expand Down
21 changes: 14 additions & 7 deletions src/test/java/session/SessionHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package session;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import colouring.Config;
import exceptions.InvalidColourException;
Expand All @@ -27,11 +26,19 @@
public class SessionHandlerTest {

@Test
public void testGetSessionByNameOrIndex()
throws IOException,
SessionException,
InvalidGenomicCoordsException,
InvalidTrackTypeException {
public void canDeleteSession() throws IOException, SessionException {
InputStream yaml = Files.newInputStream(Paths.get("test_data/session.yaml"));
SessionHandler sh = new SessionHandler(new File("test_data/session.yaml"));
sh.get("newSession");
boolean deleted = sh.deleteSession("newSession");
assertTrue(deleted);

deleted = sh.deleteSession("newSession");
assertFalse(deleted);
}

@Test
public void testGetSessionByNameOrIndex() throws IOException, SessionException {
InputStream yaml = Files.newInputStream(Paths.get("test_data/session.yaml"));
SessionHandler sh = new SessionHandler(new File("test_data/session.yaml"));
assertEquals("no-fastafile", sh.get("1").getSessionName());
Expand Down

0 comments on commit b1bac93

Please sign in to comment.