Skip to content

Commit

Permalink
updates for genomespan ctor and fastasubseq.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreese committed Apr 17, 2015
1 parent a04d003 commit 58a1676
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Binary file modified lib/compgen-cmdline-0.4.1.jar
Binary file not shown.
Binary file modified lib/compgen-common-0.1.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/java/io/compgen/ngsutils/NGSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.compgen.ngsutils.cli.bed.BedToBed3;
import io.compgen.ngsutils.cli.bed.BedToBed6;
import io.compgen.ngsutils.cli.bed.BedToFasta;
import io.compgen.ngsutils.cli.fasta.FastaIndex;
import io.compgen.ngsutils.cli.fasta.FastaSubseq;
import io.compgen.ngsutils.cli.fasta.FastaTag;
import io.compgen.ngsutils.cli.fastq.FastqFilterCli;
import io.compgen.ngsutils.cli.fastq.FastqMerge;
Expand Down Expand Up @@ -72,7 +72,7 @@ public static void main(String[] args) {
.addCommand(FindEvents.class)
.addCommand(JunctionDiffCli.class)
.addCommand(FastaJunctions.class)
.addCommand(FastaIndex.class)
.addCommand(FastaSubseq.class)
.addCommand(PileupCli.class)
.addCommand(RepeatAnnotate.class)
.addCommand(GTFAnnotate.class)
Expand Down
45 changes: 21 additions & 24 deletions src/java/io/compgen/ngsutils/annotation/GenomeSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,25 @@ public class GenomeSpan implements Comparable<GenomeSpan> {
final public int end;
final public Strand strand;

public GenomeSpan(String ref, int start, int end, Strand strand){
public GenomeSpan(String ref, int start, int end, Strand strand) {
super();
this.ref = ref;
this.start = start;
this.end = end;
this.strand = strand;
}

public GenomeSpan(String ref, int start){
this(ref, start, Strand.NONE);
}

public GenomeSpan(String ref, int start, int end){
super();
this.ref = ref;
this.start = start;
this.end = end;
this.strand = Strand.NONE;
this(ref, start, end, Strand.NONE);
}

public GenomeSpan(String ref, int start, Strand strand){
super();
this.ref = ref;
this.start = start;
this.end = start;
this.strand = strand;
}

public GenomeSpan(String ref, int start){
super();
this.ref = ref;
this.start = start;
this.end = start+1; // TODO: Should this be start + 1??? Check how this plays with comparisons.
this.strand = Strand.NONE;
// TODO: Should the end be start + 1??? Check how this plays with comparisons.
this(ref, start, start+1, Strand.NONE);
}

public GenomeSpan clone() {
Expand Down Expand Up @@ -97,6 +86,9 @@ public static GenomeSpan parse(String str, Strand strand, boolean zero) {
return null;
} else if (startend.indexOf('-') == -1) {
int pos = Integer.parseInt(startend);
if (!zero) {
pos = pos - 1; // if this isn't zero based INPUT, then adjust the start position
}
return new GenomeSpan(ref, pos, strand);
} else {
int start = Integer.parseInt(startend.substring(0, startend.indexOf('-')));
Expand Down Expand Up @@ -162,19 +154,23 @@ protected boolean contains(String qref, int qstart, int qend, Strand qstrand, bo
}
return false;
}

@Override
public String toString() {
return toString(false);
}

public String toString(boolean oneBasedStart) {
if (start == end) {
if (strand==Strand.NONE) {
return ref+":"+Integer.toString(start);
return ref+":"+Integer.toString(start + (oneBasedStart ? 1: 0));
}
return ref+strand+":"+Integer.toString(start);
return ref+strand+":"+Integer.toString(start + (oneBasedStart ? 1: 0));
}
if (strand==Strand.NONE) {
return ref+":"+Integer.toString(start)+"-"+Integer.toString(end);
return ref+":"+Integer.toString(start + (oneBasedStart ? 1: 0))+"-"+Integer.toString(end);
}
return ref+strand+":"+Integer.toString(start)+"-"+Integer.toString(end);
return ref+strand+":"+Integer.toString(start + (oneBasedStart ? 1: 0))+"-"+Integer.toString(end);
}

@Override
Expand Down Expand Up @@ -285,4 +281,5 @@ public GenomeSpan getStartPos() {
public GenomeSpan getEndPos() {
return new GenomeSpan(ref, end-1, end, strand);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.io.IOException;
import java.util.List;

@Command(name="faidx", desc="Extract subsequences from an indexed FASTA file", category="fasta")
public class FastaIndex extends AbstractOutputCommand {
@Command(name="fasta-subseq", desc="Extract subsequences from a FASTA file (optionally, indexed)", category="fasta", doc="Note: the start-position in the region should be 1-based")
public class FastaSubseq extends AbstractOutputCommand {

private String filename = null;
private GenomeSpan region = null;
Expand All @@ -31,6 +31,9 @@ public void setArgs(List<String> args) throws CommandArgumentException {
}
filename = args.get(0);
region = GenomeSpan.parse(args.get(1));
if (region.start < 0) {
throw new CommandArgumentException("Invalid region!");
}
}

@Exec
Expand All @@ -39,8 +42,8 @@ public void exec() throws IOException, CommandArgumentException {
throw new CommandArgumentException("Missing/invalid arguments!");
}
FastaReader fasta = FastaReader.open(filename);
System.out.println(">"+region);
String seq = fasta.fetchSequence(region.ref, region.start-1, region.end);
System.out.println(">"+region.toString(true));
String seq = fasta.fetchSequence(region.ref, region.start, region.end);
while (seq.length() > wrap) {
System.out.println(seq.substring(0, wrap));
seq = seq.substring(wrap);
Expand Down
11 changes: 6 additions & 5 deletions src/java/io/compgen/ngsutils/fasta/BasicFastaReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class BasicFastaReader extends FastaReader {
protected final String filename;
private boolean closed = false;
private StringLineReader reader = null;

protected BasicFastaReader(String filename) {
this.filename = filename;
Expand All @@ -29,10 +30,8 @@ public String fetchSequence(String ref, int start, int end) throws IOException {
}
}

while (it.hasNext()) {
// exhaust iterator to close open file
// should probably be something more efficient, but for random access, we should be indexed anyway.
it.next();
if (reader != null) {
reader.close();
}

return seq;
Expand All @@ -48,9 +47,11 @@ public Iterator<FastaRecord> iterator() throws IOException {
if (closed) {
throw new IOException("FastaReader closed");
}

reader = new StringLineReader(filename);

return new Iterator<FastaRecord> () {

StringLineReader reader = new StringLineReader(filename);
Iterator<String> it = null;

FastaRecord nextRecord = null;
Expand Down

0 comments on commit 58a1676

Please sign in to comment.