2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.sifts;
23 import jalview.analysis.AlignSeq;
24 import jalview.api.DBRefEntryI;
25 import jalview.api.SiftsClientI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.schemes.ResidueProperties;
29 import jalview.structure.StructureMapping;
30 import jalview.util.Format;
31 import jalview.xml.binding.sifts.Entry;
32 import jalview.xml.binding.sifts.Entry.Entity;
33 import jalview.xml.binding.sifts.Entry.Entity.Segment;
34 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListMapRegion.MapRegion;
35 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue;
36 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.CrossRefDb;
37 import jalview.xml.binding.sifts.Entry.ListDB.Db;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.PrintStream;
47 import java.net.URLConnection;
48 import java.util.ArrayList;
49 import java.util.HashSet;
50 import java.util.LinkedHashMap;
51 import java.util.List;
52 import java.util.zip.GZIPInputStream;
54 import javax.xml.bind.JAXBContext;
55 import javax.xml.bind.JAXBException;
56 import javax.xml.bind.Unmarshaller;
57 import javax.xml.stream.FactoryConfigurationError;
58 import javax.xml.stream.XMLInputFactory;
59 import javax.xml.stream.XMLStreamException;
60 import javax.xml.stream.XMLStreamReader;
62 public class SiftsClient implements SiftsClientI
64 private Entry siftsEntry;
68 private String structId;
70 private String segStartEnd;
72 private static final int BUFFER_SIZE = 4096;
74 private static final String SIFTS_FTP_BASE_URL = "ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/";
76 public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System
77 .getProperty("user.home")
79 + ".sifts_downloads" + File.separatorChar;
81 public static final String SIFTS_DOWNLOAD_DIR = jalview.bin.Cache
82 .getDefault("sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR);
84 private final static String NEWLINE = System.lineSeparator();
87 * Fetch SIFTs file for the given PDB Id and construct an instance of
92 public SiftsClient(String pdbId)
97 File siftsFile = getSiftsFile(pdbId);
98 siftsEntry = parseSIFTs(siftsFile);
106 * Construct an instance of SiftsClient using the supplied SIFTs file -
107 * the SIFTs file should correspond to the given PDB Id
112 public SiftsClient(String pdbId, File siftsFile)
117 siftsEntry = parseSIFTs(siftsFile);
118 } catch (Exception e)
126 * Parse the given SIFTs File and return a JAXB POJO of parsed data
129 * - the GZipped SIFTs XML file to parse
132 * if a problem occurs while parsing the SIFTs XML
134 private Entry parseSIFTs(File siftFile) throws Exception
138 System.out.println("File : " + siftFile.getAbsolutePath());
139 JAXBContext jc = JAXBContext.newInstance("jalview.xml.binding.sifts");
140 InputStream in = new FileInputStream(siftFile);
141 GZIPInputStream gzis = new GZIPInputStream(in);
142 XMLStreamReader streamReader = XMLInputFactory.newInstance()
143 .createXMLStreamReader(gzis);
144 Unmarshaller um = jc.createUnmarshaller();
145 return (Entry) um.unmarshal(streamReader);
146 } catch (JAXBException e)
149 } catch (FileNotFoundException e)
152 } catch (XMLStreamException e)
155 } catch (FactoryConfigurationError e)
158 } catch (IOException e)
162 throw new Exception("Error parsing siftFile");
166 * Get a SIFTs XML file for a given PDB Id
169 * @return SIFTs XML file
171 public static File getSiftsFile(String pdbId)
173 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
175 if (siftsFile.exists())
177 // TODO it may be worth performing a timestamp age check to determine if a
178 // new SIFTs file should be re-downloaded as SIFTs entries are usually
180 System.out.println(">>> SIFTS File already downloaded for " + pdbId);
183 siftsFile = downloadSiftsFile(pdbId.toLowerCase());
188 * Download a SIFTs XML file for a given PDB Id
191 * @return downloaded SIFTs XML file
193 public static File downloadSiftsFile(String pdbId)
195 String siftFile = pdbId + ".xml.gz";
196 String siftsFileFTPURL = SIFTS_FTP_BASE_URL + siftFile;
197 String downloadedSiftsFile = SIFTS_DOWNLOAD_DIR + siftFile;
198 File siftsDownloadDir = new File(SIFTS_DOWNLOAD_DIR);
199 if (!siftsDownloadDir.exists())
201 siftsDownloadDir.mkdirs();
205 System.out.println(">> Download ftp url : " + siftsFileFTPURL);
206 URL url = new URL(siftsFileFTPURL);
207 URLConnection conn = url.openConnection();
208 InputStream inputStream = conn.getInputStream();
209 FileOutputStream outputStream = new FileOutputStream(
210 downloadedSiftsFile);
211 byte[] buffer = new byte[BUFFER_SIZE];
213 while ((bytesRead = inputStream.read(buffer)) != -1)
215 outputStream.write(buffer, 0, bytesRead);
217 outputStream.close();
219 System.out.println(">>> File downloaded : " + downloadedSiftsFile);
220 } catch (IOException ex)
222 ex.printStackTrace();
224 return new File(downloadedSiftsFile);
228 * Delete the SIFTs file for the given PDB Id in the local SIFTs download
232 * @return true if the file was deleted or doesn't exist
234 public static boolean deleteSiftsFileByPDBId(String pdbId)
236 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
238 if (siftsFile.exists())
240 return siftsFile.delete();
247 * Get a valid SIFTs DBRef for the given sequence current SIFTs entry
250 * - the target sequence for the operation
251 * @return a valid DBRefEntry that is SIFTs compatible
253 * if no valid source DBRefEntry was found for the given sequences
255 public DBRefEntryI getValidSourceDBRef(SequenceI seq)
256 throws SiftsException
258 DBRefEntryI sourceDBRef = null;
259 sourceDBRef = seq.getSourceDBRef();
260 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
266 DBRefEntry[] dbRefs = seq.getDBRefs();
267 if (dbRefs == null || dbRefs.length < 1)
269 final SequenceI[] seqs = new SequenceI[] { seq };
270 new jalview.ws.DBRefFetcher(seqs, null, null, null, false)
272 dbRefs = seq.getDBRefs();
275 if (dbRefs == null || dbRefs.length < 1)
277 throw new SiftsException("Could not get source DB Ref");
280 for (DBRefEntryI dbRef : dbRefs)
282 if (dbRef == null || dbRef.getAccessionId() == null
283 || dbRef.getSource() == null)
287 if (isFoundInSiftsEntry(dbRef.getAccessionId())
288 && (dbRef.getSource().equalsIgnoreCase("uniprot") || dbRef
289 .getSource().equalsIgnoreCase("pdb")))
295 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
299 throw new SiftsException("Could not get source DB Ref");
304 * Check that the DBRef Entry is properly populated and is available in the
305 * instantiated SIFTs Entry
308 * - DBRefEntry to validate
309 * @return true validation is successful otherwise false is returned.
311 private boolean isValidDBRefEntry(DBRefEntryI entry)
313 return entry != null && entry.getAccessionId() != null
314 && isFoundInSiftsEntry(entry.getAccessionId());
315 // & entry.getStartRes() > 0;
319 public HashSet<String> getAllMappingAccession()
321 HashSet<String> accessions = new HashSet<String>();
322 List<Entity> entities = siftsEntry.getEntity();
323 for (Entity entity : entities)
325 List<Segment> segments = entity.getSegment();
326 for (Segment segment : segments)
328 List<MapRegion> mapRegions = segment.getListMapRegion()
330 for (MapRegion mapRegion : mapRegions)
332 accessions.add(mapRegion.getDb().getDbAccessionId());
340 public StructureMapping getSiftsStructureMapping(SequenceI seq,
341 String pdbFile, String chain) throws SiftsException
343 structId = (chain == null) ? pdbId : pdbId + "|" + chain;
344 System.out.println("Getting mapping for: " + pdbId + "|" + chain
345 + " : seq- " + seq.getName());
347 final StringBuilder mappingDetails = new StringBuilder(128);
348 PrintStream ps = new PrintStream(System.out)
351 public void print(String x)
353 mappingDetails.append(x);
357 public void println()
359 mappingDetails.append(NEWLINE);
362 int[][] mapping = getGreedyMapping(chain, seq, ps);
364 String mappingOutput = mappingDetails.toString();
365 return new StructureMapping(seq, pdbFile, pdbId, chain, mapping,
370 public int[][] getGreedyMapping(String entityId, SequenceI seq,
371 java.io.PrintStream os)
372 throws SiftsException
374 int matchedResStart = -1;
375 int matchedResEnd = -1;
381 boolean startDetected = false;
383 System.out.println("Generating mappings for : " + entityId);
384 Entity entity = null;
385 entity = getEntityById(entityId);
386 String seqStr = AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
387 seq.getSequenceAsString());
388 int mapping[][] = new int[seqStr.length() + seq.getStart()][2];
389 DBRefEntryI sourceDBRef = seq.getSourceDBRef();
390 if (sourceDBRef == null)
392 sourceDBRef = getValidSourceDBRef(seq);
393 // TODO update sequence start/end with sourceDBRef start/end
394 // seq.setStart(sourceDBRef.getStartRes());
395 // seq.setEnd(sourceDBRef.getEndRes());
398 String crossRefAccessionId = sourceDBRef.getAccessionId();
400 for (int residue[] : mapping)
402 residue[1] = count++;
406 LinkedHashMap<Integer, String> resNumMap = new LinkedHashMap<Integer, String>();
407 List<Segment> segments = entity.getSegment();
408 for (Segment segment : segments)
410 segStartEnd = segment.getStart() + " - " + segment.getEnd();
411 System.out.println("Mappging segments : " + segment.getSegId() + "\\"
413 List<Residue> residues = segment.getListResidue().getResidue();
414 for (Residue residue : residues)
416 int refDbResNum = -1;
417 List<CrossRefDb> cRefDbs = residue.getCrossRefDb();
418 for (CrossRefDb cRefDb : cRefDbs)
420 if (cRefDb.getDbAccessionId().equalsIgnoreCase(
421 crossRefAccessionId))
423 refDbResNum = Integer.valueOf(cRefDb.getDbResNum());
426 if (refDbResNum == -1)
431 for (int[] x : mapping)
433 if (loopCount > seq.getStart() && x[1] == refDbResNum)
435 int resNum = Integer.valueOf(residue.getDbResNum());
437 char resCharCode = ResidueProperties
438 .getSingleCharacterCode(residue.getDbResName());
439 resNumMap.put(resNum, String.valueOf(resCharCode));
446 for (int[] x : mapping)
448 if (!startDetected && x[0] > -1)
450 matchedResStart = counter;
451 // System.out.println(matchedResStart);
452 startDetected = true;
455 if (startDetected && x[0] == -1)
457 matchedResEnd = counter;
462 String matchedSeqStr = seqStr;
463 if (matchedResStart != -1)
465 matchedResEnd = (matchedResEnd == -1) ? counter : matchedResEnd;
466 pdbStart = mapping[matchedResStart][0];
467 pdbEnd = mapping[matchedResEnd - 1][0];
468 sStart = mapping[matchedResStart][1];
469 sEnd = mapping[matchedResEnd - 1][1];
470 int seqStart = seq.getStart();
473 matchedResStart = matchedResStart - seqStart;
474 matchedResEnd = matchedResEnd - seqStart;
481 matchedSeqStr = seqStr.substring(matchedResStart, matchedResEnd);
484 StringBuilder targetStrucSeqs = new StringBuilder();
485 for (String res : resNumMap.values())
487 targetStrucSeqs.append(res);
494 MappingOutputPojo mop = new MappingOutputPojo();
495 mop.setSeqStart(sStart);
497 mop.setSeqName(seq.getName());
498 mop.setSeqResidue(matchedSeqStr);
500 mop.setStrStart(pdbStart);
501 mop.setStrEnd(pdbEnd);
502 mop.setStrName(structId);
503 mop.setStrResidue(targetStrucSeqs.toString());
506 os.print(getMappingOutput(mop).toString());
508 } catch (Exception ex)
510 ex.printStackTrace();
516 public boolean isFoundInSiftsEntry(String accessionId)
518 return accessionId != null
519 && getAllMappingAccession().contains(accessionId);
523 public Entity getEntityById(String id) throws SiftsException
525 List<Entity> entities = siftsEntry.getEntity();
526 for (Entity entity : entities)
528 if (!entity.getEntityId().equalsIgnoreCase(id))
534 throw new SiftsException("Entity " + id + " not found");
538 public String[] getEntryDBs()
540 System.out.println("\nListing DB entries...");
541 List<String> availDbs = new ArrayList<String>();
542 List<Db> dbs = siftsEntry.getListDB().getDb();
545 availDbs.add(db.getDbSource());
546 System.out.println(db.getDbSource() + " | " + db.getDbCoordSys());
548 return availDbs.toArray(new String[0]);
552 public StringBuffer getMappingOutput(MappingOutputPojo mp)
554 String seqRes = mp.getSeqResidue();
555 String seqName = mp.getSeqName();
556 int sStart = mp.getSeqStart();
557 int sEnd = mp.getSeqEnd();
559 String strRes = mp.getStrResidue();
560 String strName = mp.getStrName();
561 int pdbStart = mp.getStrStart();
562 int pdbEnd = mp.getStrEnd();
564 String type = mp.getType();
566 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
568 int len = 72 - maxid - 1;
570 int nochunks = ((seqRes.length()) / len)
571 + ((seqRes.length()) % len > 0 ? 1 : 0);
573 StringBuffer output = new StringBuffer();
574 output.append(NEWLINE);
575 output.append("Sequence ⟷ Structure mapping details:");
576 output.append(NEWLINE).append(NEWLINE);
578 output.append(new Format("%" + maxid + "s").form(seqName));
579 output.append(" : ");
580 output.append(String.valueOf(sStart));
581 output.append(" - ");
582 output.append(String.valueOf(sEnd));
583 output.append(" Maps to ");
584 output.append(NEWLINE);
585 output.append(new Format("%" + maxid + "s").form(structId));
586 output.append(" : ");
587 output.append(String.valueOf(pdbStart));
588 output.append(" - ");
589 output.append(String.valueOf(pdbEnd));
590 output.append(NEWLINE).append(NEWLINE);
593 for (int j = 0; j < nochunks; j++)
595 // Print the first aligned sequence
596 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
599 for (int i = 0; i < len; i++)
601 if ((i + (j * len)) < seqRes.length())
603 output.append(seqRes.charAt(i + (j * len)));
607 output.append(NEWLINE);
608 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
610 // Print out the matching chars
611 for (int i = 0; i < len; i++)
613 if ((i + (j * len)) < seqRes.length())
615 if (seqRes.charAt(i + (j * len)) == strRes.charAt(i + (j * len))
616 && !jalview.util.Comparison.isGap(seqRes.charAt(i
622 else if (type.equals("pep"))
624 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
625 strRes.charAt(i + (j * len))) > 0)
640 // Now print the second aligned sequence
641 output = output.append(NEWLINE);
642 output = output.append(new Format("%" + (maxid) + "s").form(strName))
644 for (int i = 0; i < len; i++)
646 if ((i + (j * len)) < strRes.length())
648 output.append(strRes.charAt(i + (j * len)));
651 output.append(NEWLINE).append(NEWLINE);
653 pid = pid / (seqRes.length()) * 100;
654 output.append("Length of alignment = " + seqRes.length())
656 output.append(new Format("Percentage ID = %2.2f").form(pid));
657 output.append(NEWLINE);
658 output.append("Mapping method: SIFTS").append(NEWLINE);
663 public int getEntityCount()
665 return siftsEntry.getEntity().size();
669 public String getDbAccessionId()
671 return siftsEntry.getDbAccessionId();
675 public String getDbCoordSys()
677 return siftsEntry.getDbCoordSys();
681 public String getDbEvidence()
683 return siftsEntry.getDbEvidence();
687 public String getDbSource()
689 return siftsEntry.getDbSource();
693 public String getDbVersion()
695 return siftsEntry.getDbVersion();