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: https://sites.google.com/site/cmzmasek/home/software/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
43 public class pfam2go_extractor {
44
45     final static private String PRG_NAME = "pfam2go_extractor";
46
47     public static void main( final String args[] ) {
48         if ( args.length < 3 ) {
49             printHelp();
50         }
51         final PfamToGoParser p = new PfamToGoParser( new File( args[ 0 ] ) );
52         List<PfamToGoMapping> pfam2go = null;
53         try {
54             pfam2go = p.parse();
55         }
56         catch ( final IOException e ) {
57             printHelp();
58             e.printStackTrace();
59         }
60         final OBOparser parser = new OBOparser( new File( args[ 1 ] ), OBOparser.ReturnType.BASIC_GO_TERM );
61         List<GoTerm> all_go_terms = null;
62         try {
63             all_go_terms = parser.parse();
64         }
65         catch ( final IOException e ) {
66             printHelp();
67             e.printStackTrace();
68         }
69         final Map<GoId, GoTerm> goid_to_term_map = GoUtils.createGoIdToGoTermMap( all_go_terms );
70         System.out.println( "# pfam2go : " + args[ 0 ] );
71         System.out.println( "# OBO file: " + args[ 1 ] );
72         final GoId[] queries = new GoId[ args.length - 2 ];
73         for( int i = 2; i < args.length; ++i ) {
74             queries[ i - 2 ] = new GoId( args[ i ] );
75             System.out.println( "# " + ( i - 2 ) + ": " + queries[ i - 2 ].getId() + " = "
76                     + goid_to_term_map.get( queries[ i - 2 ] ).getName() + " ("
77                     + goid_to_term_map.get( queries[ i - 2 ] ).getDefinition() + ")" );
78         }
79         final SortedSet<String> pfams = new TreeSet<String>();
80         for( final PfamToGoMapping pfam_to_go_mapping : pfam2go ) {
81             final String domain_id = pfam_to_go_mapping.getKey();
82             final GoId go_id = pfam_to_go_mapping.getValue();
83             final Set<GoId> supers = GoUtils.getAllSuperGoIds( go_id, goid_to_term_map );
84             supers.add( go_id );
85             for( final GoId querie : queries ) {
86                 if ( supers.contains( querie ) ) {
87                     pfams.add( domain_id.toString() );
88                 }
89             }
90         }
91         for( final String pfam : pfams ) {
92             System.out.println( pfam );
93         }
94     }
95
96     private static void printHelp() {
97         System.out.println();
98         System.out.println( PRG_NAME
99                             + " <pfam2go mapping file> <file with all GO terms, in 'obo' format> <GO id> [more GO ids]" );
100         System.out.println();
101     }
102 }