Debian Codegraph @

current/jgi/KmerSample.java

raw symbols SCIP
1 package jgi;
2
3 import fileIO.TextFile;
4
5 /**
6 * @author Brian Bushnell
7 * @date Oct 10, 2012
8 *
9 */
10 public class KmerSample {
11
12
13 public static int[] makeKmerSet(int K, String filename){
14
15 //Number of bits in a kmer
16 int kbits=2*K;
17
18 //Number of possible kmers
19 long kmerSpace=(1L<<kbits);
20
21 //Make an array of the correct size, remembering that int is 32 bits
22 int[] array=new int[(int)(kmerSpace/32)];
23
24 //Current kmer
25 long kmer=0;
26
27 //Length of current kmer
28 int len=0;
29
30 //This will create a bitmask of 00000...0000111111...11111, where the number if 1's is equal to kbits.
31 long mask=(kbits>63 ? -1L : ~((-1L)<<kbits));
32
33 //Initialize an input stream for the fasta file
34 TextFile tf=new TextFile(filename, false);
35
36 //Grab the first line of the fasta file
37 String line=tf.nextLine();
38
39 while(line!=null){
40
41 if(line.length()<1){
42 //The line is empty, so ignore it (should never happen in a proper fasta file)
43 }else if(line.charAt(0)=='>'){
44 //The line is name of a new contig/scaffold, so reset the kmer
45 kmer=0;
46 len=0;
47 }else{
48 //Otherwise, generate kmers
49
50 for(int i=0; i<line.length(); i++){
51
52 //The base at location "i" in the string
53 char letter=line.charAt(i);
54
55 //The 2-bit numeric code for the base
56 int code;
57
58 if(letter=='A'){code=0;}
59 else if(letter=='C'){code=1;}
60 else if(letter=='G'){code=2;}
61 else if(letter=='T'){code=3;}
62 else{code=-1;}
63
64 if(code<0){
65 //The base was an N or degenerate letter, so reset the kmer
66 kmer=0;
67 len=0;
68 }else{
69 //insert the code into the current kmer
70 kmer=(kmer<<2); //left shift by 2
71 kmer=(kmer|code); //or with the code
72 kmer=(kmer&mask); //and with the mask to prevent going past the intended kmer length
73 len++; //Increment the length of the kmer
74
75 if(len>=K){
76 //If the kmer is long enough, then add it to the array
77
78 //The index in the array is the upper bits of the kmer. Each location in the array is 32 bits.
79 int index=(int)(kmer/32);
80
81 //The bit within the word of the array is the lower 5 bits of the kmer
82 int bit=(int)(kmer%32);
83
84 //A bitmask to set the correct bit in the array to 1.
85 int x=(1<<bit);
86
87 //OR the array location with the new mask.
88 array[index]=(array[index] | x);
89 }
90 }
91 }
92 }
93
94 //Grab the next line
95 line=tf.nextLine();
96 }
97
98 //Close your input stream
99 tf.close();
100
101 return array;
102 }
103
104 public static boolean containsKmer(long kmer, int[] array){
105
106 //The index in the array is the upper bits of the kmer. Each location in the array is 32 bits.
107 int index=(int)(kmer/32);
108
109 //The bit within the word of the array is the lower 5 bits of the kmer
110 int bit=(int)(kmer%32);
111
112 //A bitmask to test the correct bit in the array to 1.
113 int x=(1<<bit);
114
115 if((array[index]&x)==0){//Check to see if the bit is set in the array
116 return false;
117 }else{
118 return true;
119 }
120
121 }
122
123 }