vamsas api example classes - AstexViewer@MSD-EBI
[vamsas.git] / src / uk / ac / ebi / msd / vamsas / vamav / documentmanagement / grouping / SequenceGrouping_structure.java
1 /* * EMBL-EBI
2 * MSD Group
3 * VAMSAS Project
4 * VAMAV
5 */
6 package uk.ac.ebi.msd.vamsas.vamav.documentmanagement.grouping;
7
8 import uk.ac.ebi.msd.vamsas.vamav.documentmanagement.PDBUtil;
9 import uk.ac.ebi.msd.vamsas.vamav.documentmanagement.grouping.exceptions.UnableToGroupSequencesException;
10 import uk.ac.ebi.msd.vamsas.vamav.exceptions.IncorrectVamavModuleInitialisation;
11 import uk.ac.ebi.msd.vamsas.vamav.session.VAMAVSession;
12 import uk.ac.ebi.msd.vamsas.vamav.util.data.PDBEntry;
13 import uk.ac.ebi.msd.vamsas.vamav.util.mapping.MappingManager;
14 import uk.ac.ebi.msd.vamsas.vamav.util.pool.StringBufferPool;
15
16
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.Hashtable;
20 import java.util.Properties;
21
22 /**
23  *Group sequences per structure
24  *
25  * First, add sequences, with DB ref is provided ( addSequence methods)
26  * then, 
27  *storeGroup Method (To create atribute file and alignment files)
28  *
29  *Each time a sequence is added, it is added to the determined grou.
30  * @version 1
31  *@author <a href="mailto:pierre@ebi.ac.uk">Pierre MARGUERITE</a>
32  */
33 public class SequenceGrouping_structure  extends SequenceGrouping //implements uk.ac.ebi.msd.vamsas.vamav.VamaVModule{
34 {
35         
36         
37         //private final VAMAVLogger logger = new VAMAVLoggerImpl(this.getClass());
38         protected VAMAVSession vamavSession = null;
39         
40         
41         /**
42          *  
43          * Constructor of the  SequenceGrouping class, given a mapping manager 
44          *
45          * @param _mappingManager the mapping manager to use for mapping 
46          */
47           public SequenceGrouping_structure(  MappingManager _mappingManager)
48                 {
49                         super(_mappingManager);
50                 }
51           
52           /**
53                  *  
54                  * Constructor of the  SequenceGrouping class, given a mapping manager 
55                  *
56                  * @param _mappingManager the mapping manager to use for mapping 
57                  * @param sessionID current session for which will be used the current sequence grouping.
58                  */
59                   public SequenceGrouping_structure(MappingManager _mappingManager, VAMAVSession session )
60                         {
61                                 super(_mappingManager);
62                                 this.logger.setSession(session.getSessionID());
63                                 this.vamavSession = session;
64                         }
65         
66         /**
67          * Inits the Sequence Grouping sub module, with a list of configuration properties.
68          */
69         public void init (Properties moduleProperties) throws IncorrectVamavModuleInitialisation
70                 {
71                         super.init(moduleProperties);
72                 }
73     
74     /**
75      * Add a sequence for grouping.
76      * 
77      * @param sequence chain sequence to add
78      * @param name name of the sequence to add
79      * @return a list of parameters to call the AstexViewer
80      * @throws UnableToGroupSequencesException if an error occurs during the adding.
81      */
82     public Hashtable<String, Object>  addSequence(String sequence, String name) throws UnableToGroupSequencesException
83         {
84                 if( sequence == null || sequence.length()<1)
85                         {
86                                 this.logger.sessionWarn("SequenceGrouping addSequence - no Sequence Provided. Can not add.");
87                                 return null;
88                         }
89                 if( name == null || name.length()<1)
90                         {
91                                 this.logger.sessionWarn("SequenceGrouping addSequence - no name Provided. Can not add.");
92                                 return null;
93                         }
94                 ArrayList<PDBEntry> entries = null;
95                 try
96                         {
97                                 entries = this.getMapping().retrievePDBIDFromSequence(sequence);
98                         }
99                 catch (IncorrectVamavModuleInitialisation inE)
100                         {
101                                 this.logger.sessionError("Sequence Grouping addSequence - error during mapping execution. Can not proceed.", inE);
102                                 throw new UnableToGroupSequencesException();
103                         }
104                 Hashtable<String, Object> parameters = null;
105                 if (entries == null ) return null;
106                 StringBuffer pdbIDs = StringBufferPool.getString();
107                 for (int entryNumber = entries.size() -1; entryNumber>=0; entryNumber--)
108                         {
109                                 PDBEntry entry = entries.get(entryNumber);
110                                 if (entry == null) continue;
111                                         
112                                 parameters = new Hashtable<String, Object>();
113                                 int molNumber = 0;
114                                 if( !parameters.containsValue(entry))
115                                         {
116                                                 parameters.put("moleculeG"+molNumber,entry);
117                                                 parameters.put("nameG"+molNumber, entry.getPdbID());
118                                                 this.logger.sessionDebug("New molecule added "+entry);
119                                         
120                                                 molNumber++;
121                                         }
122                                 PDBUtil util = new PDBUtil(this.vamavSession);
123                                 
124                                 ArrayList<PDBUtil.Chain> chains = util.GetChain(entry);
125                                 util = null;
126                                 if( chains != null)
127                                         {
128                                                         /*System.out.println(" chains found ");*/
129                                                 for (int chainN = chains.size()-1; chainN>=0; chainN --)
130                                                         {
131                                                                 PDBUtil.Chain chain =chains.get(chainN);
132                                                                         if (chain == null)continue;
133                                                                         /*System.out.println("chain "+chain.getName()+ " "+chain.getSequence());*/
134                                                                         String groupName = chain.getName();
135                                                                         int pos = groupName.indexOf("_");
136                                                                         if( pos>0) groupName = groupName.substring(0,pos);
137                                                                         this.addSequenceToGroup(groupName,chain.getSequence(),chain.getName()); 
138                                                                         this.addStructureToGroup(groupName, entry.getPdbID());
139                                                         }
140                                         }
141                                 else 
142                                         this.logger.sessionWarn("no chains found ");
143                                 String id = entry.getPdbID();
144                                 this.addSequenceToGroup(id, sequence, name);
145                                 if(pdbIDs.length()<1) pdbIDs.append(id);
146                                 else pdbIDs.append(", "+id);
147                         }
148                 parameters.put(this.pdbidsproperty,pdbIDs.toString());
149                         StringBufferPool.returnString(pdbIDs);
150                 return parameters;
151         }
152     
153     /**
154      * Add a sequence for grouping, with an associated accessionID and its source.
155      * 
156      * @param sequence chain sequence to add for grouping
157      * @param name name of the sequence to add
158      * @param accessionID accessionId associated to the sequence
159      * @param source source of the accessionID
160      * @return list of parameters to call the AstexViewer, if needed.  Null, otherwise.
161      * @throws UnableToGroupSequencesException if an error occurs during the adding.
162      */
163     public Hashtable<String, Object> addSequence(String sequence, String name, String accessionID, String source) throws UnableToGroupSequencesException
164         {
165                 
166                 if( sequence == null || sequence.length()<1)
167                         {
168                                 this.logger.sessionError("SequenceGrouping - addSequence: Can not add sequence / The sequence is unknown.");
169                                 return null;
170                         }
171                 if( name == null || name.length()<1)
172                         {
173                                 this.logger.sessionError("SequenceGrouping - addSequence: Can not add sequence / The name is unknown.");
174                                 return null;
175                         }
176                 if( source == null || source.length()<1)
177                         {
178                                 this.logger.sessionWarn("SequenceGrouping - addSequence: the accession ID source is not provided / try to determine PDBID from the sequence.");
179                                 return this.addSequence(sequence, name);
180                         }
181                 if( accessionID == null || accessionID.length()<1)
182                         {
183                                 this.logger.sessionWarn("SequenceGrouping - addSequence: the accession ID is not provided / try to determine PDBID from the sequence.");
184                                 return this.addSequence(sequence, name);
185                         }
186                 Hashtable<String, Object> parameters =  null;
187                 try
188                         {
189                                 ArrayList <PDBEntry> entries = null;
190                 
191                                 if("PDB".equals(source))
192                                         {
193                                                 PDBEntry entry = this.getMapping().getPDBEntryFromPDBID(accessionID);
194                                                 if( entry != null)
195                                                         {
196                                                                 entries = new ArrayList<PDBEntry>(1);
197                                                                 entries.add(entry);
198                                                         }
199                                         }
200                                 if ("UNIPROT".equalsIgnoreCase(source))
201                                         {
202                                                 entries = this.getMapping().retrievePDBIDFromUniprotAC(accessionID);
203                                         }
204                                 if( entries == null || entries.size()<1) 
205                                         {
206                                                 this.logger.sessionError("No associated PDB ID to the accessionAC "+ accessionID + " from "+source);
207                                                 return this.addSequence(sequence, name);
208                                         }
209                                 //boolean notFound = false;
210                                 //hasAlignmentSequences = true;
211                                 parameters = new Hashtable<String, Object>();
212                         
213                         //currenty only use the first entry
214                         //for ( int entryNumber = entries.size()-1; entryNumber>=0; entryNumber --)
215                                 //{
216                                 int molNumber = 0;
217                                 StringBuffer pdbIDs = null;
218                                 if (entries != null || entries.size()<1)
219                                         {
220                                                 PDBUtil util = new PDBUtil(this.vamavSession);
221                                                 for (int entryNumber = entries.size() -1; entryNumber >=0; entryNumber --)
222                                                         {
223                                                                 PDBEntry entry =  entries.get(entryNumber);
224                                                                 parameters.put("moleculeG"+molNumber,entry);
225                                                                 parameters.put("nameG"+molNumber, entry.getPdbID());
226                                                 
227                                                                 ArrayList<PDBUtil.Chain> chains = util.GetChain(entry);
228                                                 
229                                                                 if( chains != null)
230                                                                         {
231                                                                                 this.logger.sessionDebug(" chains found ");
232                                                         //              StringBuffer buff = StringBufferPool.getString();
233                                                                                 for (int chainN = chains.size()-1; chainN>=0; chainN --)
234                                                                                         {
235                                                                                                 PDBUtil.Chain chain = chains.get(chainN);
236                                                                                                 if (chain == null)continue;
237                                                                                                 String groupName = chain.getName();
238                                                                                                 int pos = groupName.indexOf("_");
239                                                                                                 if( pos>0) groupName = groupName.substring(0,pos);
240                                                                                                 this.addSequenceToGroup(groupName, chain.getSequence(),chain.getName());  
241                                                                                                 this.addStructureToGroup(groupName, entry.getPdbID());
242                                                                                                 chain = null;
243                                                                                         }
244                                                                         }
245                                                                 else 
246                                                                         this.logger.sessionDebug("no chains found ");
247                                         
248                                                                 this.addSequenceToGroup(entry.getPdbID(), sequence, name);
249                                                                 if( pdbIDs == null) pdbIDs = StringBufferPool.getString();
250                                                                 if(pdbIDs.length()>0)pdbIDs.append(", "+entry.getPdbID());
251                                                                 else pdbIDs.append(entry.getPdbID());
252                                                                 entries.remove(entry);
253                                                         }
254                                                 util = null;
255                                                 if(!"PDB".equals(source))parameters.put(this.pdbidsproperty,pdbIDs.toString());
256                                                 StringBufferPool.returnString(pdbIDs);
257                                         }
258                                 
259                         //}
260                 /*catch (IncorrectVamavModuleInitialisation inE)
261                         {
262                                 logger.AppliError("Sequence Grouping addSequence - error during mapping execution. Can not proceed.", inE);
263                                 throw new UnableToGroupSequencesException();
264                         }*/
265                         }
266                 catch (Exception inE)
267                         {
268                                 this.logger.sessionError("Sequence Grouping addSequence - error during mapping execution. Can not proceed.", inE);
269                                 throw new UnableToGroupSequencesException();
270                         }
271                         return parameters;
272         }
273         
274     /**
275      * Retrieves the directory to store files in.
276      * @return the output directory for created file by the sequence grouping processing.
277      */
278     public String getOutputDirectory()
279         {
280                 File sessionDir = this.vamavSession.getSessionDirectory(); 
281                 if( sessionDir != null)
282                         return sessionDir.getAbsolutePath();
283                 return null; //should not happen, has directory file is attached during object creation. 
284         }
285
286         /**
287          * Retrieves current sessionID
288          * @see uk.ac.ebi.msd.vamsas.vamav.documentmanagement.grouping.SequenceGrouping#getSessionID()
289          */
290         @Override
291         public String getSessionID()
292                 {
293                         
294                         return this.vamavSession.getSessionID();
295                 }
296     
297     
298  }