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.HashMap;
54 import java.util.HashSet;
55 import java.util.List;
56 import java.util.TreeMap;
57 import java.util.zip.GZIPInputStream;
59 import javax.xml.bind.JAXBContext;
60 import javax.xml.bind.JAXBException;
61 import javax.xml.bind.Unmarshaller;
62 import javax.xml.stream.FactoryConfigurationError;
63 import javax.xml.stream.XMLInputFactory;
64 import javax.xml.stream.XMLStreamException;
65 import javax.xml.stream.XMLStreamReader;
68 import MCview.PDBChain;
69 import MCview.PDBfile;
71 public class SiftsClient implements SiftsClientI
73 private Entry siftsEntry;
79 private String structId;
81 private String segStartEnd;
83 private CoordinateSys seqCoordSys = CoordinateSys.UNIPROT;
85 private static final int BUFFER_SIZE = 4096;
87 public static final int UNASSIGNED = -1;
89 private static final int PDB_RES_POS = 0;
91 private static final int PDB_ATOM_POS = 1;
93 private static final String NOT_FOUND = "Not_Found";
95 private static final String NOT_OBSERVED = "Not_Observed";
97 private static final String SIFTS_FTP_BASE_URL = "ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/";
99 public static final String DEFAULT_SIFTS_DOWNLOAD_DIR = System
100 .getProperty("user.home")
102 + ".sifts_downloads" + File.separatorChar;
104 public static final String SIFTS_DOWNLOAD_DIR = jalview.bin.Cache
105 .getDefault("sifts_download_dir", DEFAULT_SIFTS_DOWNLOAD_DIR);
107 private final static String NEWLINE = System.lineSeparator();
109 private String curSourceDBRef;
111 private HashSet<String> curDBRefAccessionIdsString;
113 public enum CoordinateSys
115 UNIPROT("UniProt"), PDB("PDBresnum"), PDBe("PDBe");
118 private CoordinateSys(String name)
123 public String getName()
129 public enum ResidueDetailType
131 NAME_SEC_STRUCTURE("nameSecondaryStructure"), CODE_SEC_STRUCTURE(
132 "codeSecondaryStructure"), ANNOTATION("Annotation");
135 private ResidueDetailType(String code)
140 public String getCode()
147 * Fetch SIFTs file for the given PDBfile and construct an instance of
151 * @throws SiftsException
153 public SiftsClient(PDBfile pdb) throws SiftsException
157 File siftsFile = getSiftsFile(pdbId);
158 siftsEntry = parseSIFTs(siftsFile);
162 * Construct an instance of SiftsClient using the supplied SIFTs file. Note:
163 * The SIFTs file should correspond to the PDB Id in PDBfile instance
167 * @throws SiftsException
170 public SiftsClient(PDBfile pdb, File siftsFile) throws SiftsException
174 siftsEntry = parseSIFTs(siftsFile);
178 * Parse the given SIFTs File and return a JAXB POJO of parsed data
181 * - the GZipped SIFTs XML file to parse
184 * if a problem occurs while parsing the SIFTs XML
186 private Entry parseSIFTs(File siftFile) throws SiftsException
188 try (InputStream in = new FileInputStream(siftFile);
189 GZIPInputStream gzis = new GZIPInputStream(in);)
191 System.out.println("File : " + siftFile.getAbsolutePath());
192 JAXBContext jc = JAXBContext.newInstance("jalview.xml.binding.sifts");
193 XMLStreamReader streamReader = XMLInputFactory.newInstance()
194 .createXMLStreamReader(gzis);
195 Unmarshaller um = jc.createUnmarshaller();
196 return (Entry) um.unmarshal(streamReader);
197 } catch (JAXBException e)
200 throw new SiftsException(e.getMessage());
201 } catch (FileNotFoundException e)
204 throw new SiftsException(e.getMessage());
205 } catch (XMLStreamException e)
208 throw new SiftsException(e.getMessage());
209 } catch (FactoryConfigurationError e)
212 throw new SiftsException(e.getMessage());
213 } catch (IOException e)
216 throw new SiftsException(e.getMessage());
221 * Get a SIFTs XML file for a given PDB Id from Cache or download from FTP
222 * repository if not found in cache
225 * @return SIFTs XML file
226 * @throws SiftsException
228 public static File getSiftsFile(String pdbId) throws SiftsException
230 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
232 if (siftsFile.exists())
234 // TODO it may be worth performing an age check to determine if a
235 // new SIFTs file should be re-downloaded as SIFTs entries are usually
237 System.out.println(">>> SIFTS File already downloaded for " + pdbId);
240 siftsFile = downloadSiftsFile(pdbId.toLowerCase());
245 * Download a SIFTs XML file for a given PDB Id from an FTP repository
248 * @return downloaded SIFTs XML file
249 * @throws SiftsException
251 public static File downloadSiftsFile(String pdbId) throws SiftsException
253 String siftFile = pdbId + ".xml.gz";
254 String siftsFileFTPURL = SIFTS_FTP_BASE_URL + siftFile;
255 String downloadedSiftsFile = SIFTS_DOWNLOAD_DIR + siftFile;
256 File siftsDownloadDir = new File(SIFTS_DOWNLOAD_DIR);
257 if (!siftsDownloadDir.exists())
259 siftsDownloadDir.mkdirs();
263 System.out.println(">> Download ftp url : " + siftsFileFTPURL);
264 URL url = new URL(siftsFileFTPURL);
265 URLConnection conn = url.openConnection();
266 InputStream inputStream = conn.getInputStream();
267 FileOutputStream outputStream = new FileOutputStream(
268 downloadedSiftsFile);
269 byte[] buffer = new byte[BUFFER_SIZE];
271 while ((bytesRead = inputStream.read(buffer)) != -1)
273 outputStream.write(buffer, 0, bytesRead);
275 outputStream.close();
277 System.out.println(">>> File downloaded : " + downloadedSiftsFile);
278 } catch (IOException ex)
280 throw new SiftsException(ex.getMessage());
282 return new File(downloadedSiftsFile);
286 * Delete the SIFTs file for the given PDB Id in the local SIFTs download
290 * @return true if the file was deleted or doesn't exist
292 public static boolean deleteSiftsFileByPDBId(String pdbId)
294 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
296 if (siftsFile.exists())
298 return siftsFile.delete();
305 * Get a valid SIFTs DBRef for the given sequence current SIFTs entry
308 * - the target sequence for the operation
309 * @return a valid DBRefEntry that is SIFTs compatible
311 * if no valid source DBRefEntry was found for the given sequences
313 public DBRefEntryI getValidSourceDBRef(SequenceI seq)
314 throws SiftsException
316 DBRefEntryI sourceDBRef = null;
317 sourceDBRef = seq.getSourceDBRef();
318 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
324 DBRefEntry[] dbRefs = seq.getDBRefs();
325 if (dbRefs == null || dbRefs.length < 1)
327 // final SequenceI[] seqs = new SequenceI[] { seq };
328 // new jalview.ws.DBRefFetcher(seqs, null, null, null, false)
329 // .fetchDBRefs(true);
330 // dbRefs = seq.getDBRefs();
333 if (dbRefs == null || dbRefs.length < 1)
335 throw new SiftsException("Could not get source DB Ref");
338 for (DBRefEntryI dbRef : dbRefs)
340 if (dbRef == null || dbRef.getAccessionId() == null
341 || dbRef.getSource() == null)
345 if (isFoundInSiftsEntry(dbRef.getAccessionId())
346 && (dbRef.getSource().equalsIgnoreCase(DBRefSource.UNIPROT) || dbRef
347 .getSource().equalsIgnoreCase(DBRefSource.PDB)))
353 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
357 throw new SiftsException("Could not get source DB Ref");
362 * Check that the DBRef Entry is properly populated and is available in this
363 * SiftClient instance
366 * - DBRefEntry to validate
367 * @return true validation is successful otherwise false is returned.
369 private boolean isValidDBRefEntry(DBRefEntryI entry)
371 return entry != null && entry.getAccessionId() != null
372 && isFoundInSiftsEntry(entry.getAccessionId());
376 public HashSet<String> getAllMappingAccession()
378 HashSet<String> accessions = new HashSet<String>();
379 List<Entity> entities = siftsEntry.getEntity();
380 for (Entity entity : entities)
382 List<Segment> segments = entity.getSegment();
383 for (Segment segment : segments)
385 List<MapRegion> mapRegions = segment.getListMapRegion()
387 for (MapRegion mapRegion : mapRegions)
389 accessions.add(mapRegion.getDb().getDbAccessionId());
397 public StructureMapping getSiftsStructureMapping(SequenceI seq,
398 String pdbFile, String chain) throws SiftsException
400 structId = (chain == null) ? pdbId : pdbId + "|" + chain;
401 System.out.println("Getting mapping for: " + pdbId + "|" + chain
402 + " : seq- " + seq.getName());
404 final StringBuilder mappingDetails = new StringBuilder(128);
405 PrintStream ps = new PrintStream(System.out)
408 public void print(String x)
410 mappingDetails.append(x);
414 public void println()
416 mappingDetails.append(NEWLINE);
419 HashMap<Integer, int[]> mapping = getGreedyMapping(chain, seq, ps);
421 String mappingOutput = mappingDetails.toString();
422 StructureMapping siftsMapping = new StructureMapping(seq, pdbFile,
423 pdbId, chain, mapping,
429 public HashMap<Integer, int[]> getGreedyMapping(String entityId, SequenceI seq,
430 java.io.PrintStream os)
431 throws SiftsException
433 ArrayList<Integer> omitNonObserved = new ArrayList<Integer>();
434 int nonObservedShiftIndex = 0;
435 System.out.println("Generating mappings for : " + entityId);
436 Entity entity = null;
437 entity = getEntityById(entityId);
438 String originalSeq = AlignSeq.extractGaps(
439 jalview.util.Comparison.GapChars,
440 seq.getSequenceAsString());
441 HashMap<Integer, int[]> mapping = new HashMap<Integer, int[]>();
442 DBRefEntryI sourceDBRef = seq.getSourceDBRef();
443 if (sourceDBRef == null)
445 sourceDBRef = getValidSourceDBRef(seq);
446 // TODO ensure sequence start/end is in the same coordinate system and
447 // consistent with the choosen sourceDBRef
450 // set sequence coordinate system - default value is UniProt
451 if (sourceDBRef.getSource().equalsIgnoreCase(DBRefSource.PDB))
453 seqCoordSys = CoordinateSys.PDB;
456 HashSet<String> dbRefAccessionIdsString = new HashSet<String>();
457 for (DBRefEntry dbref : seq.getDBRefs())
459 dbRefAccessionIdsString.add(dbref.getAccessionId().toLowerCase());
461 dbRefAccessionIdsString.add(sourceDBRef.getAccessionId().toLowerCase());
463 curDBRefAccessionIdsString = dbRefAccessionIdsString;
464 curSourceDBRef = sourceDBRef.getAccessionId();
466 TreeMap<Integer, String> resNumMap = new TreeMap<Integer, String>();
467 List<Segment> segments = entity.getSegment();
468 for (Segment segment : segments)
470 segStartEnd = segment.getStart() + " - " + segment.getEnd();
471 System.out.println("Mappging segments : " + segment.getSegId() + "\\"
473 List<Residue> residues = segment.getListResidue().getResidue();
474 for (Residue residue : residues)
476 int currSeqIndex = UNASSIGNED;
477 List<CrossRefDb> cRefDbs = residue.getCrossRefDb();
478 CrossRefDb pdbRefDb = null;
479 for (CrossRefDb cRefDb : cRefDbs)
481 if (cRefDb.getDbSource().equalsIgnoreCase(DBRefSource.PDB))
485 if (cRefDb.getDbCoordSys()
486 .equalsIgnoreCase(seqCoordSys.getName())
487 && isAccessionMatched(cRefDb.getDbAccessionId()))
489 String resNumIndexString = cRefDb.getDbResNum()
490 .equalsIgnoreCase("None") ? String.valueOf(UNASSIGNED)
491 : cRefDb.getDbResNum();
492 currSeqIndex = Integer.valueOf(resNumIndexString);
493 if (pdbRefDb != null)
495 break;// exit loop if pdb and uniprot are already found
499 if (currSeqIndex == UNASSIGNED)
503 if (currSeqIndex > seq.getStart() && currSeqIndex <= seq.getEnd())
508 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
509 .getDbResNum()) : Integer.valueOf(pdbRefDb.getDbResNum());
510 } catch (NumberFormatException nfe)
512 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
513 .getDbResNum()) : Integer.valueOf(pdbRefDb
514 .getDbResNum().split("[a-zA-Z]")[0]);
517 if (isResidueObserved(residue)
518 || seqCoordSys == CoordinateSys.UNIPROT)
520 char resCharCode = ResidueProperties
521 .getSingleCharacterCode(residue.getDbResName());
522 resNumMap.put(currSeqIndex, String.valueOf(resCharCode));
526 omitNonObserved.add(currSeqIndex);
527 ++nonObservedShiftIndex;
529 mapping.put(currSeqIndex - nonObservedShiftIndex, new int[] {
530 Integer.valueOf(resNum), UNASSIGNED });
536 populateAtomPositions(entityId, mapping);
537 } catch (Exception e)
541 padWithGaps(resNumMap, omitNonObserved);
542 int seqStart = UNASSIGNED;
543 int seqEnd = UNASSIGNED;
544 int pdbStart = UNASSIGNED;
545 int pdbEnd = UNASSIGNED;
547 Integer[] keys = mapping.keySet().toArray(new Integer[0]);
550 seqEnd = keys[keys.length - 1];
552 String matchedSeq = originalSeq;
553 if (seqStart != UNASSIGNED)
555 pdbStart = mapping.get(seqStart)[PDB_RES_POS];
556 pdbEnd = mapping.get(seqEnd)[PDB_RES_POS];
557 int orignalSeqStart = seq.getStart();
558 if (orignalSeqStart >= 1)
560 int subSeqStart = seqStart - orignalSeqStart;
561 int subSeqEnd = seqEnd - (orignalSeqStart - 1);
562 subSeqEnd = originalSeq.length() < subSeqEnd ? originalSeq.length()
564 matchedSeq = originalSeq.substring(subSeqStart, subSeqEnd);
568 StringBuilder targetStrucSeqs = new StringBuilder();
569 for (String res : resNumMap.values())
571 targetStrucSeqs.append(res);
576 MappingOutputPojo mop = new MappingOutputPojo();
577 mop.setSeqStart(seqStart);
578 mop.setSeqEnd(seqEnd);
579 mop.setSeqName(seq.getName());
580 mop.setSeqResidue(matchedSeq);
582 mop.setStrStart(pdbStart);
583 mop.setStrEnd(pdbEnd);
584 mop.setStrName(structId);
585 mop.setStrResidue(targetStrucSeqs.toString());
588 os.print(getMappingOutput(mop).toString());
594 * Checks if the residue instance is marked 'Not_observed' or not
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))
616 * Get annotation String for a given residue and annotation type
622 private String getResidueAnnotaiton(Residue residue,
623 ResidueDetailType type)
625 List<ResidueDetail> resDetails = residue.getResidueDetail();
626 for (ResidueDetail resDetail : resDetails)
628 if (resDetail.getProperty().equalsIgnoreCase(type.getCode()))
630 return resDetail.getContent();
637 public boolean isAccessionMatched(String accession)
639 boolean isStrictMatch = true;
640 return isStrictMatch ? curSourceDBRef.equalsIgnoreCase(accession)
641 : curDBRefAccessionIdsString.contains(accession.toLowerCase());
644 private boolean isFoundInSiftsEntry(String accessionId)
646 return accessionId != null
647 && getAllMappingAccession().contains(accessionId);
651 * Pad omitted residue positions in PDB sequence with gaps
655 void padWithGaps(TreeMap<Integer, String> resNumMap,
656 ArrayList<Integer> omitNonObserved)
658 if (resNumMap == null || resNumMap.isEmpty())
662 Integer[] keys = resNumMap.keySet().toArray(new Integer[0]);
664 int firstIndex = keys[0];
665 int lastIndex = keys[keys.length - 1];
666 System.out.println("Min value " + firstIndex);
667 System.out.println("Max value " + lastIndex);
668 for (int x = firstIndex; x <= lastIndex; x++)
670 if (!resNumMap.containsKey(x) && !omitNonObserved.contains(x))
672 resNumMap.put(x, "-");
681 * Target chain to populate mapping of its atom positions.
683 * Two dimension array of residue index versus atom position
684 * @throws IllegalArgumentException
685 * Thrown if chainId or mapping is null
687 void populateAtomPositions(String chainId, HashMap<Integer, int[]> mapping)
688 throws IllegalArgumentException
690 PDBChain chain = pdb.findChain(chainId);
691 if (chain == null || mapping == null)
693 throw new IllegalArgumentException(
694 "Chain id or mapping must not be null.");
696 for (int[] map : mapping.values())
698 if (map[PDB_RES_POS] != UNASSIGNED)
700 map[PDB_ATOM_POS] = getAtomIndex(map[PDB_RES_POS], chain.atoms);
707 * @param residueIndex
708 * The residue index used for the search
710 * A collection of Atom to search
711 * @return atom position for the given residue index
713 int getAtomIndex(int residueIndex, Collection<Atom> atoms)
717 throw new IllegalArgumentException(
718 "atoms collection must not be null!");
720 for (Atom atom : atoms)
722 if (atom.resNumber == residueIndex)
724 return atom.atomIndex;
731 public Entity getEntityById(String id) throws SiftsException
733 List<Entity> entities = siftsEntry.getEntity();
734 for (Entity entity : entities)
736 if (!entity.getEntityId().equalsIgnoreCase(id))
742 throw new SiftsException("Entity " + id + " not found");
746 public String[] getEntryDBs()
748 System.out.println("\nListing DB entries...");
749 List<String> availDbs = new ArrayList<String>();
750 List<Db> dbs = siftsEntry.getListDB().getDb();
753 availDbs.add(db.getDbSource());
754 System.out.println(db.getDbSource() + " | " + db.getDbCoordSys());
756 return availDbs.toArray(new String[0]);
760 public StringBuffer getMappingOutput(MappingOutputPojo mp)
761 throws SiftsException
763 String seqRes = mp.getSeqResidue();
764 String seqName = mp.getSeqName();
765 int sStart = mp.getSeqStart();
766 int sEnd = mp.getSeqEnd();
768 String strRes = mp.getStrResidue();
769 String strName = mp.getStrName();
770 int pdbStart = mp.getStrStart();
771 int pdbEnd = mp.getStrEnd();
773 String type = mp.getType();
775 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
777 int len = 72 - maxid - 1;
779 int nochunks = ((seqRes.length()) / len)
780 + ((seqRes.length()) % len > 0 ? 1 : 0);
782 StringBuffer output = new StringBuffer();
783 output.append(NEWLINE);
784 output.append("Sequence ⟷ Structure mapping details").append(NEWLINE);
785 output.append("Method: SIFTS");
786 output.append(NEWLINE).append(NEWLINE);
788 output.append(new Format("%" + maxid + "s").form(seqName));
789 output.append(" : ");
790 output.append(String.valueOf(sStart));
791 output.append(" - ");
792 output.append(String.valueOf(sEnd));
793 output.append(" Maps to ");
794 output.append(NEWLINE);
795 output.append(new Format("%" + maxid + "s").form(structId));
796 output.append(" : ");
797 output.append(String.valueOf(pdbStart));
798 output.append(" - ");
799 output.append(String.valueOf(pdbEnd));
800 output.append(NEWLINE).append(NEWLINE);
802 int matchedSeqCount = 0;
803 for (int j = 0; j < nochunks; j++)
805 // Print the first aligned sequence
806 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
809 for (int i = 0; i < len; i++)
811 if ((i + (j * len)) < seqRes.length())
813 output.append(seqRes.charAt(i + (j * len)));
817 output.append(NEWLINE);
818 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
820 // Print out the matching chars
821 for (int i = 0; i < len; i++)
825 if ((i + (j * len)) < seqRes.length())
827 if (seqRes.charAt(i + (j * len)) == strRes.charAt(i + (j * len))
828 && !jalview.util.Comparison.isGap(seqRes.charAt(i
834 else if (type.equals("pep"))
836 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
837 strRes.charAt(i + (j * len))) > 0)
851 } catch (IndexOutOfBoundsException e)
856 // Now print the second aligned sequence
857 output = output.append(NEWLINE);
858 output = output.append(new Format("%" + (maxid) + "s").form(strName))
860 for (int i = 0; i < len; i++)
862 if ((i + (j * len)) < strRes.length())
864 output.append(strRes.charAt(i + (j * len)));
867 output.append(NEWLINE).append(NEWLINE);
869 float pid = (float) matchedSeqCount / seqRes.length() * 100;
872 throw new SiftsException("Low PID detected for SIFTs mapping...");
874 output.append("Length of alignment = " + seqRes.length())
876 output.append(new Format("Percentage ID = %2.2f").form(pid));
877 output.append(NEWLINE);
882 public int getEntityCount()
884 return siftsEntry.getEntity().size();
888 public String getDbAccessionId()
890 return siftsEntry.getDbAccessionId();
894 public String getDbCoordSys()
896 return siftsEntry.getDbCoordSys();
900 public String getDbEvidence()
902 return siftsEntry.getDbEvidence();
906 public String getDbSource()
908 return siftsEntry.getDbSource();
912 public String getDbVersion()
914 return siftsEntry.getDbVersion();