in progress
[jalview.git] / forester / java / src / org / forester / application / aaa.java
1
2 package org.forester.application;
3
4 import java.io.FileInputStream;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.forester.io.parsers.FastaParser;
14 import org.forester.sequence.Sequence;
15 import org.forester.util.ForesterUtil;
16
17 public class aaa {
18
19     public final static Pattern GN_PATTERN    = Pattern.compile( "GN=(\\S+)\\s" );     //use w+ instead of S+ for more stringent setting.
20     public final static Pattern RANGE_PATTERN = Pattern.compile( "\\[(\\d+-\\d+)\\]" ); //use w+ instead of S+ for more stringent setting.
21
22     public static void main( final String args[] ) {
23         try {
24             System.out.println( "STARTING..." );
25             final List<Sequence> orig = FastaParser
26                     .parse( new FileInputStream( "C:\\Users\\zma\\Desktop\\RRMa_domains_ext_20_2.fasta" ) );
27             final List<String> new_seqs = new ArrayList<String>();
28             for( final Sequence seq : orig ) {
29                 final Matcher matcher = GN_PATTERN.matcher( seq.getIdentifier() );
30                 String gn = "";
31                 if ( matcher.find() ) {
32                     gn = matcher.group( 1 );
33                 }
34                 else {
35                     System.out.println( "ERROR: no gene for: " + seq.getIdentifier() );
36                     System.exit( -1 );
37                 }
38                 new_seqs.add( ">" + gn + "|" + seq.getIdentifier() + "\n" + seq.getMolecularSequenceAsString() );
39             }
40             final Set<String> gn_ra_set = new HashSet<String>();
41             final Set<String> mol_seq_set = new HashSet<String>();
42             Collections.sort( new_seqs );
43             int unique_counter = 0;
44             int duplicate_counter_gn_ra = 0;
45             int duplicate_counter_mol_seq = 0;
46             String prev_gn = "____";
47             for( final String seq : new_seqs ) {
48                 final Matcher matcher_ra = RANGE_PATTERN.matcher( seq );
49                 final Matcher matcher_gn = GN_PATTERN.matcher( seq );
50                 String range = "";
51                 if ( matcher_ra.find() ) {
52                     range = matcher_ra.group( 1 );
53                 }
54                 else {
55                     System.out.println( "ERROR: no range for: " + seq );
56                     System.exit( -1 );
57                 }
58                 matcher_gn.find();
59                 final String gn = matcher_gn.group( 1 );
60                 final String gn_ra = gn + "_" + range;
61                 if ( !gn_ra_set.contains( gn_ra ) ) {
62                     gn_ra_set.add( gn_ra );
63                     final String mol_seq = seq.split( "\n" )[ 1 ];
64                     if ( !mol_seq_set.contains( mol_seq ) ) {
65                         mol_seq_set.add( mol_seq );
66                         if ( prev_gn.equals( gn ) ) {
67                             int count = same_protein_seqs.size();
68                             if ( count == 1 ) {
69                                 System.out.println( seq );
70                             }
71                             else {
72                                 int c = 1;
73                                 for( final String s : same_protein_seqs ) {
74                                     System.out.println( new StringBuffer( s ).insert( s.indexOf( "|" ),
75                                                                                       "__" + c + "_OF_" + count )
76                                             .toString() );
77                                     c++;
78                                 }
79                             }
80                         }
81                         prev_gn = gn;
82                         System.out.println( seq );
83                         unique_counter++;
84                     }
85                     else {
86                         duplicate_counter_mol_seq++;
87                     }
88                 }
89                 else {
90                     duplicate_counter_gn_ra++;
91                 }
92             }
93             System.out.println( "unique   : " + unique_counter );
94             System.out.println( "duplicate because gn and range same: " + duplicate_counter_gn_ra );
95             System.out.println( "duplicate because mol seq same     : " + duplicate_counter_mol_seq );
96         }
97         catch ( final Exception e ) {
98             e.printStackTrace();
99         }
100     }
101 }