in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / GoAnnotation.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.net.HttpURLConnection;
32 import java.net.URL;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.forester.archaeopteryx.MainFrameApplication;
37 import org.forester.archaeopteryx.TreePanel;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.data.Accession;
41 import org.forester.phylogeny.data.Annotation;
42 import org.forester.phylogeny.data.Sequence;
43 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
44 import org.forester.util.ForesterUtil;
45
46 public class GoAnnotation implements Runnable {
47
48     private static final String        SYMBOL   = "Symbol";
49     private static final String        ASPECT   = "Aspect";
50     private static final String        DB       = "DB";
51     private static final String        EVIDENCE = "Evidence";
52     private static final String        GO_NAME  = "GO Name";
53     private static final String        GO_ID    = "GO ID";
54     private final Phylogeny            _phy;
55     private final MainFrameApplication _mf;
56     private final TreePanel            _treepanel;
57
58     public GoAnnotation( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
59         _phy = phy;
60         _mf = mf;
61         _treepanel = treepanel;
62     }
63
64     private void annotate() {
65         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
66         for( final PhylogenyNodeIterator iter = _phy.iteratorPostorder(); iter.hasNext(); ) {
67             final PhylogenyNode node = iter.next();
68             if ( ( node.getNodeData().getSequences() != null ) && !node.getNodeData().getSequences().isEmpty() ) {
69                 for( final Sequence seq : node.getNodeData().getSequences() ) {
70                     if ( ( ( seq.getAccession() != null ) && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) && ( seq
71                             .getAnnotations() == null ) ) || seq.getAnnotations().isEmpty() ) {
72                         final Accession acc = seq.getAccession();
73                         try {
74                             final URL url = new URL( "http://www.ebi.ac.uk/QuickGO/GAnnotation?protein="
75                                     + acc.getValue() + "&format=tsv" );
76                             final HttpURLConnection url_connection = ( HttpURLConnection ) url.openConnection();
77                             final BufferedReader br = new BufferedReader( new InputStreamReader( url_connection.getInputStream() ) );
78                             final List<String> columns = Arrays.asList( br.readLine().split( "\t" ) );
79                             System.out.println( columns );
80                             final int db_index = columns.indexOf( DB );
81                             final int goid_index = columns.indexOf( GO_ID );
82                             final int name_index = columns.indexOf( GO_NAME );
83                             final int evidence_index = columns.indexOf( EVIDENCE );
84                             final int taxon_index = columns.indexOf( "Taxon" );
85                             final int qualifier_index = columns.indexOf( "Qualifier" );
86                             final int reference_index = columns.indexOf( "Reference" );
87                             final int symbol_index = columns.indexOf( SYMBOL );
88                             final int splice_index = columns.indexOf( "Splice" );
89                             final int with_index = columns.indexOf( "With" );
90                             final int aspect_index = columns.indexOf( ASPECT );
91                             final int source_index = columns.indexOf( "Source" );
92                             String line;
93                             while ( ( line = br.readLine() ) != null ) {
94                                 final String[] fields = line.split( "\t" );
95                                 final Annotation a = new Annotation( fields[ goid_index ] );
96                                 a.setDesc( name_index >= 0 ? fields[ name_index ] : "" );
97                                 a.setSource( db_index >= 0 ? fields[ db_index ] : "" );
98                                 a.setEvidence( evidence_index >= 0 ? fields[ evidence_index ] : "" );
99                                 a.setType( aspect_index >= 0 ? fields[ aspect_index ] : "" );
100                                 seq.addAnnotation( a );
101                                 if ( ForesterUtil.isEmpty( seq.getSymbol() ) && ( symbol_index >= 0 )
102                                         && !ForesterUtil.isEmpty( fields[ symbol_index ] ) ) {
103                                     seq.setSymbol( fields[ symbol_index ] );
104                                 }
105                                 System.out.println( DB + ": " + fields[ db_index ] );
106                                 System.out.println( GO_ID + ": " + fields[ goid_index ] );
107                                 System.out.println( GO_NAME + ": " + fields[ name_index ] );
108                                 System.out.println( EVIDENCE + ": " + fields[ evidence_index ] );
109                                 System.out.println( " taxon" + ": " + fields[ taxon_index ] );
110                                 System.out.println( " qualifier" + ": " + fields[ qualifier_index ] );
111                                 System.out.println( " reference" + ": " + fields[ reference_index ] );
112                                 System.out.println( SYMBOL + ": " + fields[ symbol_index ] );
113                                 System.out.println( " splice" + ": " + fields[ splice_index ] );
114                                 System.out.println( " with" + ": " + fields[ with_index ] );
115                                 System.out.println( ASPECT + ": " + fields[ aspect_index ] );
116                                 System.out.println( " source" + ": " + fields[ source_index ] );
117                             }
118                             br.close();
119                         }
120                         catch ( final IOException e ) {
121                             // TODO Auto-generated catch block
122                             e.printStackTrace();
123                         }
124                     }
125                 }
126             }
127         }
128         _treepanel.repaint();
129         _treepanel.setEdited( true );
130     }
131
132     @Override
133     public void run() {
134         annotate();
135     }
136 }