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