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
190 System.out.println("File : " + siftFile.getAbsolutePath());
191 JAXBContext jc = JAXBContext.newInstance("jalview.xml.binding.sifts");
192 InputStream in = new FileInputStream(siftFile);
193 GZIPInputStream gzis = new GZIPInputStream(in);
194 XMLStreamReader streamReader = XMLInputFactory.newInstance()
195 .createXMLStreamReader(gzis);
196 Unmarshaller um = jc.createUnmarshaller();
197 return (Entry) um.unmarshal(streamReader);
198 } catch (JAXBException e)
201 throw new SiftsException(e.getMessage());
202 } catch (FileNotFoundException e)
205 throw new SiftsException(e.getMessage());
206 } catch (XMLStreamException e)
209 throw new SiftsException(e.getMessage());
210 } catch (FactoryConfigurationError e)
213 throw new SiftsException(e.getMessage());
214 } catch (IOException e)
217 throw new SiftsException(e.getMessage());
222 * Get a SIFTs XML file for a given PDB Id from Cache or download from FTP
223 * repository if not found in cache
226 * @return SIFTs XML file
227 * @throws SiftsException
229 public static File getSiftsFile(String pdbId) throws SiftsException
231 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
233 if (siftsFile.exists())
235 // TODO it may be worth performing an age check to determine if a
236 // new SIFTs file should be re-downloaded as SIFTs entries are usually
238 System.out.println(">>> SIFTS File already downloaded for " + pdbId);
241 siftsFile = downloadSiftsFile(pdbId.toLowerCase());
246 * Download a SIFTs XML file for a given PDB Id from an FTP repository
249 * @return downloaded SIFTs XML file
250 * @throws SiftsException
252 public static File downloadSiftsFile(String pdbId) throws SiftsException
254 String siftFile = pdbId + ".xml.gz";
255 String siftsFileFTPURL = SIFTS_FTP_BASE_URL + siftFile;
256 String downloadedSiftsFile = SIFTS_DOWNLOAD_DIR + siftFile;
257 File siftsDownloadDir = new File(SIFTS_DOWNLOAD_DIR);
258 if (!siftsDownloadDir.exists())
260 siftsDownloadDir.mkdirs();
264 System.out.println(">> Download ftp url : " + siftsFileFTPURL);
265 URL url = new URL(siftsFileFTPURL);
266 URLConnection conn = url.openConnection();
267 InputStream inputStream = conn.getInputStream();
268 FileOutputStream outputStream = new FileOutputStream(
269 downloadedSiftsFile);
270 byte[] buffer = new byte[BUFFER_SIZE];
272 while ((bytesRead = inputStream.read(buffer)) != -1)
274 outputStream.write(buffer, 0, bytesRead);
276 outputStream.close();
278 System.out.println(">>> File downloaded : " + downloadedSiftsFile);
279 } catch (IOException ex)
281 throw new SiftsException(ex.getMessage());
283 return new File(downloadedSiftsFile);
287 * Delete the SIFTs file for the given PDB Id in the local SIFTs download
291 * @return true if the file was deleted or doesn't exist
293 public static boolean deleteSiftsFileByPDBId(String pdbId)
295 File siftsFile = new File(SIFTS_DOWNLOAD_DIR + pdbId.toLowerCase()
297 if (siftsFile.exists())
299 return siftsFile.delete();
306 * Get a valid SIFTs DBRef for the given sequence current SIFTs entry
309 * - the target sequence for the operation
310 * @return a valid DBRefEntry that is SIFTs compatible
312 * if no valid source DBRefEntry was found for the given sequences
314 public DBRefEntryI getValidSourceDBRef(SequenceI seq)
315 throws SiftsException
317 DBRefEntryI sourceDBRef = null;
318 sourceDBRef = seq.getSourceDBRef();
319 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
325 DBRefEntry[] dbRefs = seq.getDBRefs();
326 if (dbRefs == null || dbRefs.length < 1)
328 final SequenceI[] seqs = new SequenceI[] { seq };
329 new jalview.ws.DBRefFetcher(seqs, null, null, null, false)
331 dbRefs = seq.getDBRefs();
334 if (dbRefs == null || dbRefs.length < 1)
336 throw new SiftsException("Could not get source DB Ref");
339 for (DBRefEntryI dbRef : dbRefs)
341 if (dbRef == null || dbRef.getAccessionId() == null
342 || dbRef.getSource() == null)
346 if (isFoundInSiftsEntry(dbRef.getAccessionId())
347 && (dbRef.getSource().equalsIgnoreCase(DBRefSource.UNIPROT) || dbRef
348 .getSource().equalsIgnoreCase(DBRefSource.PDB)))
354 if (sourceDBRef != null && isValidDBRefEntry(sourceDBRef))
358 throw new SiftsException("Could not get source DB Ref");
363 * Check that the DBRef Entry is properly populated and is available in this
364 * SiftClient instance
367 * - DBRefEntry to validate
368 * @return true validation is successful otherwise false is returned.
370 private boolean isValidDBRefEntry(DBRefEntryI entry)
372 return entry != null && entry.getAccessionId() != null
373 && isFoundInSiftsEntry(entry.getAccessionId());
377 public HashSet<String> getAllMappingAccession()
379 HashSet<String> accessions = new HashSet<String>();
380 List<Entity> entities = siftsEntry.getEntity();
381 for (Entity entity : entities)
383 List<Segment> segments = entity.getSegment();
384 for (Segment segment : segments)
386 List<MapRegion> mapRegions = segment.getListMapRegion()
388 for (MapRegion mapRegion : mapRegions)
390 accessions.add(mapRegion.getDb().getDbAccessionId());
398 public StructureMapping getSiftsStructureMapping(SequenceI seq,
399 String pdbFile, String chain) throws SiftsException
401 structId = (chain == null) ? pdbId : pdbId + "|" + chain;
402 System.out.println("Getting mapping for: " + pdbId + "|" + chain
403 + " : seq- " + seq.getName());
405 final StringBuilder mappingDetails = new StringBuilder(128);
406 PrintStream ps = new PrintStream(System.out)
409 public void print(String x)
411 mappingDetails.append(x);
415 public void println()
417 mappingDetails.append(NEWLINE);
420 HashMap<Integer, int[]> mapping = getGreedyMapping(chain, seq, ps);
422 String mappingOutput = mappingDetails.toString();
423 StructureMapping siftsMapping = new StructureMapping(seq, pdbFile,
424 pdbId, chain, mapping,
430 public HashMap<Integer, int[]> getGreedyMapping(String entityId, SequenceI seq,
431 java.io.PrintStream os)
432 throws SiftsException
434 ArrayList<Integer> omitNonObserved = new ArrayList<Integer>();
435 int nonObservedShiftIndex = 0;
436 System.out.println("Generating mappings for : " + entityId);
437 Entity entity = null;
438 entity = getEntityById(entityId);
439 String originalSeq = AlignSeq.extractGaps(
440 jalview.util.Comparison.GapChars,
441 seq.getSequenceAsString());
442 HashMap<Integer, int[]> mapping = new HashMap<Integer, int[]>();
443 DBRefEntryI sourceDBRef = seq.getSourceDBRef();
444 if (sourceDBRef == null)
446 sourceDBRef = getValidSourceDBRef(seq);
447 // TODO ensure sequence start/end is in the same coordinate system and
448 // consistent with the choosen sourceDBRef
451 // set sequence coordinate system - default value is UniProt
452 if (sourceDBRef.getSource().equalsIgnoreCase(DBRefSource.PDB))
454 seqCoordSys = CoordinateSys.PDB;
457 HashSet<String> dbRefAccessionIdsString = new HashSet<String>();
458 for (DBRefEntry dbref : seq.getDBRefs())
460 dbRefAccessionIdsString.add(dbref.getAccessionId().toLowerCase());
462 dbRefAccessionIdsString.add(sourceDBRef.getAccessionId().toLowerCase());
464 curDBRefAccessionIdsString = dbRefAccessionIdsString;
465 curSourceDBRef = sourceDBRef.getAccessionId();
467 TreeMap<Integer, String> resNumMap = new TreeMap<Integer, String>();
468 List<Segment> segments = entity.getSegment();
469 for (Segment segment : segments)
471 segStartEnd = segment.getStart() + " - " + segment.getEnd();
472 System.out.println("Mappging segments : " + segment.getSegId() + "\\"
474 List<Residue> residues = segment.getListResidue().getResidue();
475 for (Residue residue : residues)
477 int currSeqIndex = UNASSIGNED;
478 List<CrossRefDb> cRefDbs = residue.getCrossRefDb();
479 CrossRefDb pdbRefDb = null;
480 for (CrossRefDb cRefDb : cRefDbs)
482 if (cRefDb.getDbSource().equalsIgnoreCase(DBRefSource.PDB))
486 if (cRefDb.getDbCoordSys()
487 .equalsIgnoreCase(seqCoordSys.getName())
488 && isAccessionMatched(cRefDb.getDbAccessionId()))
490 String resNumIndexString = cRefDb.getDbResNum()
491 .equalsIgnoreCase("None") ? String.valueOf(UNASSIGNED)
492 : cRefDb.getDbResNum();
493 currSeqIndex = Integer.valueOf(resNumIndexString);
494 if (pdbRefDb != null)
496 break;// exit loop if pdb and uniprot are already found
500 if (currSeqIndex == UNASSIGNED)
504 if (currSeqIndex > seq.getStart() && currSeqIndex <= seq.getEnd())
509 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
510 .getDbResNum()) : Integer.valueOf(pdbRefDb.getDbResNum());
511 } catch (NumberFormatException nfe)
513 resNum = (pdbRefDb == null) ? Integer.valueOf(residue
514 .getDbResNum()) : Integer.valueOf(pdbRefDb
515 .getDbResNum().split("[a-zA-Z]")[0]);
518 if (isResidueObserved(residue)
519 || seqCoordSys == CoordinateSys.UNIPROT)
521 char resCharCode = ResidueProperties
522 .getSingleCharacterCode(residue.getDbResName());
523 resNumMap.put(currSeqIndex, String.valueOf(resCharCode));
527 omitNonObserved.add(currSeqIndex);
528 ++nonObservedShiftIndex;
530 mapping.put(currSeqIndex - nonObservedShiftIndex, new int[] {
531 Integer.valueOf(resNum), UNASSIGNED });
537 populateAtomPositions(entityId, mapping);
538 } catch (Exception e)
542 padWithGaps(resNumMap, omitNonObserved);
543 int seqStart = UNASSIGNED;
544 int seqEnd = UNASSIGNED;
545 int pdbStart = UNASSIGNED;
546 int pdbEnd = UNASSIGNED;
548 Integer[] keys = mapping.keySet().toArray(new Integer[0]);
551 seqEnd = keys[keys.length - 1];
553 String matchedSeq = originalSeq;
554 if (seqStart != UNASSIGNED)
556 pdbStart = mapping.get(seqStart)[PDB_RES_POS];
557 pdbEnd = mapping.get(seqEnd)[PDB_RES_POS];
558 int orignalSeqStart = seq.getStart();
559 if (orignalSeqStart >= 1)
561 int subSeqStart = seqStart - orignalSeqStart;
562 int subSeqEnd = seqEnd - (orignalSeqStart - 1);
563 subSeqEnd = originalSeq.length() < subSeqEnd ? originalSeq.length()
565 matchedSeq = originalSeq.substring(subSeqStart, subSeqEnd);
569 StringBuilder targetStrucSeqs = new StringBuilder();
570 for (String res : resNumMap.values())
572 targetStrucSeqs.append(res);
577 MappingOutputPojo mop = new MappingOutputPojo();
578 mop.setSeqStart(seqStart);
579 mop.setSeqEnd(seqEnd);
580 mop.setSeqName(seq.getName());
581 mop.setSeqResidue(matchedSeq);
583 mop.setStrStart(pdbStart);
584 mop.setStrEnd(pdbEnd);
585 mop.setStrName(structId);
586 mop.setStrResidue(targetStrucSeqs.toString());
589 os.print(getMappingOutput(mop).toString());
595 * Checks if the residue instance is marked 'Not_observed' or not
600 private boolean isResidueObserved(Residue residue)
602 String annotation = getResidueAnnotaiton(residue,
603 ResidueDetailType.ANNOTATION);
604 if (annotation == null)
608 if (!annotation.equalsIgnoreCase(NOT_FOUND)
609 && annotation.equalsIgnoreCase(NOT_OBSERVED))
617 * Get annotation String for a given residue and annotation type
623 private String getResidueAnnotaiton(Residue residue,
624 ResidueDetailType type)
626 List<ResidueDetail> resDetails = residue.getResidueDetail();
627 for (ResidueDetail resDetail : resDetails)
629 if (resDetail.getProperty().equalsIgnoreCase(type.getCode()))
631 return resDetail.getContent();
638 public boolean isAccessionMatched(String accession)
640 boolean isStrictMatch = true;
641 return isStrictMatch ? curSourceDBRef.equalsIgnoreCase(accession)
642 : curDBRefAccessionIdsString.contains(accession.toLowerCase());
645 private boolean isFoundInSiftsEntry(String accessionId)
647 return accessionId != null
648 && getAllMappingAccession().contains(accessionId);
652 * Pad omitted residue positions in PDB sequence with gaps
656 void padWithGaps(TreeMap<Integer, String> resNumMap,
657 ArrayList<Integer> omitNonObserved)
659 if (resNumMap == null || resNumMap.isEmpty())
663 Integer[] keys = resNumMap.keySet().toArray(new Integer[0]);
665 int firstIndex = keys[0];
666 int lastIndex = keys[keys.length - 1];
667 System.out.println("Min value " + firstIndex);
668 System.out.println("Max value " + lastIndex);
669 for (int x = firstIndex; x <= lastIndex; x++)
671 if (!resNumMap.containsKey(x) && !omitNonObserved.contains(x))
673 resNumMap.put(x, "-");
682 * Target chain to populate mapping of its atom positions.
684 * Two dimension array of residue index versus atom position
685 * @throws IllegalArgumentException
686 * Thrown if chainId or mapping is null
688 void populateAtomPositions(String chainId, HashMap<Integer, int[]> mapping)
689 throws IllegalArgumentException
691 PDBChain chain = pdb.findChain(chainId);
692 if (chain == null || mapping == null)
694 throw new IllegalArgumentException(
695 "Chain id or mapping must not be null.");
697 for (int[] map : mapping.values())
699 if (map[PDB_RES_POS] != UNASSIGNED)
701 map[PDB_ATOM_POS] = getAtomIndex(map[PDB_RES_POS], chain.atoms);
708 * @param residueIndex
709 * The residue index used for the search
711 * A collection of Atom to search
712 * @return atom position for the given residue index
714 int getAtomIndex(int residueIndex, Collection<Atom> atoms)
718 throw new IllegalArgumentException(
719 "atoms collection must not be null!");
721 for (Atom atom : atoms)
723 if (atom.resNumber == residueIndex)
725 return atom.atomIndex;
732 public Entity getEntityById(String id) throws SiftsException
734 List<Entity> entities = siftsEntry.getEntity();
735 for (Entity entity : entities)
737 if (!entity.getEntityId().equalsIgnoreCase(id))
743 throw new SiftsException("Entity " + id + " not found");
747 public String[] getEntryDBs()
749 System.out.println("\nListing DB entries...");
750 List<String> availDbs = new ArrayList<String>();
751 List<Db> dbs = siftsEntry.getListDB().getDb();
754 availDbs.add(db.getDbSource());
755 System.out.println(db.getDbSource() + " | " + db.getDbCoordSys());
757 return availDbs.toArray(new String[0]);
761 public StringBuffer getMappingOutput(MappingOutputPojo mp)
762 throws SiftsException
764 String seqRes = mp.getSeqResidue();
765 String seqName = mp.getSeqName();
766 int sStart = mp.getSeqStart();
767 int sEnd = mp.getSeqEnd();
769 String strRes = mp.getStrResidue();
770 String strName = mp.getStrName();
771 int pdbStart = mp.getStrStart();
772 int pdbEnd = mp.getStrEnd();
774 String type = mp.getType();
776 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
778 int len = 72 - maxid - 1;
780 int nochunks = ((seqRes.length()) / len)
781 + ((seqRes.length()) % len > 0 ? 1 : 0);
783 StringBuffer output = new StringBuffer();
784 output.append(NEWLINE);
785 output.append("Sequence ⟷ Structure mapping details").append(NEWLINE);
786 output.append("Method: SIFTS");
787 output.append(NEWLINE).append(NEWLINE);
789 output.append(new Format("%" + maxid + "s").form(seqName));
790 output.append(" : ");
791 output.append(String.valueOf(sStart));
792 output.append(" - ");
793 output.append(String.valueOf(sEnd));
794 output.append(" Maps to ");
795 output.append(NEWLINE);
796 output.append(new Format("%" + maxid + "s").form(structId));
797 output.append(" : ");
798 output.append(String.valueOf(pdbStart));
799 output.append(" - ");
800 output.append(String.valueOf(pdbEnd));
801 output.append(NEWLINE).append(NEWLINE);
803 int matchedSeqCount = 0;
804 for (int j = 0; j < nochunks; j++)
806 // Print the first aligned sequence
807 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
810 for (int i = 0; i < len; i++)
812 if ((i + (j * len)) < seqRes.length())
814 output.append(seqRes.charAt(i + (j * len)));
818 output.append(NEWLINE);
819 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
821 // Print out the matching chars
822 for (int i = 0; i < len; i++)
826 if ((i + (j * len)) < seqRes.length())
828 if (seqRes.charAt(i + (j * len)) == strRes.charAt(i + (j * len))
829 && !jalview.util.Comparison.isGap(seqRes.charAt(i
835 else if (type.equals("pep"))
837 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
838 strRes.charAt(i + (j * len))) > 0)
852 } catch (IndexOutOfBoundsException e)
857 // Now print the second aligned sequence
858 output = output.append(NEWLINE);
859 output = output.append(new Format("%" + (maxid) + "s").form(strName))
861 for (int i = 0; i < len; i++)
863 if ((i + (j * len)) < strRes.length())
865 output.append(strRes.charAt(i + (j * len)));
868 output.append(NEWLINE).append(NEWLINE);
870 float pid = (float) matchedSeqCount / seqRes.length() * 100;
873 throw new SiftsException("Low PID detected for SIFTs mapping...");
875 output.append("Length of alignment = " + seqRes.length())
877 output.append(new Format("Percentage ID = %2.2f").form(pid));
878 output.append(NEWLINE);
883 public int getEntityCount()
885 return siftsEntry.getEntity().size();
889 public String getDbAccessionId()
891 return siftsEntry.getDbAccessionId();
895 public String getDbCoordSys()
897 return siftsEntry.getDbCoordSys();
901 public String getDbEvidence()
903 return siftsEntry.getDbEvidence();
907 public String getDbSource()
909 return siftsEntry.getDbSource();
913 public String getDbVersion()
915 return siftsEntry.getDbVersion();