in progress
[jalview.git] / forester / java / src / org / forester / application / pfam2go_extractor.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2009 Christian M. Zmasek
6 // Copyright (C) 2009 Burnham Institute for Medical Research
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.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.SortedSet;
34 import java.util.TreeSet;
35
36 import org.forester.go.GoId;
37 import org.forester.go.GoTerm;
38 import org.forester.go.GoUtils;
39 import org.forester.go.OBOparser;
40 import org.forester.go.PfamToGoMapping;
41 import org.forester.go.PfamToGoParser;
42 import org.forester.protein.DomainId;
43
44 public class pfam2go_extractor {
45
46     final static private String PRG_NAME = "pfam2go_extractor";
47
48     public static void main( final String args[] ) {
49         if ( args.length < 3 ) {
50             printHelp();
51         }
52         final PfamToGoParser p = new PfamToGoParser( new File( args[ 0 ] ) );
53         List<PfamToGoMapping> pfam2go = null;
54         try {
55             pfam2go = p.parse();
56         }
57         catch ( final IOException e ) {
58             printHelp();
59             e.printStackTrace();
60         }
61         final OBOparser parser = new OBOparser( new File( args[ 1 ] ), OBOparser.ReturnType.BASIC_GO_TERM );
62         List<GoTerm> all_go_terms = null;
63         try {
64             all_go_terms = parser.parse();
65         }
66         catch ( final IOException e ) {
67             printHelp();
68             e.printStackTrace();
69         }
70         final Map<GoId, GoTerm> goid_to_term_map = GoUtils.createGoIdToGoTermMap( all_go_terms );
71         System.out.println( "# pfam2go : " + args[ 0 ] );
72         System.out.println( "# OBO file: " + args[ 1 ] );
73         final GoId[] queries = new GoId[ args.length - 2 ];
74         for( int i = 2; i < args.length; ++i ) {
75             queries[ i - 2 ] = new GoId( args[ i ] );
76             System.out.println( "# " + ( i - 2 ) + ": " + queries[ i - 2 ].getId() + " = "
77                     + goid_to_term_map.get( queries[ i - 2 ] ).getName() + " ("
78                     + goid_to_term_map.get( queries[ i - 2 ] ).getDefinition() + ")" );
79         }
80         final SortedSet<String> pfams = new TreeSet<String>();
81         for( final PfamToGoMapping pfam_to_go_mapping : pfam2go ) {
82             final DomainId domain_id = pfam_to_go_mapping.getKey();
83             final GoId go_id = pfam_to_go_mapping.getValue();
84             final Set<GoId> supers = GoUtils.getAllSuperGoIds( go_id, goid_to_term_map );
85             supers.add( go_id );
86             for( int i = 0; i < queries.length; ++i ) {
87                 if ( supers.contains( queries[ i ] ) ) {
88                     pfams.add( domain_id.toString() );
89                 }
90             }
91         }
92         for( final String pfam : pfams ) {
93             System.out.println( pfam );
94         }
95     }
96
97     private static void printHelp() {
98         System.out.println();
99         System.out.println( PRG_NAME
100                 + " <pfam2go mapping file> <file with all GO terms, in 'obo' format> <GO id> [more GO ids]" );
101         System.out.println();
102     }
103 }