Skip to content

Commit

Permalink
Tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dariober committed May 15, 2024
1 parent 1d8530f commit 0db18ca
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 56 deletions.
13 changes: 13 additions & 0 deletions src/main/java/exceptions/InvalidTrackTypeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package exceptions;

public class InvalidTrackTypeException extends Exception {
public InvalidTrackTypeException() {

}
public InvalidTrackTypeException(String message)
{
super(message);
}
/** */
private static final long serialVersionUID = 1L;
}
12 changes: 12 additions & 0 deletions src/main/java/exceptions/SessionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package exceptions;

public class SessionException extends Exception {
public SessionException() {

}
public SessionException(String message)
{
super(message);
}
private static final long serialVersionUID = 1L;
}
2 changes: 1 addition & 1 deletion src/main/java/samTextViewer/ASCIIGenomeHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void setCommands(List<String> commands) {
this.commands = commands;
}

private void write(File outYaml) throws IOException {
protected void write(File outYaml) throws IOException {
Map<String, Object> asciigenome_history = new HashMap<String, Object>();

// List of commands
Expand Down
39 changes: 19 additions & 20 deletions src/main/java/samTextViewer/InteractiveInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@
import com.google.common.collect.Lists;
import commandHelp.Command;
import commandHelp.CommandList;
import exceptions.InvalidColourException;
import exceptions.InvalidCommandLineException;
import exceptions.InvalidConfigException;
import exceptions.InvalidGenomicCoordsException;
import exceptions.InvalidRecordException;
import exceptions.*;
import htsjdk.samtools.SAMSequenceDictionary;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import jline.console.ConsoleReader;
Expand All @@ -41,6 +33,7 @@ public class InteractiveInput {
private static int debug;
private SAMSequenceDictionary samSeqDict;
private String fasta;
private String messages = ""; // Messages that may be sent from the various methods.

public InteractiveInput(ConsoleReader console, int debug) {
InteractiveInput.debug = debug;
Expand Down Expand Up @@ -89,7 +82,6 @@ protected TrackProcessor processInput(String cmdConcatInput, TrackProcessor proc
this.fasta = proc.getGenomicCoordsHistory().current().getFastaFile();
this.samSeqDict = proc.getGenomicCoordsHistory().current().getSamSeqDict();

String messages = ""; // Messages that may be sent from the various methods.
for (String cmdString : cmdInputChainList) {

List<String> cmdTokens = new Tokenizer(cmdString).tokenize();
Expand Down Expand Up @@ -812,15 +804,22 @@ private void setConfigOpt(List<String> cmdTokens)
}
}

private void openSession(String sessionYamlFile, String session, TrackProcessor proc) throws IOException, InvalidGenomicCoordsException, InvalidCommandLineException {
Session ss = new Session(sessionYamlFile, session, Utils.getTerminalWidth());
GenomicCoords testSeqDict = new GenomicCoords(ss.getRegion(), Utils.getTerminalWidth(), null, null);
List<String> genomeFile = new ArrayList<String>();
genomeFile.add(ss.getGenomeFile());
testSeqDict.setGenome(genomeFile, true);
proc.getGenomicCoordsHistory().setGenome(genomeFile);
proc.getGenomicCoordsHistory()
.add(testSeqDict);
private void openSession(String sessionYamlFile, String session, TrackProcessor proc) throws IOException {
Session ss = null;
try {
ss = new Session(sessionYamlFile, session);
} catch (SessionException e) {
this.messages += e.getMessage();
this.interactiveInputExitCode = ExitCode.ERROR;
return;
}
try {
GenomicCoords gc = ss.getGenome();
proc.getGenomicCoordsHistory().setGenome(Collections.singletonList(gc.getFastaFile()));
proc.getGenomicCoordsHistory().add(gc);
} catch (Exception e) {
this.messages += "Unable to set genome for session " + session;
}
}

private void setGenome(List<String> cmdTokens, TrackProcessor proc)
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/session/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.esotericsoftware.yamlbeans.YamlReader;
import exceptions.InvalidGenomicCoordsException;
import exceptions.InvalidRecordException;
import exceptions.InvalidTrackTypeException;
import samTextViewer.GenomicCoords;
import samTextViewer.Utils;
import tracks.Track;
import tracks.TrackPileup;
import tracks.TrackReads;
Expand All @@ -15,26 +17,36 @@
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import exceptions.SessionException;

public class Session {
private ArrayList<Map<String, Object>> sessionList;
private Map<String, Object> session;

public Session(String sessionYamlFile, String session, int terminalWidth) throws FileNotFoundException, YamlException {
public Session(String sessionYamlFile, String session) throws FileNotFoundException, YamlException, SessionException {
// Read yaml file
// Get genome file if set and parse it into genomicCoords
// For each track populate trackSet
YamlReader reader = new YamlReader(new FileReader(sessionYamlFile));
this.sessionList = (ArrayList<Map<String, Object>>) reader.read();
this.sortListSessionsLastRead(this.sessionList);
this.session = this.sessionList.get(0);
for (Map<String, Object> ss : this.sessionList) {
if (((String) ss.get("name")).equals(session)) {
this.session = ss;
break;
}
}
if (this.session == null) {
throw new SessionException("Session '" + session + "' not found in file " + sessionYamlFile);
}
}

public String getGenomeFile() {
public GenomicCoords getGenome() throws IOException, InvalidGenomicCoordsException {
Map<String, Object> genome = (Map<String, Object>) this.session.get("genome");
return (String) genome.get("file");
return new GenomicCoords((String) genome.get("region"), Utils.getTerminalWidth(),null, (String) genome.get("fasta"));
}
public String getRegion() {

private String getRegion() {
Map<String, Object> genome = (Map<String, Object>) this.session.get("genome");
return (String) genome.get("region");
}
Expand All @@ -43,11 +55,11 @@ private void sortListSessionsLastRead(List<Map<String, Object>> ss) {
ss.sort((o1, o2) -> ((String) o2.get("lastRead")).compareTo((String) o1.get("lastRead")));
}

public TrackSet getTrackSet() throws Exception {
public TrackSet getTrackSet() throws SQLException, InvalidGenomicCoordsException, IOException, InvalidRecordException, ClassNotFoundException, InvalidTrackTypeException {
List<Map<String, Object>> tracks = (List<Map<String, Object>>) this.session.get("tracks");
List<String> tl = new ArrayList<String>();
TrackSet trackSet = new TrackSet(tl, null);
GenomicCoords gc = new GenomicCoords(this.getRegion(), 80, null, null);
GenomicCoords gc = new GenomicCoords(this.getRegion(), Utils.getTerminalWidth(), null, null);
for (Map<String, Object> map : tracks) {
Track tr;
String type = (String) map.get("type");
Expand All @@ -56,7 +68,7 @@ public TrackSet getTrackSet() throws Exception {
} else if (type.equals("TrackReads")) {
tr = new TrackReads((String) map.get("source"), gc);
} else {
throw new Exception("Invalid type: " + type);
throw new InvalidTrackTypeException("Invalid type: " + type);
}
trackSet.addTrack(tr, (String) map.get("name"));
}
Expand Down
25 changes: 18 additions & 7 deletions src/test/java/session/SessionTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package session;

import coloring.Config;
import com.esotericsoftware.yamlbeans.YamlException;
import exceptions.InvalidConfigException;
import exceptions.InvalidGenomicCoordsException;
import exceptions.InvalidRecordException;
import exceptions.SessionException;
import org.junit.Test;
import samTextViewer.GenomicCoords;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;

public class SessionTest {
/** TESTS:
* genome.file not found
* genome.file not valid
* genome.file empty or not present (session doesn't have a genome)
* genome.region Invalid (malformed, extending beyond contig, contig not found in genome.file)
* genome.region empty or not present
*/
@Test
public void canReadSession() throws Exception {
public void canReadValidGenome() throws IOException, InvalidConfigException, SessionException, InvalidGenomicCoordsException {
new Config(null);
Session session = new Session("test_data/session.yaml", "last", 80);
System.err.println(session.getTrackSet().getTrackList());
Session session = new Session("test_data/session.yaml", "S2");
GenomicCoords gc = session.getGenome();
assertEquals((long)gc.getFrom(), 1);
assertEquals((long)gc.getTo(), 10000);
assertEquals(gc.getSamSeqDict().getSequence("chr7").getSequenceName(), "chr7");
}
}
61 changes: 41 additions & 20 deletions test_data/session.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name: mysession,
lastRead: "2024-01-01T20:40:00",
genome: {
file: "test_data/chr7.fa",
fasta: "test_data/chr7.fa",
region: "chr7:1-1000",
},
tracks: [
Expand All @@ -14,25 +14,46 @@
}
]
},
{
name: mysession2,
lastRead: "2024-01-02T20:40:00",
genome: {
file: "test_data/chr7.fa",
region: "chr7:1-10000",
},
tracks: [
{
name: "ds051#1",
source: "test_data/ds051.actb.bam",
type: "TrackPileup",
},
{
name: "ds051#2",
{
name: S2,
lastRead: "2024-01-02T20:40:00",
genome: {
fasta: "test_data/chr7.fa",
region: "chr7:1-10000",
},
tracks: [
{
name: "ds051#1",
source: "test_data/ds051.actb.bam",
type: "TrackReads",
}
type: "TrackPileup",
},
{
name: "ds051#2",
source: "test_data/ds051.actb.bam",
type: "TrackReads",
}

]
}
]
},
{
name: genome-not-found,
lastRead: "2024-01-03T20:40:00",
genome: {
fasta: "notfound.fasta",
region: "chr7:1-10000",
},
tracks: [
{
name: "ds051#1",
source: "test_data/ds051.actb.bam",
type: "TrackPileup",
},
{
name: "ds051#2",
source: "test_data/ds051.actb.bam",
type: "TrackReads",
}

]
}
]

0 comments on commit 0db18ca

Please sign in to comment.