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.DBRefSource;
28 import jalview.datamodel.SequenceI;
29 import jalview.schemes.ResidueProperties;
30 import jalview.structure.StructureMapping;
31 import jalview.util.Format;
32 import jalview.xml.binding.sifts.Entry;
33 import jalview.xml.binding.sifts.Entry.Entity;
34 import jalview.xml.binding.sifts.Entry.Entity.Segment;
35 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListMapRegion.MapRegion;
36 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue;
37 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.CrossRefDb;
38 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.ResidueDetail;
39 import jalview.xml.binding.sifts.Entry.ListDB.Db;
42 import java.io.FileInputStream;
43 import java.io.FileNotFoundException;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.PrintStream;
49 import java.net.URLConnection;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.HashSet;
54 import java.util.List;
55 import java.util.TreeMap;
56 import java.util.zip.GZIPInputStream;
58 import javax.xml.bind.JAXBContext;
59 import javax.xml.bind.JAXBException;
60 import javax.xml.bind.Unmarshaller;
61 import javax.xml.stream.FactoryConfigurationError;
62 import javax.xml.stream.XMLInputFactory;
63 import javax.xml.stream.XMLStreamException;
64 import javax.xml.stream.XMLStreamReader;
67 import MCview.PDBChain;
68 import MCview.PDBfile;
70 public class SiftsClient implements SiftsClientI
72 private Entry siftsEntry;
78 private String structId;
80 private String segStartEnd;
82 private CoordinateSys seqCoordSys = CoordinateSys.UNIPROT;
84 private static final int BUFFER_SIZE = 4096;
86 public static final int UNASSIGNED = -1;
88 private static final int PDB_RES_POS = 0;
90 private static final int PDB_ATOM_POS = 1;
92 private static final String SIFTS_FTP_BASE_URL = "ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/";
94 public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System
95 .getProperty("user.home")
97 + ".sifts_downloads" + File.separatorChar;
99 public static final String SIFTS_DOWNLOAD_DIR = jalview.bin.Cache
100 .getDefault("sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR);
102 private final static String NEWLINE = System.lineSeparator();
104 private String curSourceDBRef;
106 private HashSet<String> curDBRefAccessionIdsString;
108 public enum CoordinateSys
110 UNIPROT("UniProt"), PDB("PDBresnum"), PDBe("PDBe");
113 private CoordinateSys(String name)
118 public String getName()
124 public enum ResidueDetailType
126 NAME_SEC_STRUCTURE("nameSecondaryStructure"), CODE_SEC_STRUCTURE(
127 "codeSecondaryStructure"), ANNOTATION("Annotation");
130 private ResidueDetailType(String code)
135 public String getCode()
142 * Fetch SIFTs file for the given PDB Id and construct an instance of
146 * @throws SiftsException
148 public SiftsClient(PDBfile pdb) throws SiftsException
152 File siftsFile = getSiftsFile(pdbId);
153 siftsEntry = parseSIFTs(siftsFile);
157 * Construct an instance of SiftsClient using the supplied SIFTs file - the
158 * SIFTs file should correspond to the given PDB Id
162 * @throws SiftsException
165 public SiftsClient(PDBfile pdb, File siftsFile) throws SiftsException
169 siftsEntry = parseSIFTs(siftsFile);
173 * Parse the given SIFTs File and return a JAXB POJO of parsed data
176 * - the GZipped SIFTs XML file to parse
179 * if a problem occurs while parsing the SIFTs XML
181 private Entry parseSIFTs(File siftFile) throws SiftsException
185 System.out.println("File : " + siftFile.getAbsolutePath());
186 JAXBContext jc = JAXBContext.newInstance("jalview.xml.binding.sifts");
187 InputStream in = new FileInputStream(siftFile);
188 GZIPInputStream gzis = new GZIPInputStream(in);
189 XMLStreamReader streamReader = XMLInputFactory.newInstance()
190 .createXMLStreamReader(gzis);
191 Unmarshaller um = jc.createUnmarshaller();
192 return (Entry) um.unmarshal(streamReader);
193 } catch (JAXBException e)
196 throw new SiftsException(e.getMessage());
197 } catch (FileNotFoundException e)
200 throw new SiftsException(e.getMessage());
201 } catch (XMLStreamException e)
204 throw new SiftsException(e.getMessage());
205 } catch (FactoryConfigurationError e)
208 throw new SiftsException(e.getMessage());
209 } catch (IOException e)
212 throw new SiftsException(e.getMessage());
217 * Get a SIFTs XML file for a given PDB Id
220 * @return SIFTs XML file
221 * @throws SiftsException
223 public static File getSiftsFile(String pdbId) throws SiftsException
225 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
227 if (siftsFile.exists())
229 // TODO it may be worth performing an age check to determine if a
230 // new SIFTs file should be re-downloaded as SIFTs entries are usually
232 System.out.println(">>> SIFTS File already downloaded for " + pdbId);
235 siftsFile = downloadSiftsFile(pdbId.toLowerCase());
240 * Download a SIFTs XML file for a given PDB Id
243 * @return downloaded SIFTs XML file
244 * @throws SiftsException
246 public static File downloadSiftsFile(String pdbId) throws SiftsException
248 String siftFile = pdbId + ".xml.gz";
249 String siftsFileFTPURL = SIFTS_FTP_BASE_URL + siftFile;
250 String downloadedSiftsFile = SIFTS_DOWNLOAD_DIR + siftFile;
251 File siftsDownloadDir = new File(SIFTS_DOWNLOAD_DIR);
252 if (!siftsDownloadDir.exists())
254 siftsDownloadDir.mkdirs();
258 System.out.println(">> Download ftp url : " + siftsFileFTPURL);
259 URL url = new URL(siftsFileFTPURL);
260 URLConnection conn = url.openConnection();
261 InputStream inputStream = conn.getInputStream();
262 FileOutputStream outputStream = new FileOutputStream(
263 downloadedSiftsFile);
264 byte[] buffer = new byte[BUFFER_SIZE];
266 while ((bytesRead = inputStream.read(buffer)) != -1)
268 outputStream.write(buffer, 0, bytesRead);
270 outputStream.close();
272 System.out.println(">>> File downloaded : " + downloadedSiftsFile);
273 } catch (IOException ex)
275 throw new SiftsException(ex.getMessage());
277 return new File(downloadedSiftsFile);
281 * Delete the SIFTs file for the given PDB Id in the local SIFTs download
285 * @return true if the file was deleted or doesn't exist
287 public static boolean deleteSiftsFileByPDBId(String pdbId)
289 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
291 if (siftsFile.exists())
293 return siftsFile.delete();
300 * Get a valid SIFTs DBRef for the given sequence current SIFTs entry
303 * - the target sequence for the operation
304 * @return a valid DBRefEntry that is SIFTs compatible
306 * if no valid source DBRefEntry was found for the given sequences
308 public DBRefEntryI getValidSourceDBRef(SequenceI seq)
309 throws SiftsException
311 DBRefEntryI sourceDBRef = null;
312 sourceDBRef = seq.getSourceDBRef();
313 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
319 DBRefEntry[] dbRefs = seq.getDBRefs();
320 if (dbRefs == null || dbRefs.length < 1)
322 final SequenceI[] seqs = new SequenceI[] { seq };
323 new jalview.ws.DBRefFetcher(seqs, null, null, null, false)
325 dbRefs = seq.getDBRefs();
328 if (dbRefs == null || dbRefs.length < 1)
330 throw new SiftsException("Could not get source DB Ref");
333 for (DBRefEntryI dbRef : dbRefs)
335 if (dbRef == null || dbRef.getAccessionId() == null
336 || dbRef.getSource() == null)
340 if (isFoundInSiftsEntry(dbRef.getAccessionId())
341 && (dbRef.getSource().equalsIgnoreCase(DBRefSource.UNIPROT) || dbRef
342 .getSource().equalsIgnoreCase(DBRefSource.PDB)))
348 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
352 throw new SiftsException("Could not get source DB Ref");
357 * Check that the DBRef Entry is properly populated and is available in the
358 * instantiated SIFTs Entry
361 * - DBRefEntry to validate
362 * @return true validation is successful otherwise false is returned.
364 private boolean isValidDBRefEntry(DBRefEntryI entry)
366 return entry != null && entry.getAccessionId() != null
367 && isFoundInSiftsEntry(entry.getAccessionId());
371 public HashSet<String> getAllMappingAccession()
373 HashSet<String> accessions = new HashSet<String>();
374 List<Entity> entities = siftsEntry.getEntity();
375 for (Entity entity : entities)
377 List<Segment> segments = entity.getSegment();
378 for (Segment segment : segments)
380 List<MapRegion> mapRegions = segment.getListMapRegion()
382 for (MapRegion mapRegion : mapRegions)
384 accessions.add(mapRegion.getDb().getDbAccessionId());
392 public StructureMapping getSiftsStructureMapping(SequenceI seq,
393 String pdbFile, String chain) throws SiftsException
395 structId = (chain == null) ? pdbId : pdbId + "|" + chain;
396 System.out.println("Getting mapping for: " + pdbId + "|" + chain
397 + " : seq- " + seq.getName());
399 final StringBuilder mappingDetails = new StringBuilder(128);
400 PrintStream ps = new PrintStream(System.out)
403 public void print(String x)
405 mappingDetails.append(x);
409 public void println()
411 mappingDetails.append(NEWLINE);
414 int[][] mapping = getGreedyMapping(chain, seq, ps);
416 String mappingOutput = mappingDetails.toString();
417 StructureMapping siftsMapping = new StructureMapping(seq, pdbFile,
418 pdbId, chain, mapping,
424 public int[][] getGreedyMapping(String entityId, SequenceI seq,
425 java.io.PrintStream os)
426 throws SiftsException
428 System.out.println("Generating mappings for : " + entityId);
429 Entity entity = null;
430 entity = getEntityById(entityId);
431 String originalSeq = AlignSeq.extractGaps(
432 jalview.util.Comparison.GapChars,
433 seq.getSequenceAsString());
434 int mapping[][] = new int[originalSeq.length() + seq.getStart()][2];
435 DBRefEntryI sourceDBRef = seq.getSourceDBRef();
436 if (sourceDBRef == null)
438 sourceDBRef = getValidSourceDBRef(seq);
439 // TODO ensure sequence start/end is in the same coordinate system and
440 // consistent with the choosen sourceDBRef
443 // set sequence coordinate system - default value is UniProt
444 if (sourceDBRef.getSource().equalsIgnoreCase(DBRefSource.PDB))
446 seqCoordSys = CoordinateSys.PDB;
449 HashSet<String> dbRefAccessionIdsString = new HashSet<String>();
450 for (DBRefEntry dbref : seq.getDBRefs())
452 dbRefAccessionIdsString.add(dbref.getAccessionId().toLowerCase());
454 dbRefAccessionIdsString.add(sourceDBRef.getAccessionId().toLowerCase());
456 curDBRefAccessionIdsString = dbRefAccessionIdsString;
457 curSourceDBRef = sourceDBRef.getAccessionId();
459 // initialise all mapping positions to unassigned
460 for (int residuePos[] : mapping)
462 residuePos[PDB_RES_POS] = UNASSIGNED;
463 residuePos[PDB_ATOM_POS] = UNASSIGNED;
465 TreeMap<Integer, String> resNumMap = new TreeMap<Integer, String>();
466 List<Segment> segments = entity.getSegment();
467 for (Segment segment : segments)
469 segStartEnd = segment.getStart() + " - " + segment.getEnd();
470 System.out.println("Mappging segments : " + segment.getSegId() + "\\"
472 List<Residue> residues = segment.getListResidue().getResidue();
473 for (Residue residue : residues)
475 int currSeqIndex = UNASSIGNED;
476 List<CrossRefDb> cRefDbs = residue.getCrossRefDb();
477 CrossRefDb pdbRefDb = null;
478 for (CrossRefDb cRefDb : cRefDbs)
480 if (cRefDb.getDbSource().equalsIgnoreCase(DBRefSource.PDB))
484 if (cRefDb.getDbCoordSys()
485 .equalsIgnoreCase(seqCoordSys.getName())
486 && hasAccessionId(cRefDb.getDbAccessionId()))
488 String resNumIndexString = cRefDb.getDbResNum()
489 .equalsIgnoreCase("None") ? String.valueOf(UNASSIGNED)
490 : cRefDb.getDbResNum();
491 currSeqIndex = Integer.valueOf(resNumIndexString);
492 if (pdbRefDb != null)
494 break;// exit loop if pdb and uniprot are already found
498 if (currSeqIndex == UNASSIGNED)
502 if (currSeqIndex > seq.getStart() && currSeqIndex <= seq.getEnd())
507 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
508 .getDbResNum()) : Integer.valueOf(pdbRefDb.getDbResNum());
509 } catch (NumberFormatException nfe)
511 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
512 .getDbResNum()) : Integer.valueOf(pdbRefDb
513 .getDbResNum().split("[a-zA-Z]")[0]);
517 mapping[currSeqIndex][PDB_RES_POS] = Integer
519 } catch (ArrayIndexOutOfBoundsException e)
523 char resCharCode = ResidueProperties
524 .getSingleCharacterCode(residue.getDbResName());
525 resNumMap.put(currSeqIndex, String.valueOf(resCharCode));
531 populateAtomPositions(entityId, mapping);
532 } catch (Exception e)
536 padWithGaps(resNumMap);
538 int seqStart = UNASSIGNED;
539 int seqEnd = UNASSIGNED;
540 int pdbStart = UNASSIGNED;
541 int pdbEnd = UNASSIGNED;
542 boolean startDetected = false;
543 for (int[] x : mapping)
545 if (!startDetected && x[PDB_RES_POS] != UNASSIGNED)
548 startDetected = true;
549 // System.out.println("Seq start: "+ seqStart);
552 if (startDetected && x[PDB_RES_POS] != UNASSIGNED)
559 String matchedSeq = originalSeq;
560 if (seqStart != UNASSIGNED)
562 seqEnd = (seqEnd == UNASSIGNED) ? counter : seqEnd;
563 pdbStart = mapping[seqStart][PDB_RES_POS];
564 pdbEnd = mapping[seqEnd][PDB_RES_POS];
565 int orignalSeqStart = seq.getStart();
566 if (orignalSeqStart >= 1)
568 int subSeqStart = seqStart - orignalSeqStart;
569 int subSeqEnd = seqEnd - (orignalSeqStart - 1);
570 matchedSeq = originalSeq.substring(subSeqStart, subSeqEnd);
574 StringBuilder targetStrucSeqs = new StringBuilder();
575 for (String res : resNumMap.values())
577 targetStrucSeqs.append(res);
582 MappingOutputPojo mop = new MappingOutputPojo();
583 mop.setSeqStart(seqStart);
584 mop.setSeqEnd(seqEnd);
585 mop.setSeqName(seq.getName());
586 mop.setSeqResidue(matchedSeq);
588 mop.setStrStart(pdbStart);
589 mop.setStrEnd(pdbEnd);
590 mop.setStrName(structId);
591 mop.setStrResidue(targetStrucSeqs.toString());
594 os.print(getMappingOutput(mop).toString());
599 private boolean isResidueObserved(Residue residue)
601 String annotation = getResidueAnnotaiton(residue,
602 ResidueDetailType.ANNOTATION);
603 if (annotation == null)
607 if (!annotation.equalsIgnoreCase("Not_Found")
608 && annotation.equalsIgnoreCase("Not_Observed"))
615 private String getResidueAnnotaiton(Residue residue,
616 ResidueDetailType type)
618 List<ResidueDetail> resDetails = residue.getResidueDetail();
619 for (ResidueDetail resDetail : resDetails)
621 if (resDetail.getProperty().equalsIgnoreCase(type.getCode()))
623 return resDetail.getContent();
629 private boolean hasAccessionId(String accession)
631 boolean isStrictMatch = true;
632 return isStrictMatch ? curSourceDBRef.equalsIgnoreCase(accession)
633 : curDBRefAccessionIdsString.contains(accession.toLowerCase());
637 public boolean isFoundInSiftsEntry(String accessionId)
639 return accessionId != null
640 && getAllMappingAccession().contains(accessionId);
644 * Pads missing positions with gaps
648 void padWithGaps(TreeMap<Integer, String> resNumMap)
650 if (resNumMap == null || resNumMap.isEmpty())
654 Integer[] keys = resNumMap.keySet().toArray(new Integer[0]);
656 int firstIndex = keys[0];
657 int lastIndex = keys[keys.length - 1];
658 System.out.println("Min value " + firstIndex);
659 System.out.println("Max value " + lastIndex);
660 for (int x = firstIndex; x <= lastIndex; x++)
662 if (!resNumMap.containsKey(x))
664 resNumMap.put(x, "-");
672 * Target chain to populate mapping of its atom positions.
674 * Two dimension array of residue index versus atom position
675 * @throws IllegalArgumentException
676 * Thrown if chainId or mapping is null
678 void populateAtomPositions(String chainId, int[][] mapping)
679 throws IllegalArgumentException
681 PDBChain chain = pdb.findChain(chainId);
682 if (chain == null || mapping == null)
684 throw new IllegalArgumentException(
685 "Chain id or mapping must not be null.");
687 for (int[] map : mapping)
689 if (map[PDB_RES_POS] != UNASSIGNED)
691 map[PDB_ATOM_POS] = getAtomIndex(map[PDB_RES_POS], chain.atoms);
698 * @param residueIndex
699 * The residue index used for the search
701 * A collection of Atom to search
702 * @return atom position for the given residue index
704 int getAtomIndex(int residueIndex, Collection<Atom> atoms)
708 throw new IllegalArgumentException(
709 "atoms collection must not be null!");
711 for (Atom atom : atoms)
713 if (atom.resNumber == residueIndex)
715 return atom.atomIndex;
722 public Entity getEntityById(String id) throws SiftsException
724 List<Entity> entities = siftsEntry.getEntity();
725 for (Entity entity : entities)
727 if (!entity.getEntityId().equalsIgnoreCase(id))
733 throw new SiftsException("Entity " + id + " not found");
737 public String[] getEntryDBs()
739 System.out.println("\nListing DB entries...");
740 List<String> availDbs = new ArrayList<String>();
741 List<Db> dbs = siftsEntry.getListDB().getDb();
744 availDbs.add(db.getDbSource());
745 System.out.println(db.getDbSource() + " | " + db.getDbCoordSys());
747 return availDbs.toArray(new String[0]);
751 public StringBuffer getMappingOutput(MappingOutputPojo mp)
752 throws SiftsException
754 String seqRes = mp.getSeqResidue();
755 String seqName = mp.getSeqName();
756 int sStart = mp.getSeqStart();
757 int sEnd = mp.getSeqEnd();
759 String strRes = mp.getStrResidue();
760 String strName = mp.getStrName();
761 int pdbStart = mp.getStrStart();
762 int pdbEnd = mp.getStrEnd();
764 String type = mp.getType();
766 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
768 int len = 72 - maxid - 1;
770 int nochunks = ((seqRes.length()) / len)
771 + ((seqRes.length()) % len > 0 ? 1 : 0);
773 StringBuffer output = new StringBuffer();
774 output.append(NEWLINE);
775 output.append("Sequence ⟷ Structure mapping details").append(NEWLINE);
776 output.append("Method: SIFTS");
777 output.append(NEWLINE).append(NEWLINE);
779 output.append(new Format("%" + maxid + "s").form(seqName));
780 output.append(" : ");
781 output.append(String.valueOf(sStart));
782 output.append(" - ");
783 output.append(String.valueOf(sEnd));
784 output.append(" Maps to ");
785 output.append(NEWLINE);
786 output.append(new Format("%" + maxid + "s").form(structId));
787 output.append(" : ");
788 output.append(String.valueOf(pdbStart));
789 output.append(" - ");
790 output.append(String.valueOf(pdbEnd));
791 output.append(NEWLINE).append(NEWLINE);
793 int matchedSeqCount = 0;
794 for (int j = 0; j < nochunks; j++)
796 // Print the first aligned sequence
797 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
800 for (int i = 0; i < len; i++)
802 if ((i + (j * len)) < seqRes.length())
804 output.append(seqRes.charAt(i + (j * len)));
808 output.append(NEWLINE);
809 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
811 // Print out the matching chars
812 for (int i = 0; i < len; i++)
816 if ((i + (j * len)) < seqRes.length())
818 if (seqRes.charAt(i + (j * len)) == strRes.charAt(i + (j * len))
819 && !jalview.util.Comparison.isGap(seqRes.charAt(i
825 else if (type.equals("pep"))
827 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
828 strRes.charAt(i + (j * len))) > 0)
842 } catch (IndexOutOfBoundsException e)
847 // Now print the second aligned sequence
848 output = output.append(NEWLINE);
849 output = output.append(new Format("%" + (maxid) + "s").form(strName))
851 for (int i = 0; i < len; i++)
853 if ((i + (j * len)) < strRes.length())
855 output.append(strRes.charAt(i + (j * len)));
858 output.append(NEWLINE).append(NEWLINE);
860 float pid = (float) matchedSeqCount / seqRes.length() * 100;
863 throw new SiftsException("Low PID detected for SIFTs mapping...");
865 output.append("Length of alignment = " + seqRes.length())
867 output.append(new Format("Percentage ID = %2.2f").form(pid));
868 output.append(NEWLINE);
873 public int getEntityCount()
875 return siftsEntry.getEntity().size();
879 public String getDbAccessionId()
881 return siftsEntry.getDbAccessionId();
885 public String getDbCoordSys()
887 return siftsEntry.getDbCoordSys();
891 public String getDbEvidence()
893 return siftsEntry.getDbEvidence();
897 public String getDbSource()
899 return siftsEntry.getDbSource();
903 public String getDbVersion()
905 return siftsEntry.getDbVersion();