@@ -7,10 +7,228 @@ How to serialize a protein structure to a database
77
88There is a little project that provides
99[ Hibernate] ( http://www.hibernate.org ) mappings for the protein structure
10- classes at
11- [ < http://www.spice-3d.org/hibernatePDB/ > ] ( http://www.spice-3d.org/hibernatePDB/ ) .
12- It mainly consists of the Hibernate mappings files,with a few Java
13- helper classes.
10+ classes at [ BioJava Structure
11+ Hibernate] ( http://www.spice-3d.org/hibernatePDB/ ) . It mainly consists of
12+ the Hibernate mappings files,with a few Java helper classes.
1413
15- I am not sure if this should be joint with the core BioJava
16- CVS. --[ Andreas] ( User:Andreas " wikilink ") 17:42, 8 October 2007 (EDT)
14+ I am not sure if this code should be joint with the core BioJava CVS,
15+ but then BioJava should not directly depend on
16+ Hibernate... --[ Andreas] ( User:Andreas " wikilink ") 17:42, 8 October 2007
17+ (EDT)
18+
19+ If you have BioJava Structure Hibernate installed, you can do something
20+ like:
21+
22+ See also the [ example
23+ page] ( http://www.spice-3d.org/hibernatePDB/examples.jsp )
24+
25+ <java >
26+
27+ import org.biojava.bio.structure.Chain; import
28+ org.biojava.bio.structure.Compound; import
29+ org.biojava.bio.structure.Group; import
30+ org.biojava.bio.structure.Structure; import
31+ org.biojava.bio.structure.hibernate.HibernateUtil; import
32+ org.biojava.bio.structure.hibernate.StructureFile; import
33+ org.biojava.bio.structure.io.PDBFileReader; import
34+ org.hibernate.HibernateException; import org.hibernate.Session; import
35+ java.io.File; import java.io.FileFilter; import java.util.ArrayList;
36+ import java.util.List;
37+
38+ /\*\* A class that finds all PDB files from the filesystem and stores
39+ them in the database.
40+
41+ ` * A current release of PDB (2007 - September) takes about one night for an upload `
42+ ` * (without the atoms). `
43+ ` * `
44+ ` * `
45+ ` */ `
46+
47+ public class DemoWritePDBFiles {
48+
49+ ` public static void main(String[] args) { `
50+ ` `
51+ ` // init log4j `
52+ ` //org.apache.log4j.BasicConfigurator.configure(); `
53+
54+ ` if (args.length < 1 ) { `
55+ ` System.err.println("please provide PDB directory as argument"); `
56+ ` System.exit(0); `
57+ ` } `
58+
59+ // init the installation
60+
61+ ` File pdbLocation = new File(args[0]); `
62+
63+ ` System.out.println("searching in " + pdbLocation); `
64+
65+ ` // init the demo class `
66+ ` DemoWritePDBFiles demo = new DemoWritePDBFiles(); `
67+ ` `
68+ ` // now we find all PDB files under the provided directory `
69+ ` List ` <File >` pdbfiles = demo.getAllPDB(pdbLocation); `
70+ ` System.out.println("serializing "+ pdbfiles.size() + " PDB files..."); `
71+ ` try { `
72+ ` demo.storeFiles(pdbfiles); `
73+ ` } catch (Exception e) { `
74+ ` e.printStackTrace(); `
75+ ` } `
76+ ` `
77+ ` } `
78+
79+ ` /** get all PDBfiles from a directory `
80+ ` * `
81+ ` * @param dir the directory where the PDB files are located `
82+ ` * @return all PDB files `
83+ ` */ `
84+ ` public List ` <File >` getAllPDB(File dir) { `
85+
86+ ` List ` <File >` allpdbs = new ArrayList ` <File >` (); `
87+
88+ ` if ( ! dir.isDirectory()){ `
89+ ` throw new IllegalArgumentException("path is not a directory " + dir); `
90+ ` } `
91+
92+ ` // This filter only returns directories `
93+ ` FileFilter fileFilter = new FileFilter() { `
94+ ` public boolean accept(File file) { `
95+ ` return file.isDirectory(); `
96+ ` } `
97+ ` }; `
98+ ` File[] subfiles = dir.listFiles(fileFilter); `
99+
100+ ` for (File f: subfiles){ `
101+ ` List ` <File >` pdbs = getAllPDB(f); `
102+ ` allpdbs.addAll(pdbs); `
103+
104+ ` } `
105+
106+ ` String[] all = dir.list(); `
107+
108+ ` for (int i = 0 ; i < all.length;i++ ){ `
109+ ` // filenames are like 'pdb1234.ent.gz' `
110+ ` String file = all[i]; `
111+ ` if ( (file.endsWith(".pdb.gz")) || `
112+ ` ( file.endsWith(".ent.gz")) || `
113+ ` (file.endsWith(".pdb")) || `
114+ ` (file.endsWith(".ent")) `
115+ ` ){ `
116+ ` allpdbs.add(new File(dir+File.separator + file)); `
117+ ` } `
118+ ` } `
119+
120+ ` return allpdbs; `
121+ ` } `
122+
123+ ` /** upload the set of PDB files into the database `
124+ ` * `
125+ ` * @param pdbfiles - list of PDBFiles `
126+ ` * @throws HibernateException `
127+ ` */ `
128+ ` public void storeFiles(List ` <File >` pdbfiles) throws HibernateException{ `
129+ ` //Object ownership = HibernateSession.createSession(); `
130+ ` //Session session = HibernateSession.getSession(); `
131+ ` int l = pdbfiles.size(); `
132+
133+ ` //long loopStart = System.currentTimeMillis(); `
134+ ` PDBFileReader pdbreader = new PDBFileReader(); `
135+
136+ ` int i=0; `
137+ ` for (File f: pdbfiles){ `
138+ ` i++; `
139+ ` `
140+ ` System.out.println(f); `
141+ ` StructureFile struFile = new StructureFile(); `
142+ ` struFile.setPath(f.toString()); `
143+
144+ ` System.out.println("# "+i + " / " + l + " " + f); `
145+ ` try { `
146+ ` `
147+ ` // associate the biojava Structure container class with `
148+ ` // the location of the PDB file in the filesystem `
149+ ` Structure s = pdbreader.getStructure(f); `
150+ ` struFile.setPDBCode(s.getPDBCode()); `
151+ ` struFile.setStructure(s); `
152+
153+ ` // now we write it `
154+ ` createAndStoreStructure(struFile); `
155+
156+ ` } catch (Exception e){ `
157+ ` e.printStackTrace(); `
158+ ` } `
159+
160+ ` } `
161+ ` } `
162+
163+ ` /** does the actual Hibernate serialisation `
164+ ` * `
165+ ` * @param struFile `
166+ ` */ `
167+ ` private void createAndStoreStructure(StructureFile struFile ) { `
168+ ` //System.out.println(s); `
169+
170+ ` Structure s = struFile.getStructure(); `
171+
172+ ` // open a new Hibernate session `
173+ ` Session session = HibernateUtil.getSessionFactory().openSession(); `
174+ ` session.beginTransaction(); `
175+
176+ ` //System.err.println("saving struc"); `
177+ ` for (Compound compound : s.getCompounds()){ `
178+ ` session.save(compound); `
179+ ` } `
180+ ` `
181+ ` // save the toplevel container `
182+ ` session.saveOrUpdate(s); `
183+ ` `
184+ ` `
185+ ` // save the file path `
186+ ` session.saveOrUpdate(struFile); `
187+ ` `
188+ ` for (Chain chain:s.getChains(0)){ `
189+ ` `
190+ ` session.saveOrUpdate(chain); `
191+ ` `
192+ ` for (Group g: chain.getAtomGroups()){ `
193+ ` g.setParent(chain); `
194+ ` session.saveOrUpdate(g); `
195+ ` `
196+ ` /* at the moment writing Atoms is very slow, `
197+ ` * it needs some more optimization ... :-( `
198+ ` * patches are welcome! `
199+ ` * `
200+ ` * Therefore by default writing the Atoms is disabled. `
201+ ` * if you want to do that, uncomment these lines, as well as the `
202+ ` * lines in HetatomImpl.hbm.xml `
203+ ` */ `
204+ ` //for ( Atom a: g.getAtoms()) `
205+ ` // session.saveOrUpdate(a); `
206+ ` `
207+ ` `
208+ ` } `
209+ ` //System.err.println("saving groups seqres"); `
210+ ` for (Group g: chain.getSeqResGroups()){ `
211+ ` g.setParent(chain); `
212+ ` session.saveOrUpdate(g); `
213+ ` `
214+ ` /* see above `
215+ ` */ `
216+ ` //for ( Atom a: g.getAtoms()) `
217+ ` // session.saveOrUpdate(a); `
218+ ` `
219+ ` } `
220+ ` } `
221+ ` `
222+ ` `
223+ ` `
224+ ` session.flush(); `
225+ ` `
226+ ` session.getTransaction().commit(); `
227+ ` session.clear(); `
228+ ` `
229+ ` HibernateUtil.getSessionFactory().close(); `
230+ ` `
231+
232+ ` } `
233+
234+ } </java >
0 commit comments