Skip to content

Commit b69ddfb

Browse files
committed
Setter for chromosome coordinate systems: 0-based, 1-based
1 parent e5813e8 commit b69ddfb

2 files changed

Lines changed: 23 additions & 27 deletions

File tree

biojava-genome/src/main/java/org/biojava/nbio/genome/util/ChromosomeMappingTools.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public class ChromosomeMappingTools {
2828

2929
public static final String CHROMOSOME = "CHROMOSOME";
3030
public static final String CDS = "CDS";
31-
31+
32+
public static int base = 1;
33+
public static void setCoordinateSystem(int baseInt) {
34+
base = baseInt;
35+
}
3236

3337
/** Pretty print the details of a GeneChromosomePosition to a String
3438
*
@@ -202,25 +206,12 @@ private static String formatExonStructureReverse(GeneChromosomePosition chromPos
202206
*/
203207
public static int getCDSLength(GeneChromosomePosition chromPos) {
204208

205-
logger.debug(chromPos.toString());
206-
207-
logger.debug("chromosomal information: ");
208-
209-
logger.debug("Gene:" + chromPos.getGeneName());
210-
logger.debug(" Transcription (including UTRs): " + chromPos.getTranscriptionStart() + " - " + chromPos.getTranscriptionEnd() + " (length:" + (chromPos.getTranscriptionEnd() - chromPos.getTranscriptionStart()) + ")");
211-
logger.debug(" Orientation: " + chromPos.getOrientation());
212-
logger.debug(" CDS: " + (chromPos.getCdsStart()) + " - " + chromPos.getCdsEnd() + " (length: " + (chromPos.getCdsEnd() - chromPos.getCdsStart()) + ")");
213-
214-
215209
List<Integer> exonStarts = chromPos.getExonStarts();
216210
List<Integer> exonEnds = chromPos.getExonEnds();
217211

218-
logger.debug("Exons:" + exonStarts.size());
219-
220212
int cdsStart = chromPos.getCdsStart();
221213
int cdsEnd = chromPos.getCdsEnd();
222214

223-
224215
int codingLength;
225216
if (chromPos.getOrientation().equals('+'))
226217
codingLength = ChromosomeMappingTools.getCDSLengthForward(exonStarts, exonEnds, cdsStart, cdsEnd);
@@ -511,6 +502,7 @@ public static int getCDSLengthReverse(List<Integer> exonStarts, List<Integer> ex
511502
cdsEnd = cdsStart;
512503
cdsStart = tmp;
513504
}
505+
cdsStart = cdsStart + base;
514506

515507
// map reverse
516508
for (int i = exonStarts.size() - 1; i >= 0; i--) {
@@ -523,6 +515,7 @@ public static int getCDSLengthReverse(List<Integer> exonStarts, List<Integer> ex
523515
end = start;
524516
start = tmp;
525517
}
518+
start = start + base;
526519

527520
if ((start < cdsStart && end < cdsStart) || (start > cdsEnd && end > cdsEnd))
528521
continue;
@@ -535,7 +528,7 @@ public static int getCDSLengthReverse(List<Integer> exonStarts, List<Integer> ex
535528

536529
codingLength += (end - start + 1);
537530
}
538-
return codingLength;
531+
return codingLength - 3;
539532
}
540533

541534
/**
@@ -553,21 +546,21 @@ public static int getCDSLengthForward(List<Integer> exonStarts, List<Integer> ex
553546

554547
for (int i = 0; i < exonStarts.size(); i++) {
555548

556-
int start = exonStarts.get(i);
549+
int start = exonStarts.get(i)+base;
557550
int end = exonEnds.get(i);
558551

559-
if ( (start < cdsStart && end < cdsStart) || (start > cdsEnd && end > cdsEnd) )
552+
if ( (start < cdsStart+base && end < cdsStart) || (start > cdsEnd && end > cdsEnd) )
560553
continue;
561554

562-
if (start < cdsStart)
563-
start = cdsStart;
555+
if (start < cdsStart+base)
556+
start = cdsStart+base;
564557

565558
if (end > cdsEnd)
566559
end = cdsEnd;
567560

568561
codingLength += (end - start + 1);
569562
}
570-
return codingLength;
563+
return codingLength - 3;
571564
}
572565

573566
/** Extracts the exon boundaries in CDS coordinates. (needs to be divided by 3 to get AA positions)
@@ -810,7 +803,7 @@ public static int getCDSPosForward(int chromPos, List<Integer> exonStarts, List<
810803
int cdsStart, int cdsEnd) {
811804

812805
// the genetic coordinate is not in a coding region
813-
if ( (chromPos < (cdsStart+1) ) || ( chromPos > (cdsEnd+1) ) ) {
806+
if ( (chromPos < (cdsStart+base) ) || ( chromPos > (cdsEnd+base) ) ) {
814807
logger.debug("The "+format(chromPos)+" position is not in a coding region");
815808
return -1;
816809
}
@@ -829,7 +822,7 @@ public static int getCDSPosForward(int chromPos, List<Integer> exonStarts, List<
829822

830823
lengthExon = end - start;
831824

832-
if (start+1 <= chromPos && end >= chromPos ) {
825+
if (start+base <= chromPos && end >= chromPos ) {
833826
return codingLength + (chromPos-start);
834827
}
835828
else {
@@ -856,7 +849,7 @@ public static int getCDSPosReverse(int chromPos, List<Integer> exonStarts, List<
856849
int cdsStart, int cdsEnd) {
857850

858851
// the genetic coordinate is not in a coding region
859-
if ( (chromPos < (cdsStart+1)) || ( chromPos > (cdsEnd+1) ) ) {
852+
if ( (chromPos < (cdsStart+base)) || ( chromPos > (cdsEnd+base) ) ) {
860853
logger.debug("The "+format(chromPos)+" position is not in a coding region");
861854
return -1;
862855
}
@@ -875,7 +868,7 @@ public static int getCDSPosReverse(int chromPos, List<Integer> exonStarts, List<
875868

876869
lengthExon = end - start;
877870
// +1 offset to be a base 1
878-
if (start+1 <= chromPos && end >= chromPos ) {
871+
if (start+base <= chromPos && end >= chromPos ) {
879872
return codingLength + (end-chromPos+1);
880873
}
881874
else {

biojava-genome/src/test/java/org/biojava/nbio/genome/TestChromosomeMappingTools.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public void testGetCDSLengthForward() {
2222
int cdsStart = 35;
2323
int cdsEnd = 75;
2424

25-
int cdsDesired = 23;
25+
int cdsDesired = 23 - 3;
26+
ChromosomeMappingTools.setCoordinateSystem(0);
2627
int cdsTest = ChromosomeMappingTools.getCDSLengthForward(exonStarts, exonEnds, cdsStart, cdsEnd);
2728

2829
assertEquals(cdsDesired, cdsTest);
@@ -36,7 +37,8 @@ public void testGetCDSLengthReverseAsc() {
3637
int cdsStart = 55;
3738
int cdsEnd = 75;
3839

39-
int cdsDesired = 12;
40+
int cdsDesired = 12 - 3;
41+
ChromosomeMappingTools.setCoordinateSystem(0);
4042
int cdsTest = ChromosomeMappingTools.getCDSLengthReverse(exonStarts, exonEnds, cdsStart, cdsEnd);
4143

4244
assertEquals(cdsDesired, cdsTest);
@@ -50,7 +52,8 @@ public void testGetCDSLengthReverseDesc() {
5052
int cdsStart = 75;
5153
int cdsEnd = 50;
5254

53-
int cdsDesired = 17;
55+
int cdsDesired = 17 - 3;
56+
ChromosomeMappingTools.setCoordinateSystem(0);
5457
int cdsTest = ChromosomeMappingTools.getCDSLengthReverse(exonStarts, exonEnds, cdsStart, cdsEnd);
5558

5659
assertEquals(cdsDesired, cdsTest);

0 commit comments

Comments
 (0)