| 1 |
package pp2.datahandler; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.BufferedWriter; |
| 5 |
import java.io.File; |
| 6 |
import java.io.FileNotFoundException; |
| 7 |
import java.io.IOException; |
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.HashSet; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Set; |
| 12 |
|
| 13 |
import pp2.components.GO; |
| 14 |
import pp2.components.Protein; |
| 15 |
import pp2.tools.Tools; |
| 16 |
|
| 17 |
public class SwissProtParser { |
| 18 |
|
| 19 |
private List<Protein> proteins; |
| 20 |
private File database; |
| 21 |
|
| 22 |
public SwissProtParser(String db) { |
| 23 |
this.database = new File(db); |
| 24 |
} |
| 25 |
|
| 26 |
public SwissProtParser(File db) { |
| 27 |
this.database = db; |
| 28 |
} |
| 29 |
|
| 30 |
public void loadProteins(String outputFile) { |
| 31 |
loadProteins(outputFile, false); |
| 32 |
} |
| 33 |
|
| 34 |
public void loadProteins(String outputFile, boolean save) { |
| 35 |
proteins = new ArrayList<Protein>(); |
| 36 |
boolean output = true; |
| 37 |
if(outputFile.isEmpty()) { |
| 38 |
output = false; |
| 39 |
} |
| 40 |
try { |
| 41 |
BufferedReader db = Tools.openFile(database); |
| 42 |
BufferedWriter of = null; |
| 43 |
if(output) { |
| 44 |
of = Tools.writeToFile(outputFile); |
| 45 |
} |
| 46 |
|
| 47 |
String line; |
| 48 |
boolean newEntry = true; |
| 49 |
StringBuffer entry = new StringBuffer(""); |
| 50 |
|
| 51 |
while((line = db.readLine()) != null) { |
| 52 |
if(newEntry) { |
| 53 |
entry = new StringBuffer(""); |
| 54 |
entry.append(line+"\n"); |
| 55 |
newEntry = false; |
| 56 |
} else { |
| 57 |
entry.append(line+"\n"); |
| 58 |
if(line.endsWith("//")) { |
| 59 |
newEntry = true; |
| 60 |
Protein p = new Protein(entry); |
| 61 |
if(save) { |
| 62 |
proteins.add(p); |
| 63 |
} |
| 64 |
if(output && p.getAnnotations().length > 0) { |
| 65 |
of.write(p.toString()+"\n"); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
db.close(); |
| 72 |
if(output) { |
| 73 |
of.flush(); |
| 74 |
of.close(); |
| 75 |
} |
| 76 |
} catch (FileNotFoundException e) { |
| 77 |
System.err.println("Could not find database file!"); |
| 78 |
} catch (IOException e) { |
| 79 |
e.printStackTrace(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public void goStatistics() { |
| 84 |
int totalNumber = 0; |
| 85 |
int goNumber = 0; |
| 86 |
int goSequences = 0; |
| 87 |
Set<String> goIDs = new HashSet<String>(); |
| 88 |
|
| 89 |
for(Protein p : proteins) { |
| 90 |
totalNumber++; |
| 91 |
if(p.getAnnotations().length > 0) { |
| 92 |
goSequences++; |
| 93 |
goNumber += p.getAnnotations().length; |
| 94 |
for(GO go : p.getAnnotations()) { |
| 95 |
goIDs.add(go.getId()); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
System.out.println("There are a total of "+goSequences+" GO-annotated sequences."); |
| 101 |
System.out.println("The average number of GO numbers per se" + |
| 102 |
"quence is\n"+String.format("%.2f", (double)goNumber/(double)goSequences)+ |
| 103 |
"when considering only GO-annotated sequences, and\n"+ |
| 104 |
String.format("%.2f", (double)goNumber/(double)totalNumber)+" for all" + |
| 105 |
"sequences."); |
| 106 |
System.out.println("There are "+goIDs.size()+" distinct GO numbers."); |
| 107 |
} |
| 108 |
|
| 109 |
public void goStatistics(String fastaFile) { |
| 110 |
int goNumber = 0; |
| 111 |
int goSequences = 0; |
| 112 |
Set<String> goIDs = new HashSet<String>(); |
| 113 |
|
| 114 |
try { |
| 115 |
BufferedReader fasta = Tools.openFile(fastaFile); |
| 116 |
String line; |
| 117 |
|
| 118 |
while((line = fasta.readLine()) != null) { |
| 119 |
if(line.startsWith(">")) { |
| 120 |
goSequences++; |
| 121 |
goNumber += line.split(",").length; |
| 122 |
for(String go : line.substring(1).split(",")) { |
| 123 |
goIDs.add(go); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} catch (FileNotFoundException e) { |
| 128 |
System.err.println("Could not find file "+fastaFile); |
| 129 |
} catch (IOException e) { |
| 130 |
e.printStackTrace(); |
| 131 |
} |
| 132 |
System.out.println("There are a total of "+goSequences+" GO-annotated sequences."); |
| 133 |
System.out.println("The average number of GO numbers per se" + |
| 134 |
"quence is\n"+String.format("%.2f", (double)goNumber/(double)goSequences)+ |
| 135 |
" when considering only GO-annotated sequences."); |
| 136 |
System.out.println("There are "+goIDs.size()+" distinct GO numbers."); |
| 137 |
} |
| 138 |
|
| 139 |
public void loadProteins() { |
| 140 |
loadProteins(""); |
| 141 |
} |
| 142 |
|
| 143 |
public static void printUsage() { |
| 144 |
System.out.println("Usage: java SwissProtParser -i [Swissprot-Database-File] -o [OutputDir]"); |
| 145 |
} |
| 146 |
|
| 147 |
public static void main(String[] args) { |
| 148 |
if(args.length > 0) { |
| 149 |
String database = ""; |
| 150 |
String outputDir = ""; |
| 151 |
for(int i=0; i<args.length; i+=2) { |
| 152 |
if(args[i].equals("-i")) { |
| 153 |
database = args[i+1]; |
| 154 |
} else if(args[i].equals("-o")) { |
| 155 |
outputDir = args[i+1]; |
| 156 |
} else { |
| 157 |
printUsage(); |
| 158 |
System.exit(0); |
| 159 |
} |
| 160 |
SwissProtParser spp = new SwissProtParser(database); |
| 161 |
spp.loadProteins(outputDir+"go-annotations.fa"); |
| 162 |
spp.goStatistics(outputDir+"go-annotations.fa"); |
| 163 |
} |
| 164 |
} else { |
| 165 |
printUsage(); |
| 166 |
} |
| 167 |
} |
| 168 |
} |