1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
package gff;
import java.util.ArrayList;
import java.util.Arrays;
import fileIO.ByteStreamWriter;
import shared.Shared;
import shared.Tools;
import structures.ByteBuilder;
public class GbffFeature {
public GbffFeature(final ArrayList<byte[]> lines0, final String typeString, final String accessionString){
accession=accessionString;
setType(typeString);
parseSlow(lines0);
if(type==rRNA){
setSubtype();
}
if(stop<start){error=true;}
}
private void parseSlow(final ArrayList<byte[]> lines0){
ArrayList<byte[]> lines=fixLines(lines0);
parseStartStop(lines.get(0));
for(int i=1; i<lines.size(); i++){
byte[] line=lines.get(i);
if(Tools.startsWith(line, "product=")){
product=parseLine(line);
}else if(Tools.startsWith(line, "locus_tag=")){
locus_tag=parseLine(line);
}else if(Tools.equals(line, "pseudo")){
pseudo=true;
}
// else if(Tools.startsWith(line, "ID=")){
// id=parseLine(line);
// }else if(Tools.startsWith(line, "Name=")){
// name=parseLine(line);
// }
}
// System.err.println("\nvvvvv");
// for(byte[] line : lines0){
// System.err.println("'"+new String(line)+"'");
// }
// for(byte[] line : lines){
// System.err.println("'"+new String(line)+"'");
// }
// System.err.println("^^^^^");
}
ArrayList<byte[]> fixLines(ArrayList<byte[]> lines){
ArrayList<byte[]> fixed=new ArrayList<byte[]>();
ByteBuilder bb=new ByteBuilder();
for(byte[] line : lines){
if(bb.length()>0 && line[21]=='/'){
fixed.add(bb.toBytes());
bb.clear();
}
append(bb, line);
}
if(bb.length()>0){
fixed.add(bb.toBytes());
bb.clear();
}
return fixed;
}
void append(ByteBuilder bb, byte[] line){
assert(line[20]==' ');
assert(line.length>21);
// assert(line[21]!=' ') : "'"+new String(line)+"'";
if(line[21]=='/'){
bb.append(line, 22, line.length-22);
}else{
// System.err.println(line.length+", "+21+", "+(line.length-21+1)+"\n'"+new String(line)+"'");
if(bb.length>0){bb.append(' ');}
bb.append(line, 21, line.length-21);
}
}
void setType(String typeString){
int x=Tools.find(typeString, typeStrings);
assert(x>=0) : x+", "+typeString;
type=x;
}
void parseStartStop(final byte[] line0){
byte[] line=line0;
if(line[0]=='c'){
assert(Tools.startsWith(line, "complement("));
line=Arrays.copyOfRange(line, 11, line.length-1);
strand=Shared.MINUS;
}
if(line[0]=='j'){
assert(Tools.startsWith(line, "join("));
line=Arrays.copyOfRange(line, 5, line.length-1);
strand=Shared.MINUS;
}
int i=0;
for(start=0; i<line.length; i++){
int x=line[i];
if(x=='.'){break;}
else if(x!='<'){
if(Tools.isDigit(x)){
start=start*10+(x-'0');
}else{
//if(!error){System.err.println(new String(line0)+"\n"+new String(line));}
error=true;
}
}
}
// while(line[i]=='.'){i++;} //Not needed
for(stop=0; i<line.length; i++){
int x=line[i];
if(x=='.' || x==','){
stop=0;
}else if(x==' '){
//do nothing; line wrap
}else if(x!='>'){
if(Tools.isDigit(x)){
stop=stop*10+(x-'0');
}else{
//if(!error){System.err.println(new String(line0)+"\n"+new String(line));}
error=true;
}
}
}
}
String parseLine(byte[] line){
String[] split=Tools.equalsPattern.split(new String(line));
String s=split[1];
return s.substring(1, s.length()-1);
}
void setSubtype(){
subtype=-1;
if(product==null){return;}
String[] split=Tools.spacePattern.split(product);
subtype=Tools.find(split[0], typeStrings);
// assert(false) : type+", "+subtype+", "+split[0]+", "+this.toString()+"\n"+product;
}
public void toGff(ByteStreamWriter bsw) {
ByteBuilder bb=bsw.getBuffer();
appendGff(bb);
bb.nl();
bsw.flushBuffer(false);
}
public ByteBuilder appendGff(ByteBuilder bb) {
// bsw.print("#seqid source type start end score strand phase attributes\n".getBytes());
bb.append(accession).tab();
bb.append('.').tab();
bb.append((pseudo && type==GENE) ? "pseudogene" : typeStringsGff[type]).tab();
bb.append(start).tab();
bb.append(stop).tab();
bb.append('.').tab();
bb.append(Shared.strandCodes2[strand]).tab();
bb.append('.').tab();
boolean attributes=false;
// if(id!=null){
// bb.append("ID=").append(id);
// attributes=true;
// }
// if(name!=null){
// if(attributes){bb.append(';');}
// bb.append("Name=").append(name);
// attributes=true;
// }
if(product!=null){
if(attributes){bb.append(';');}
bb.append("product=").append(product);
attributes=true;
}
if(locus_tag!=null){
if(attributes){bb.append(';');}
bb.append("locus_tag=").append(locus_tag);
attributes=true;
}
if(subtype>-1){
if(attributes){bb.append(';');}
bb.append("subtype=").append(typeStringsGff[subtype]);
attributes=true;
}
if(!attributes){bb.append('.');}
return bb;
}
@Override
public String toString(){
return appendGff(new ByteBuilder()).toString();
}
public int type=-1;
public int subtype=-1;
//TODO: could have coding amino, for tRNA
public String product;
public String locus_tag;
// public String id;
// public String name;
public int start;
public int stop;
public byte strand=Shared.PLUS;
public String accession;
public boolean pseudo=false;
public boolean error=false;
public static final String[] typeStrings={"gene", "CDS", "rRNA", "tRNA", "ncRNA", "repeat_region",
"5'UTR", "3'UTR", "intron", "exon", "5S", "16S", "23S"};
public static final String[] typeStringsGff={"gene", "CDS", "rRNA", "tRNA", "ncRNA", "repeat_region",
"five_prime_UTR", "three_prime_UTR", "intron", "exon", "5S", "16S", "23S"};
//types
public static final int GENE=0, CDS=1, rRNA=2, tRNA=3, ncRNA=4, repeat_region=5, UTR5=6, UTR3=7, intron=8, exon=9;
//subtypes
public static final int r5S=10, r16S=11, r23S=12;
}
|