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