| 1 |
package dna; |
| 2 |
import java.util.ArrayList; |
| 3 |
|
| 4 |
import shared.Shared; |
| 5 |
import shared.Tools; |
| 6 |
|
| 7 |
|
| 8 |
public class GeneSet implements Comparable<GeneSet>{ |
| 9 |
|
| 10 |
public static void main(String[] args){ |
| 11 |
Data.getGeneIDTable(); |
| 12 |
} |
| 13 |
|
| 14 |
public GeneSet(String n, ArrayList<Gene> g){ |
| 15 |
name=n; |
| 16 |
id=g.get(0).id; |
| 17 |
genes=g; |
| 18 |
chrom=g.get(0).chromosome; |
| 19 |
transcripts=genes.size(); |
| 20 |
assert(transcripts>0); |
| 21 |
|
| 22 |
byte st=-1; |
| 23 |
|
| 24 |
boolean pse=true, unt=true; |
| 25 |
|
| 26 |
for(int i=0; i<transcripts; i++){ |
| 27 |
Gene gene=g.get(i); |
| 28 |
|
| 29 |
assert(gene.id==id) : g; |
| 30 |
|
| 31 |
pse=(pse&&gene.pseudo); |
| 32 |
unt=(unt&&gene.untranslated); |
| 33 |
minStart=min((int)gene.txStart, minStart); |
| 34 |
maxEnd=max((int)gene.txStop, maxEnd); |
| 35 |
|
| 36 |
if(st==-1){st=gene.strand;} |
| 37 |
else if(st!=gene.strand){st=(byte) Tools.find("?", Shared.strandCodes);} |
| 38 |
} |
| 39 |
|
| 40 |
pseudo=pse; |
| 41 |
untranslated=unt; |
| 42 |
|
| 43 |
for(Gene gene : g){ |
| 44 |
assert(pseudo==gene.pseudo || (!pseudo && gene.pseudo)) : g; |
| 45 |
assert(untranslated==gene.untranslated || (!untranslated && gene.untranslated)) : g; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
strand=st; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public String toString(){ |
| 54 |
StringBuilder sb=new StringBuilder(); |
| 55 |
sb.append(name); |
| 56 |
while(sb.length()<10){sb.append(' ');} |
| 57 |
sb.append('\t'); |
| 58 |
sb.append(padFront(transcripts+"",2)+" transcript"+(transcripts==1 ? " " : "s")); |
| 59 |
|
| 60 |
sb.append("\tchr"+chrom+" ("+minStart+" - "+maxEnd+"), '"+Shared.strandCodes[strand]+"'"); |
| 61 |
|
| 62 |
return sb.toString(); |
| 63 |
} |
| 64 |
|
| 65 |
private static final String padFront(String num, int width){ |
| 66 |
String r=num; |
| 67 |
while(r.length()<width){r=" "+r;} |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
private static final String padBack(String num, int width){ |
| 72 |
String r=num; |
| 73 |
while(r.length()<width){r=r+" ";} |
| 74 |
return r; |
| 75 |
} |
| 76 |
|
| 77 |
public final String name; |
| 78 |
public final int id; |
| 79 |
public final byte chrom; |
| 80 |
public final byte strand; |
| 81 |
public final ArrayList<Gene> genes; |
| 82 |
public final int transcripts; |
| 83 |
|
| 84 |
|
| 85 |
public final boolean untranslated; |
| 86 |
|
| 87 |
public final boolean pseudo; |
| 88 |
|
| 89 |
public int minStart=Integer.MAX_VALUE; |
| 90 |
public int maxEnd=0; |
| 91 |
|
| 92 |
|
| 93 |
public boolean intersects(int point){ |
| 94 |
return point>=minStart && point<=maxEnd; |
| 95 |
} |
| 96 |
public boolean intersects(int point1, int point2){ |
| 97 |
return point2>=minStart && point1<=maxEnd; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
@Override |
| 102 |
public int compareTo(GeneSet other) { |
| 103 |
if(chrom!=other.chrom){ |
| 104 |
return chrom>other.chrom ? 1 : -1; |
| 105 |
} |
| 106 |
int x=minStart<other.minStart ? -1 : minStart>other.minStart ? 1 : 0; |
| 107 |
if(x!=0){return x;} |
| 108 |
return x=name.compareTo(other.name); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public boolean equals(Object other){ |
| 113 |
return equals((GeneSet)other); |
| 114 |
} |
| 115 |
|
| 116 |
public boolean equals(GeneSet other){ |
| 117 |
return compareTo(other)==0; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public int hashCode(){ |
| 122 |
return Integer.rotateLeft(name.hashCode(), 5)^chrom; |
| 123 |
} |
| 124 |
|
| 125 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 126 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 127 |
private static final byte min(byte x, byte y){return x<y ? x : y;} |
| 128 |
private static final byte max(byte x, byte y){return x>y ? x : y;} |
| 129 |
private static final long min(long x, long y){return x<y ? x : y;} |
| 130 |
private static final long max(long x, long y){return x>y ? x : y;} |
| 131 |
private static final float min(float x, float y){return x<y ? x : y;} |
| 132 |
private static final float max(float x, float y){return x>y ? x : y;} |
| 133 |
|
| 134 |
} |