948cdd2d4065eb5555a22b50164f25f613e46555
[jalview.git] / src / jalview / io / gff / InterProScanHelper.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.io.gff;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.StringUtils;
27
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * A handler to parse GFF in the format generated by InterProScan
34  */
35 public class InterProScanHelper extends Gff3Helper
36 {
37   private static final String INTER_PRO_SCAN = "InterProScan";
38
39   private static final String SIGNATURE_DESC = "signature_desc";
40
41   /**
42    * Process one GFF feature line (as modelled by SequenceFeature)
43    * 
44    * @param seq
45    *          the sequence with which this feature is associated
46    * @param gff
47    *          the gff column data
48    * @param align
49    *          the alignment we are adding GFF to
50    * @param newseqs
51    *          any new sequences referenced by the GFF
52    * @param relaxedIdMatching
53    *          if true, match word tokens in sequence names
54    * @return a sequence feature if one should be added to the sequence, else
55    *         null (i.e. it has been processed in another way e.g. to generate a
56    *         mapping)
57    * @throws IOException
58    */
59   @Override
60   public SequenceFeature processGff(SequenceI seq, String[] gff,
61           AlignmentI align, List<SequenceI> newseqs,
62           boolean relaxedIdMatching) throws IOException
63   {
64     /*
65      * ignore the 'polypeptide' match of the whole sequence
66      */
67     if (".".equals(gff[SOURCE_COL]))
68     {
69       return null;
70     }
71
72     return super.processGff(seq, gff, align, newseqs, relaxedIdMatching);
73   }
74
75   /**
76    * An override that
77    * <ul>
78    * <li>uses Source (column 2) as feature type instead of the default column 3</li>
79    * <li>sets "InterProScan" as the feature group</li>
80    * <li>extracts "signature_desc" attribute as the feature description</li>
81    * </ul>
82    */
83   @Override
84   protected SequenceFeature buildSequenceFeature(String[] gff,
85           Map<String, List<String>> attributes)
86   {
87     SequenceFeature sf = super.buildSequenceFeature(gff, SOURCE_COL,
88             INTER_PRO_SCAN, attributes);
89
90     /*
91      * signature_desc is a more informative source of description
92      */
93     List<String> desc = attributes.get(SIGNATURE_DESC);
94     String description = StringUtils.listToDelimitedString(desc, ", ");
95     if (description.length() > 0)
96     {
97       sf.setDescription(description);
98     }
99
100     return sf;
101   }
102
103   /**
104    * Tests whether the GFF data looks like it was generated by InterProScan
105    * 
106    * @param columns
107    * @return
108    */
109   public static boolean recognises(String[] columns)
110   {
111     SequenceOntologyI so = SequenceOntologyFactory.getInstance();
112     String type = columns[TYPE_COL];
113     if (so.isA(type, SequenceOntologyI.PROTEIN_MATCH)
114             || (".".equals(columns[SOURCE_COL])
115                     && so.isA(type, SequenceOntologyI.POLYPEPTIDE)))
116     {
117       return true;
118     }
119     return false;
120   }
121
122   /**
123    * Overriden method, because InterProScan GFF has the target sequence id in
124    * GFF field 'ID' rather than the usual 'Target' :-O
125    */
126   @Override
127   protected String findTargetId(String target,
128           Map<String, List<String>> set)
129   {
130     List<String> ids = set.get(ID);
131     if (ids == null || ids.size() != 1)
132     {
133       return null;
134     }
135     return ids.get(0);
136   }
137
138 }