| 1 |
/** |
| 2 |
* Copyright Copyright 2010-17 Simon Andrews |
| 3 |
* |
| 4 |
* This file is part of FastQC. |
| 5 |
* |
| 6 |
* FastQC is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* FastQC is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with FastQC; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 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.InputStreamReader; |
| 28 |
import java.util.Vector; |
| 29 |
|
| 30 |
import uk.ac.babraham.FastQC.FastQCConfig; |
| 31 |
|
| 32 |
public class ContaminentFinder { |
| 33 |
|
| 34 |
private static Contaminant [] contaminants; |
| 35 |
|
| 36 |
public static ContaminantHit findContaminantHit (String sequence) { |
| 37 |
if (contaminants == null) { |
| 38 |
contaminants = makeContaminantList(); |
| 39 |
} |
| 40 |
|
| 41 |
ContaminantHit bestHit = null; |
| 42 |
|
| 43 |
for (int c=0;c<contaminants.length;c++) { |
| 44 |
ContaminantHit thisHit = contaminants[c].findMatch(sequence); |
| 45 |
|
| 46 |
// System.out.println("Best hit from "+c+" is "+thisHit); |
| 47 |
|
| 48 |
if (thisHit == null) continue; // No hit |
| 49 |
|
| 50 |
if (bestHit == null || thisHit.length() > bestHit.length() || (thisHit.length() == bestHit.length() && thisHit.percentID() > bestHit.percentID())) { |
| 51 |
bestHit = thisHit; |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
return bestHit; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
private static Contaminant [] makeContaminantList () { |
| 62 |
Vector<Contaminant>c = new Vector<Contaminant>(); |
| 63 |
|
| 64 |
try { |
| 65 |
|
| 66 |
BufferedReader br = null; |
| 67 |
if (FastQCConfig.getInstance().contaminant_file == null) { |
| 68 |
InputStream rsrc=ContaminentFinder.class.getResourceAsStream("/Configuration/contaminant_list.txt"); |
| 69 |
if (rsrc==null) throw new FileNotFoundException("cannot find Confituration/contaminant_list.txt"); |
| 70 |
br =new BufferedReader(new InputStreamReader(rsrc)); |
| 71 |
} |
| 72 |
else { |
| 73 |
br=new BufferedReader(new FileReader(FastQCConfig.getInstance().contaminant_file)); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
String line; |
| 79 |
while ((line = br.readLine())!= null){ |
| 80 |
|
| 81 |
if (line.startsWith("#")) continue; // Skip comments |
| 82 |
if (line.trim().length() == 0) continue; // Skip blank lines |
| 83 |
|
| 84 |
String [] sections = line.split("\\t+"); |
| 85 |
if (sections.length != 2) { |
| 86 |
System.err.println("Expected 2 sections for contaminant line but got "+sections.length+" from "+line); |
| 87 |
continue; |
| 88 |
} |
| 89 |
Contaminant con = new Contaminant(sections[0], sections[1]); |
| 90 |
c.add(con); |
| 91 |
} |
| 92 |
|
| 93 |
br.close(); |
| 94 |
} |
| 95 |
catch (IOException e) { |
| 96 |
e.printStackTrace(); |
| 97 |
} |
| 98 |
|
| 99 |
return c.toArray(new Contaminant[0]); |
| 100 |
} |
| 101 |
/* |
| 102 |
public static void main (String [] args) { |
| 103 |
|
| 104 |
Config cfg=new Config(); |
| 105 |
String query = "agagtgtagatctccgtggtcgccgtatca"; |
| 106 |
|
| 107 |
ContaminantHit c = findContaminantHit(cfg,query); |
| 108 |
|
| 109 |
System.out.println("Query was "+query.length()+"bp Found hit "+c); |
| 110 |
|
| 111 |
}*/ |
| 112 |
|
| 113 |
} |