| 1 | package jgi; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | |
| 5 | import dna.AminoAcid; |
| 6 | import fileIO.ByteFile; |
| 7 | import fileIO.FileFormat; |
| 8 | import shared.Tools; |
| 9 | import structures.IntList; |
| 10 | |
| 11 | public class Assembly { |
| 12 | |
| 13 | public Assembly(String fname_) { |
| 14 | fname=fname_; |
| 15 | load(); |
| 16 | } |
| 17 | |
| 18 | void load() { |
| 19 | FileFormat ff=FileFormat.testInput(fname, FileFormat.FASTA, null, true, true); |
| 20 | assert(ff.fasta()); |
| 21 | ByteFile bf=ByteFile.makeByteFile(ff); |
| 22 | |
| 23 | clear(); |
| 24 | acgtnio=new long[7]; |
| 25 | int contigLen=0; |
| 26 | for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) { |
| 27 | if(line[0]=='>') { |
| 28 | headerLength+=(line.length-1); |
| 29 | if(firstHeader==null && line.length>1) { |
| 30 | firstHeader=new String(line, 1, line.length-1); |
| 31 | } |
| 32 | if(contigLen>0) { |
| 33 | contigs.add(contigLen); |
| 34 | } |
| 35 | contigLen=0; |
| 36 | }else { |
| 37 | addToACGTNIO(line); |
| 38 | contigLen+=line.length; |
| 39 | } |
| 40 | } |
| 41 | bf.close(); |
| 42 | contigs.sort(); |
| 43 | contigs.reverse(); |
| 44 | length=contigs.sumLong(); |
| 45 | } |
| 46 | |
| 47 | void addToACGTNIO(byte[] line) { |
| 48 | for(byte b : line) { |
| 49 | byte x=baseToACGTNIO[b]; |
| 50 | acgtnio[x]++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void clear() { |
| 55 | contigs.clear(); |
| 56 | length=0; |
| 57 | headerLength=0; |
| 58 | firstHeader=null; |
| 59 | acgtnio=null; |
| 60 | } |
| 61 | |
| 62 | float gc() { |
| 63 | float AT=acgtnio[A]+acgtnio[T]+acgtnio[U]; |
| 64 | float GC=acgtnio[G]+acgtnio[C]; |
| 65 | return GC/Tools.max(1, AT+GC); |
| 66 | } |
| 67 | |
| 68 | long lengthAtLeast(int minimum) { |
| 69 | long sum=0; |
| 70 | for(int i=0; i<contigs.size; i++) { |
| 71 | int len=contigs.get(i); |
| 72 | if(len<minimum) {break;} |
| 73 | sum+=len; |
| 74 | } |
| 75 | return sum; |
| 76 | } |
| 77 | |
| 78 | final String fname; |
| 79 | IntList contigs=new IntList(); |
| 80 | long length=0; |
| 81 | long headerLength=0; |
| 82 | String firstHeader=null; |
| 83 | long[] acgtnio; |
| 84 | |
| 85 | public static final byte[] baseToACGTNIO=makeBaseToACGTUNIO(); |
| 86 | private static final byte A=0, C=1, G=2, T=3, U=4, N=5, IUPAC=6, OTHER=7; |
| 87 | |
| 88 | private static final byte[] makeBaseToACGTUNIO() { |
| 89 | final byte[] array=new byte[128]; |
| 90 | Arrays.fill(array, OTHER); |
| 91 | array['a']=array['A']=A; |
| 92 | array['c']=array['C']=C; |
| 93 | array['g']=array['G']=G; |
| 94 | array['t']=array['T']=T; |
| 95 | array['u']=array['U']=U; |
| 96 | array['n']=array['N']=N; |
| 97 | for(int i=0; i<array.length; i++) { |
| 98 | if(AminoAcid.baseToNumberExtended[i]>=0 && array[i]==OTHER) { |
| 99 | array[i]=IUPAC; |
| 100 | } |
| 101 | } |
| 102 | return array; |
| 103 | } |
| 104 | |
| 105 | } |