-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtoAminoAcidDatabase.java
251 lines (231 loc) · 10.7 KB
/
ProtoAminoAcidDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.util.*;
import java.io.*;
import com.google.common.collect.*;
import java.util.concurrent.*;
/**
* This singleton holds all of the ProtoAminoAcids.
* Implemented as two parallel lists.
*/
public final class ProtoAminoAcidDatabase implements Singleton
{
/**
* Contains a list of all the possible amino acids.
*/
public static final ImmutableList<AminoAcid> KEYS;
/**
* Contains all of the amino acid templates.
* List indices parallel that of KEYS.
*/
public static final ImmutableList<ImmutableList<ProtoAminoAcid>> VALUES;
// static initializer
static
{
// make temporary lists
List<AminoAcid> tempKeys = new LinkedList<>();
List<List<ProtoAminoAcid>> tempValues = new LinkedList<>();
Set<Atom> allAtoms = new HashSet<Atom>();
// populate keys
for (AminoAcid a : AminoAcid.values())
{
tempKeys.add(a);
tempValues.add(new LinkedList<ProtoAminoAcid>());
}
// read all data from the library directory
String directory = Settings.PROTOAMINOACID_DIRECTORY;
int counter = 0;
Set<String> allDescriptions = new HashSet<>();
for (File f : new File(directory).listFiles())
{
// parse all txt files
String filename = f.getName();
if ( filename.endsWith(".txt") && ! f.isDirectory() )
{
try
{
// read file
ProtoAminoAcidFile m = new ProtoAminoAcidFile(directory + filename);
ProtoAminoAcid p = new ProtoAminoAcid(m);
counter++;
p = p.shift(counter);
// check for duplicate atoms
for (Atom a : p.molecule.contents)
{
if ( ! allAtoms.contains(a) )
allAtoms.add(a);
else
throw new IllegalArgumentException("Duplicate atom: " + a.toString());
}
// check for duplicate descriptions
String description = p.residue.description.toLowerCase();
if ( allDescriptions.contains(description) )
throw new IllegalArgumentException("duplicate description: " + description);
allDescriptions.add(description);
// add to database
AminoAcid aminoAcid = p.residue.aminoAcid;
int index = tempKeys.indexOf(aminoAcid);
List<ProtoAminoAcid> thisList = tempValues.get(index);
thisList.add(p);
}
catch (Exception e)
{
System.out.println("Warning: error reading ProtoAminoAcid from: " + filename);
e.printStackTrace();
}
}
}
// create immutable lists
KEYS = ImmutableList.copyOf(tempKeys);
List<ImmutableList<ProtoAminoAcid>> tempValues2 = new LinkedList<>();
for (List<ProtoAminoAcid> l : tempValues)
tempValues2.add( ImmutableList.copyOf(l) );
VALUES = ImmutableList.copyOf(tempValues2);
}
/**
* This class is not instantiable.
*/
private ProtoAminoAcidDatabase()
{
throw new IllegalArgumentException("not instantiable");
}
/**
* Given an amino acid, returns a list of all the ProtoAminoAcid templates
* available for it. If no templates are available, an empty list will be
* returned.
* @param aminoAcid the amino acid whose templates are desired
* @return the immutable list of possible templates
*/
public static ImmutableList<ProtoAminoAcid> getProtoAminoAcids(AminoAcid aminoAcid)
{
int index = KEYS.indexOf(aminoAcid);
return VALUES.get(index);
}
/**
* Returns a listing of which amino acids are available.
* @return the description
*/
public static String getDescription()
{
int total = 0;
List<AminoAcid> empty = new LinkedList<>();
String returnString = "== ProtoAminoAcidDatabase ==\n";
// enumerate all possibilities
for (int i=0; i < KEYS.size(); i++)
{
AminoAcid aminoAcid = KEYS.get(i);
List<ProtoAminoAcid> currentList = VALUES.get(i);
total += currentList.size();
if ( currentList.size() == 1 )
{
returnString = returnString + currentList.get(0).residue.description;
}
else if ( currentList.size() > 1 )
{
returnString = returnString + aminoAcid.fullName + ":\n";
for (int j=0; j < currentList.size(); j++)
{
ProtoAminoAcid p = currentList.get(j);
returnString = returnString + String.format(" %s", p.residue.description);
if ( j < currentList.size() - 1 )
returnString = returnString + "\n";
}
}
else
empty.add(aminoAcid);
if ( currentList.size() > 0 )
returnString = returnString + "\n";
}
// list empty amino acids
returnString = returnString + "\nAminoAcids without templates:\n";
for (int i=0 ; i < empty.size(); i++)
{
AminoAcid a = empty.get(i);
returnString = returnString + a.fullName;
if ( i < empty.size() - 1 )
returnString = returnString + ", ";
}
returnString = returnString + "\n\n>>> " + total + " total entries";
return returnString;
}
/**
* Returns the ProtoAminoAcids whose descriptions match the given arguments.
* If multiple ProtoAminoAcids match a description or no match is found,
* an error will occur. The comparison is case-insensitive. The whole
* description need not be used. For example, "arg" would be enough to find
* "standard_arginine," assuming there are no other descriptions containing "arg."
* @param sequence a list of Strings containing the description of the requested amino acids
* @return the list of ProtoAminoAcids in the same order as the input sequence
*/
public static List<ProtoAminoAcid> getSpecificSequence(List<String> sequence)
{
// create a map of descriptions to ProtoAminoAcids
Map<String,ProtoAminoAcid> masterMap = new HashMap<String,ProtoAminoAcid>();
for (List<ProtoAminoAcid> list : VALUES)
{
for (ProtoAminoAcid p : list)
{
String description = p.residue.description.toLowerCase();
if ( masterMap.containsKey(description) )
throw new IllegalArgumentException("duplicate description found: " + description);
masterMap.put(description,p);
}
}
List<ProtoAminoAcid> returnList = new LinkedList<>();
for (String s : sequence)
{
String thisDescription = s.toLowerCase();
List<String> matches = new LinkedList<>();
for (String key : masterMap.keySet())
{
if (key.indexOf(thisDescription) > -1)
matches.add(key);
}
if ( matches.size() != 1 )
{
System.out.println(matches);
throw new IllegalArgumentException("invalid number of matches (" + matches.size() + " for " + s + ") -- cannot continue");
}
ProtoAminoAcid hit = masterMap.get(matches.get(0));
returnList.add(hit);
}
return ImmutableList.copyOf(returnList);
}
public static List<ProtoAminoAcid> getSpecificSequence(String... sequence)
{
List<String> list = new ArrayList<>();
for (String s : sequence)
list.add(s);
return getSpecificSequence(list);
}
public static ProtoAminoAcid getTemplate(String string)
{
List<String> list = ImmutableList.of(string);
List<ProtoAminoAcid> results = getSpecificSequence(list);
return results.get(0);
}
/** for testing */
public static void main(String[] args)
{
System.out.println(getDescription());
for ( AminoAcid a : AminoAcid.values() )
{
System.out.println();
System.out.println(a);
System.out.println(getProtoAminoAcids(a));
System.out.println("\n\n");
}
//List<ProtoAminoAcid> testSequence = getSpecificSequence("arg","standard_ala","gly","d_proline", "gly", "l_proline", "val", "hd");
List<ProtoAminoAcid> testSequence = getSpecificSequence("glycine","standard_ala","serine","l_proline");
for (ProtoAminoAcid p : testSequence)
{
Residue residue = p.residue;
System.out.println(p.residue.description);
//if ( residue.HN != null )
//System.out.printf("HN %3d %3d\n", residue.HN.type2, OPLScalculator.getOPLSClass(residue.HN.type2));
//System.out.printf(" N %3d %3d\n", residue.N.type2, OPLScalculator.getOPLSClass(residue.N.type2));
//System.out.printf(" O %3d %3d\n", residue.O.type2, OPLScalculator.getOPLSClass(residue.O.type2));
//System.out.printf(" C %3d %3d\n", residue.C.type2, OPLScalculator.getOPLSClass(residue.C.type2));
//System.out.printf("CA %3d %3d\n", residue.CA.type2, OPLScalculator.getOPLSClass(residue.CA.type2));
//System.out.printf("HA %3d %3d\n", residue.HA.type2, OPLScalculator.getOPLSClass(residue.HA.type2));
}
}
}