9cfa5a8c661b6dd1887ca59293e0c34b5ca37388
[jalview.git] / src / jalview / io / gff / GffHelperFactory.java
1 package jalview.io.gff;
2
3 /**
4  * A factory to serve instances of GFF helper classes
5  */
6 public class GffHelperFactory
7 {
8
9   /**
10    * Returns a class to process the GFF line based on inspecting its column
11    * data. This may return a general-purpose GFF2 or GFF3 helper, or a
12    * specialisation for a flavour of GFF generated by a particular tool.
13    * 
14    * @param gff
15    * @return
16    */
17   public static GffHelperI getHelper(String[] gff)
18   {
19     if (gff == null || gff.length < 6)
20     {
21       return null;
22     }
23
24     GffHelperI result = null;
25     if (ExonerateHelper.recognises(gff))
26     {
27       result = new ExonerateHelper();
28     }
29     else if (InterProScanHelper.recognises(gff))
30     {
31       result = new InterProScanHelper();
32     }
33     else if (looksLikeGff3(gff))
34     {
35       result = new Gff3Helper();
36     }
37     else
38     {
39       result = new Gff2Helper();
40     }
41
42     return result;
43   }
44
45   /**
46    * Heuristic rule: if column 9 seems to have Name=Value entries, assume this
47    * is GFF3. GFF3 uses '=' as name-value separator, GFF2 uses space ' '.
48    * 
49    * @param gff
50    * @return
51    */
52   protected static boolean looksLikeGff3(String[] gff)
53   {
54     if (gff.length >= 9)
55     {
56       String attributes = gff[8].trim();
57       int pos1 = attributes.indexOf(';');
58       int pos2 = attributes.indexOf('=');
59       if (pos2 != -1 && (pos1 == -1 || pos2 < pos1))
60       {
61         // there is an '=' before the first ';' (if any)
62         // not foolproof as theoretically GFF2 could be like "Name Value=123;"
63         return true;
64       }
65     }
66     return false;
67   }
68
69 }