JAL-2189 apply license
[jalview.git] / src / jalview / io / gff / GffHelperFactory.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 /**
24  * A factory to serve instances of GFF helper classes
25  */
26 public class GffHelperFactory
27 {
28
29   /**
30    * Returns a class to process the GFF line based on inspecting its column
31    * data. This may return a general-purpose GFF2 or GFF3 helper, or a
32    * specialisation for a flavour of GFF generated by a particular tool.
33    * 
34    * @param gff
35    * @return
36    */
37   public static GffHelperI getHelper(String[] gff)
38   {
39     if (gff == null || gff.length < 6)
40     {
41       return null;
42     }
43
44     GffHelperI result = null;
45     if (ExonerateHelper.recognises(gff))
46     {
47       result = new ExonerateHelper();
48     }
49     else if (InterProScanHelper.recognises(gff))
50     {
51       result = new InterProScanHelper();
52     }
53     else if (looksLikeGff3(gff))
54     {
55       result = new Gff3Helper();
56     }
57     else
58     {
59       result = new Gff2Helper();
60     }
61
62     return result;
63   }
64
65   /**
66    * Heuristic rule: if column 9 seems to have Name=Value entries, assume this
67    * is GFF3. GFF3 uses '=' as name-value separator, GFF2 uses space ' '.
68    * 
69    * @param gff
70    * @return
71    */
72   protected static boolean looksLikeGff3(String[] gff)
73   {
74     if (gff.length >= 9)
75     {
76       String attributes = gff[8].trim();
77       int pos1 = attributes.indexOf(';');
78       int pos2 = attributes.indexOf('=');
79       if (pos2 != -1 && (pos1 == -1 || pos2 < pos1))
80       {
81         // there is an '=' before the first ';' (if any)
82         // not foolproof as theoretically GFF2 could be like "Name Value=123;"
83         return true;
84       }
85     }
86     return false;
87   }
88
89 }