bb2e93eca98b43606743a628eef86ea125543127
[jalview.git] / src / jalview / struture / PDBEntryUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.struture;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import com.google.common.collect.Lists;
31
32 import jalview.datamodel.PDBEntry;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.StructureFile;
35 import mc_view.PDBChain;
36
37 public class PDBEntryUtils
38 {
39
40   public static String inferChainId(SequenceI seq)
41   {
42     String targetChainId;
43     if (seq.getName().indexOf("|") > -1)
44     {
45       targetChainId = seq.getName()
46               .substring(seq.getName().lastIndexOf("|") + 1);
47       if (targetChainId.length() > 1)
48       {
49         if (targetChainId.trim().length() == 0)
50         {
51           targetChainId = " ";
52         }
53         else
54         {
55           // not a valid chain identifier
56           targetChainId = "";
57         }
58       }
59     }
60     else
61     {
62       targetChainId = "";
63     }
64     return targetChainId;
65   }
66   protected static Pattern id_and_chain=Pattern.compile("(\\d[0-9A-Za-z]{3})[_:]?(.+)*");
67
68   public static List<PDBEntry> inferPDBEntry(SequenceI seq)
69   {
70     Matcher matcher = id_and_chain.matcher(seq.getName());
71     if (matcher.matches())
72     {
73       String id = matcher.group(1);
74       PDBEntry pdbe = new PDBEntry();
75       pdbe.setId(id);
76       if (matcher.groupCount() > 1)
77       {
78         pdbe.setChainCode(matcher.group(2));
79       }
80
81       return List.of(pdbe);
82     }
83     return List.of();
84   }
85   
86   
87   /**
88    * generate likely PDB IDs & chain codes from seq and ds that fit pdb
89    * @param seq
90    * @param ds
91    * @param pdb
92    * @return empty list or one or more PDBEntry which match pdb.getId()
93    */
94   public static List<PDBEntry> selectPutativePDBe(SequenceI seq,
95           SequenceI ds, StructureFile pdb)
96   {
97     List<PDBEntry> putativePDBe = new ArrayList<PDBEntry>();
98     Set<PDBEntry> possiblePDBe=PDBEntryUtils.gatherPDBEntries(seq,true);
99     for (PDBEntry infPDBe: possiblePDBe)
100     {
101       if (infPDBe.getId().equalsIgnoreCase(pdb.getId()))
102       {
103         putativePDBe.add(infPDBe);
104       }
105     }
106     return putativePDBe;
107   }
108
109
110   public static Set<PDBEntry> gatherPDBEntries(SequenceI seq,boolean inferFromName)
111   {
112     Set<PDBEntry> possiblePDBe=new HashSet<PDBEntry>();
113     while (seq!=null)
114     {
115       if (seq.getAllPDBEntries()!=null) {
116         possiblePDBe.addAll(seq.getAllPDBEntries());
117       }
118       if (inferFromName)
119       {
120         possiblePDBe.addAll(PDBEntryUtils.inferPDBEntry(seq));
121       }
122       seq = seq.getDatasetSequence();  
123     }
124     return possiblePDBe;
125   }
126
127
128   public static PDBEntry selectPutativePDBEntry(List<PDBEntry> putativePDBe,
129           PDBChain chain)
130   {
131     if (putativePDBe.isEmpty())
132     {
133       return null;
134     }
135
136     // check if there's a chaincode 
137     PDBEntry putativeEntry = null;
138     boolean hasChainCodes;
139     // check for a chaincode mapping
140     for (PDBEntry pdbe : putativePDBe)
141     {
142       if (pdbe.getChainCode() != null)
143       {
144         hasChainCodes = true;
145         if (pdbe.getChainCode().equals(chain.id))
146         {
147           putativeEntry = pdbe;
148           return putativeEntry;
149         }
150       }
151     }
152     return null;
153   }
154 }