| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package uk.ac.babraham.FastQC.Sequence.Contaminant; |
| 21 |
|
| 22 |
import java.io.BufferedReader; |
| 23 |
import java.io.FileNotFoundException; |
| 24 |
import java.io.FileReader; |
| 25 |
import java.io.IOException; |
| 26 |
import java.io.InputStream; |
| •27 |
import java.io.FileInputStream; |
| 28 |
import java.io.InputStreamReader; |
| 29 |
import java.util.Vector; |
| 30 |
|
| 31 |
import uk.ac.babraham.FastQC.FastQCConfig; |
| 32 |
|
| 33 |
public class ContaminentFinder { |
| 34 |
|
| 35 |
private static Contaminant [] contaminants; |
| 36 |
|
| 37 |
public static ContaminantHit findContaminantHit (String sequence) { |
| 38 |
if (contaminants == null) { |
| 39 |
contaminants = makeContaminantList(); |
| 40 |
} |
| 41 |
|
| 42 |
ContaminantHit bestHit = null; |
| 43 |
|
| 44 |
for (int c=0;c<contaminants.length;c++) { |
| 45 |
ContaminantHit thisHit = contaminants[c].findMatch(sequence); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if (thisHit == null) continue; |
| 50 |
|
| 51 |
if (bestHit == null || thisHit.length() > bestHit.length() || (thisHit.length() == bestHit.length() && thisHit.percentID() > bestHit.percentID())) { |
| 52 |
bestHit = thisHit; |
| 53 |
} |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
return bestHit; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
private static Contaminant [] makeContaminantList () { |
| 63 |
Vector<Contaminant>c = new Vector<Contaminant>(); |
| 64 |
|
| 65 |
try { |
| 66 |
|
| 67 |
BufferedReader br = null; |
| 68 |
if (FastQCConfig.getInstance().contaminant_file == null) { |
| •69 |
InputStream rsrc = new FileInputStream("/etc/fastqc/Configuration/contaminant_list.txt"); |
| 70 |
if (rsrc==null) throw new FileNotFoundException("cannot find /etc/fastqc/Configuration/contaminant_list.txt"); |
| 71 |
br =new BufferedReader(new InputStreamReader(rsrc)); |
| 72 |
} |
| 73 |
else { |
| 74 |
br=new BufferedReader(new FileReader(FastQCConfig.getInstance().contaminant_file)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
String line; |
| 80 |
while ((line = br.readLine())!= null){ |
| 81 |
|
| 82 |
if (line.startsWith("#")) continue; |
| 83 |
if (line.trim().length() == 0) continue; |
| 84 |
|
| 85 |
String [] sections = line.split("\\t+"); |
| 86 |
if (sections.length != 2) { |
| 87 |
System.err.println("Expected 2 sections for contaminant line but got "+sections.length+" from "+line); |
| 88 |
continue; |
| 89 |
} |
| 90 |
Contaminant con = new Contaminant(sections[0], sections[1]); |
| 91 |
c.add(con); |
| 92 |
} |
| 93 |
|
| 94 |
br.close(); |
| 95 |
} |
| 96 |
catch (IOException e) { |
| 97 |
e.printStackTrace(); |
| 98 |
} |
| 99 |
|
| 100 |
return c.toArray(new Contaminant[0]); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
} |