cc8413bfc3c5d8f09c8cc19d0421ba94c5d7c6b3
[jalview.git] / forester / java / src / org / forester / application / aa.java
1
2 package org.forester.application;
3
4 import java.io.FileInputStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.forester.io.parsers.FastaParser;
12 import org.forester.msa.Msa;
13 import org.forester.sequence.BasicSequence;
14 import org.forester.sequence.Sequence;
15 import org.forester.util.ForesterUtil;
16
17 public class aa {
18
19     public static void main( final String args[] ) {
20         try {
21             System.out.println( "STARTING..." );
22             final List<Sequence> orig = FastaParser
23                     .parse( new FileInputStream( "C:\\Users\\zma\\Desktop\\RRMa_domains_ext_20.fasta" ) );
24             final Msa msa = FastaParser.parseMsa( new FileInputStream( "C:\\Users\\zma\\Desktop\\test3_sorted.fasta" ) );
25             final Set<Sequence> all_found_seqs = new HashSet<Sequence>();
26             for( int i = 0; i < msa.getNumberOfSequences(); ++i ) {
27                 final String id = msa.getIdentifier( i );
28                 final String id_ = id.substring( 0, id.indexOf( "_" ) );
29                 final String range = id.substring( id.indexOf( "[" ) + 1, id.indexOf( "]" ) );
30                 //System.out.println( i + ": " + id + "=>" + id_ + " " + range );
31                 if ( ForesterUtil.isEmpty( id_ ) ) {
32                     System.out.println( "ERROR: id is empty for: " + id );
33                     System.exit( -1 );
34                 }
35                 if ( ForesterUtil.isEmpty( range ) ) {
36                     System.out.println( "ERROR: range is empty for: " + id );
37                     System.exit( -1 );
38                 }
39                 int found = 0;
40                 final List<Sequence> found_seqs = new ArrayList<Sequence>();
41                 for( final Sequence orig_seq : orig ) {
42                     final String orig_seq_id = orig_seq.getIdentifier();
43                     if ( ( orig_seq_id.indexOf( id_ ) >= 0 ) && ( orig_seq_id.indexOf( "[" + range + "]" ) >= 0 ) ) {
44                         found++;
45                         found_seqs.add( orig_seq );
46                     }
47                 }
48                 if ( found > 0 ) {
49                     for( final Sequence found_seq : found_seqs ) {
50                         if ( found_seq.getLength() >= 85 ) {
51                             all_found_seqs.add( BasicSequence.createAaSequence( id, found_seq
52                                     .getMolecularSequenceAsString() ) );
53                         }
54                     }
55                     if ( found > 1 ) {
56                         System.out.println( i + ": " + id + "=>" + id_ + " " + range );
57                         System.out.println( "  found: " + found );
58                         for( final Sequence found_seq : found_seqs ) {
59                             System.out.println( found_seq.toString() );
60                         }
61                     }
62                 }
63                 else {
64                     System.out.println( "ERROR: not found: " + id );
65                     System.exit( -1 );
66                 }
67             }
68             final String fasta_ary[] = new String[ all_found_seqs.size() ];
69             int i = 0;
70             for( final Sequence sequence : all_found_seqs ) {
71                 fasta_ary[ i ] = ">" + sequence.getIdentifier() + "\n" + sequence.getMolecularSequenceAsString();
72                 System.out.println( sequence );
73                 i++;
74             }
75             Arrays.sort( fasta_ary );
76             for( final String element : fasta_ary ) {
77                 System.out.println( element );
78             }
79             System.out.println( "DONE." );
80         }
81         catch ( final Exception e ) {
82             e.printStackTrace();
83         }
84     }
85 }