001/** 002 * Copyright (C) 2010-2015 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of JEnsembl: a Java API to Ensembl data sources developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * Project hosted at: http://jensembl.sourceforge.net 009 * 010 * This is free software: you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License (version 3) as published by 012 * the Free Software Foundation. 013 * 014 * This software is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * in this software distribution. If not, see: http://opensource.org/licenses/gpl-3.0.html 021 */ 022package uk.ac.roslin.ensembl.datasourceaware.core; 023 024import org.biojava3.core.sequence.RNASequence; 025import org.biojava3.core.sequence.Strand; 026import uk.ac.roslin.ensembl.exception.RangeException; 027 028/** 029 * 030 * @author paterson 031 */ 032public class GapSequence extends DADNASequence { 033 034 private static final String base = "N"; 035 private StringBuffer seq = null; 036 037 public static GapSequence makeGap(Integer length) { 038 if (length == null || length <0 ) { 039 return null; 040 } 041 return new GapSequence(length); 042 } 043 044 public static String getGapString(Integer length) { 045 StringBuffer sb = new StringBuffer(); 046 for (int i = 0; i < length; i++) { 047 sb= sb.append(base); 048 } 049 return sb.toString(); 050 } 051 public static String getGapString(Integer length, char gap) { 052 StringBuffer sb = new StringBuffer(); 053 for (int i = 0; i < length; i++) { 054 sb= sb.append(gap); 055 } 056 return sb.toString(); 057 } 058 059 private GapSequence(Integer length) { 060 super(); 061 this.setDBSeqLength(length); 062 this.setBioBegin(1); 063 this.setBioEnd(length); 064 this.setName("Gap of "+length); 065 } 066 067 068 @Override 069 public String getSequenceAsString() { 070 if (this.getLength()==0) { 071 return ""; 072 } 073 return this.getSequenceAsString(this.getBioBegin(), this.getBioEnd()); 074 } 075 076 public String getRNASequenceAsString() { 077 if(this.getLength()==0) { 078 return ""; 079 } 080 return this.getSequenceAsString(this.getBioBegin(), this.getBioEnd()); 081 } 082 083 @Override 084 public String getReverseComplementSequenceAsString() { 085 return this.getSequenceAsString(this.getBioBegin(), this.getBioEnd()); 086 } 087 088 @Override 089 public String getSequenceAsString(Integer start, Integer stop) throws RangeException { 090 if(this.getLength()==0) { 091 return ""; 092 } 093 Integer begin = start; 094 Integer end = stop; 095 if (end == null || end > this.getBioEnd()|| 096 begin == null || begin<this.getBioBegin() ) { 097 throw new RangeException("Requested range is outside of the extent of the Sequence." 098 +" Try using 'getPaddedSequenceAsString(int, int)' method."); 099 } 100 if (begin==0 || end ==0) { 101 throw new IllegalArgumentException("The position 0 is meaningless in the Ensembl DNA world." 102 +" Use -1 for one base upstream or +1 for the first base."); 103 } 104 105 if (begin>end) { 106 begin = stop; 107 end = start; 108 } 109 return GapSequence.getGapString(end-begin+1); 110 } 111 112 @Override 113 public String getPaddedSequenceAsString(Integer start, Integer stop) { 114 if (start==0 || stop ==0) { 115 throw new IllegalArgumentException("The position 0 is meaningless in the Ensembl DNA world." 116 +" Use -1 for one base upstream or +1 for the first base."); 117 } 118 if(this.getLength()==0) { 119 return ""; 120 } 121 int begin = start; 122 int end = stop; 123 if (begin>end) { 124 begin = stop; 125 end = start; 126 } 127 if ( begin > 0 || end < 0) { 128 return GapSequence.getGapString(end-begin+1); 129 } else { 130 return GapSequence.getGapString(end-begin); 131 } 132 } 133 134 public String getRNASequenceAsString(Integer start, Integer stop) throws RangeException { 135 136 return getSequenceAsString(start, stop) ; 137 } 138 139 public String getPaddedRNASequenceAsString(Integer start, Integer stop) { 140 return getPaddedSequenceAsString(start, stop) ; 141 } 142 143 @Override 144 @Deprecated 145 /** 146 * Don't use Strands for getting sequence strings - always use getSequenceAsString 147 * or getReverseComplementSequenceAsString to guarantee polarity. 148 */ 149 public String getSequenceAsString(Integer start, Integer stop, Strand strand){ 150 return getSequenceAsString(start, stop); 151 } 152 153 @Override 154 public String getReverseComplementSequenceAsString(Integer start, Integer stop) throws RangeException { 155 return this.getSequenceAsString(start, stop); 156 } 157 158 @Override 159 public String getPaddedReverseComplementSequenceAsString(Integer start, Integer stop){ 160 return this.getPaddedSequenceAsString(start, stop); 161 } 162 163 @Override 164 public String toString() { 165 return name; 166 } 167 168 @Override 169 public int getLength() { 170 return this.getDBSeqLength().intValue(); 171 } 172}