new UI types?
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainPanelEdit.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;
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.phylogeny.Phylogeny;
37 import org.forester.phylogeny.PhylogenyNode;
38 import org.forester.phylogeny.data.Accession;
39 import org.forester.phylogeny.data.Annotation;
40 import org.forester.phylogeny.data.Sequence;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.util.ForesterUtil;
43
44 public class MainPanelEdit implements Runnable {
45
46     private static final String        SYMBOL   = "Symbol";
47     private static final String        ASPECT   = "Aspect";
48     private static final String        DB       = "DB";
49     private static final String        EVIDENCE = "Evidence";
50     private static final String        GO_NAME  = "GO Name";
51     private static final String        GO_ID    = "GO ID";
52     private final Phylogeny            _phy;
53     private final MainFrameApplication _mf;
54     private final TreePanel            _treepanel;
55
56     MainPanelEdit( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
57         _phy = phy;
58         _mf = mf;
59         _treepanel = treepanel;
60     }
61
62     private void annotate() {
63         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
64         for( final PhylogenyNodeIterator iter = _phy.iteratorPostorder(); iter.hasNext(); ) {
65             final PhylogenyNode node = iter.next();
66             if ( ( node.getNodeData().getSequences() != null ) && !node.getNodeData().getSequences().isEmpty() ) {
67                 for( final Sequence seq : node.getNodeData().getSequences() ) {
68                     if ( ( ( seq.getAccession() != null ) && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) && ( seq
69                             .getAnnotations() == null ) ) || seq.getAnnotations().isEmpty() ) {
70                         final Accession acc = seq.getAccession();
71                         try {
72                             final URL url = new URL( "http://www.ebi.ac.uk/QuickGO/GAnnotation?protein="
73                                     + acc.getValue() + "&format=tsv" );
74                             final HttpURLConnection url_connection = ( HttpURLConnection ) url.openConnection();
75                             final BufferedReader br = new BufferedReader( new InputStreamReader( url_connection.getInputStream() ) );
76                             final List<String> columns = Arrays.asList( br.readLine().split( "\t" ) );
77                             System.out.println( columns );
78                             final int db_index = columns.indexOf( DB );
79                             final int goid_index = columns.indexOf( GO_ID );
80                             final int name_index = columns.indexOf( GO_NAME );
81                             final int evidence_index = columns.indexOf( EVIDENCE );
82                             final int taxon_index = columns.indexOf( "Taxon" );
83                             final int qualifier_index = columns.indexOf( "Qualifier" );
84                             final int reference_index = columns.indexOf( "Reference" );
85                             final int symbol_index = columns.indexOf( SYMBOL );
86                             final int splice_index = columns.indexOf( "Splice" );
87                             final int with_index = columns.indexOf( "With" );
88                             final int aspect_index = columns.indexOf( ASPECT );
89                             final int source_index = columns.indexOf( "Source" );
90                             String line;
91                             while ( ( line = br.readLine() ) != null ) {
92                                 final String[] fields = line.split( "\t" );
93                                 final Annotation a = new Annotation( fields[ goid_index ] );
94                                 a.setDesc( name_index >= 0 ? fields[ name_index ] : "" );
95                                 a.setSource( db_index >= 0 ? fields[ db_index ] : "" );
96                                 a.setEvidence( evidence_index >= 0 ? fields[ evidence_index ] : "" );
97                                 a.setType( aspect_index >= 0 ? fields[ aspect_index ] : "" );
98                                 seq.addAnnotation( a );
99                                 if ( ForesterUtil.isEmpty( seq.getSymbol() ) && ( symbol_index >= 0 )
100                                         && !ForesterUtil.isEmpty( fields[ symbol_index ] ) ) {
101                                     seq.setSymbol( fields[ symbol_index ] );
102                                 }
103                                 System.out.println( DB + ": " + fields[ db_index ] );
104                                 System.out.println( GO_ID + ": " + fields[ goid_index ] );
105                                 System.out.println( GO_NAME + ": " + fields[ name_index ] );
106                                 System.out.println( EVIDENCE + ": " + fields[ evidence_index ] );
107                                 System.out.println( " taxon" + ": " + fields[ taxon_index ] );
108                                 System.out.println( " qualifier" + ": " + fields[ qualifier_index ] );
109                                 System.out.println( " reference" + ": " + fields[ reference_index ] );
110                                 System.out.println( SYMBOL + ": " + fields[ symbol_index ] );
111                                 System.out.println( " splice" + ": " + fields[ splice_index ] );
112                                 System.out.println( " with" + ": " + fields[ with_index ] );
113                                 System.out.println( ASPECT + ": " + fields[ aspect_index ] );
114                                 System.out.println( " source" + ": " + fields[ source_index ] );
115                             }
116                             br.close();
117                         }
118                         catch ( final IOException e ) {
119                             // TODO Auto-generated catch block
120                             e.printStackTrace();
121                         }
122                     }
123                 }
124             }
125         }
126         _treepanel.repaint();
127         _treepanel.setEdited( true );
128     }
129
130     @Override
131     public void run() {
132         annotate();
133     }
134 }