inprogress
[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.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Set;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import org.forester.io.parsers.FastaParser;
15 import org.forester.sequence.Sequence;
16 import org.forester.util.EasyWriter;
17 import org.forester.util.ForesterUtil;
18
19 public class aaa {
20
21     public final static Pattern GN_PATTERN    = Pattern.compile( "GN=(\\S+)\\s" );     //use w+ instead of S+ for more stringent setting.
22     public final static Pattern RANGE_PATTERN = Pattern.compile( "\\[(\\d+-\\d+)\\]" ); //use w+ instead of S+ for more stringent setting.
23     public final static int     MIN_LENGTH    = 85;
24
25     public static void main( final String args[] ) {
26         try {
27             final EasyWriter out = ( EasyWriter ) ForesterUtil.createEasyWriter( "aaa_out" );
28             System.out.println( "STARTING..." );
29             final List<Sequence> too_short = new ArrayList<Sequence>();
30             final List<Sequence> orig = FastaParser
31                     .parse( new FileInputStream( "C:\\Users\\zma\\Desktop\\RRMa_domains_ext_20_2.fasta" ) );
32             final int initial_number = orig.size();
33             final List<String> new_seqs = new ArrayList<String>();
34             for( final Sequence seq : orig ) {
35                 if ( seq.getLength() < MIN_LENGTH ) {
36                     too_short.add( seq );
37                     continue;
38                 }
39                 final Matcher matcher = GN_PATTERN.matcher( seq.getIdentifier() );
40                 String gn = "";
41                 if ( matcher.find() ) {
42                     gn = matcher.group( 1 );
43                 }
44                 else {
45                     System.out.println( "ERROR: no gene for: " + seq.getIdentifier() );
46                     System.exit( -1 );
47                 }
48                 new_seqs.add( ">" + gn + "|" + seq.getIdentifier() + "\n" + seq.getMolecularSequenceAsString() );
49             }
50             final Set<String> gn_ra_set = new HashSet<String>();
51             final Set<String> mol_seq_set = new HashSet<String>();
52             Collections.sort( new_seqs );
53             int unique_counter = 0;
54             final List<String> duplicate_gn_ra = new ArrayList<String>();
55             final List<String> duplicate_mol_seq = new ArrayList<String>();
56             final List<String> new_seqs_unique = new ArrayList<String>();
57             for( final String seq : new_seqs ) {
58                 final Matcher matcher_ra = RANGE_PATTERN.matcher( seq );
59                 final Matcher matcher_gn = GN_PATTERN.matcher( seq );
60                 String range = "";
61                 if ( matcher_ra.find() ) {
62                     range = matcher_ra.group( 1 );
63                 }
64                 else {
65                     System.out.println( "ERROR: no range for: " + seq );
66                     System.exit( -1 );
67                 }
68                 matcher_gn.find();
69                 final String gn = matcher_gn.group( 1 );
70                 final String gn_ra = gn + "_" + range;
71                 if ( !gn_ra_set.contains( gn_ra ) ) {
72                     gn_ra_set.add( gn_ra );
73                     final String mol_seq = seq.split( "\n" )[ 1 ];
74                     if ( !mol_seq_set.contains( mol_seq ) ) {
75                         mol_seq_set.add( mol_seq );
76                         new_seqs_unique.add( seq );
77                         unique_counter++;
78                     }
79                     else {
80                         duplicate_mol_seq.add( seq );
81                     }
82                 }
83                 else {
84                     duplicate_gn_ra.add( seq );
85                 }
86             }
87             String prev_gn = "___";
88             boolean is_first = true;
89             List<String> seqs_from_same_protein = new ArrayList<String>();
90             for( final String seq : new_seqs_unique ) {
91                 final Matcher matcher_gn = GN_PATTERN.matcher( seq );
92                 matcher_gn.find();
93                 final String gn = matcher_gn.group( 1 );
94                 if ( !prev_gn.equals( gn ) && !is_first ) {
95                     doit( seqs_from_same_protein, out );
96                     seqs_from_same_protein = new ArrayList<String>();
97                 }
98                 prev_gn = gn;
99                 is_first = false;
100                 seqs_from_same_protein.add( seq );
101             }
102             doit( seqs_from_same_protein, out );
103             out.println( "" );
104             out.println( "" );
105             out.println( "Removed because same GN and region:" );
106             for( final String s : duplicate_gn_ra ) {
107                 out.println( s );
108             }
109             out.println( "" );
110             out.println( "" );
111             out.println( "Removed because identical mol sequence:" );
112             for( final String s : duplicate_mol_seq ) {
113                 out.println( s );
114             }
115             out.println( "" );
116             out.println( "" );
117             out.println( "Removed because too short:" );
118             for( final Sequence s : too_short ) {
119                 out.println( s.toString() );
120             }
121             out.println( "" );
122             out.println( "" );
123             out.println( "initial:" + initial_number );
124             out.println( "ignored because shorter than " + MIN_LENGTH + "aa: " + too_short.size() );
125             out.println( "unique   : " + unique_counter );
126             out.println( "unique   : " + new_seqs_unique.size() );
127             out.println( "duplicate because gn and range same: " + duplicate_gn_ra.size() );
128             out.println( "duplicate because mol seq same     : " + duplicate_mol_seq.size() );
129             out.flush();
130             out.close();
131             System.out.println( "DONE " );
132         }
133         catch ( final Exception e ) {
134             e.printStackTrace();
135         }
136     }
137
138     private static void doit( final List<String> same_protein_seqs, final EasyWriter out ) throws IOException {
139         final int count = same_protein_seqs.size();
140         if ( count == 1 ) {
141             out.println( same_protein_seqs.get( 0 ) );
142         }
143         else {
144             int c = 1;
145             for( final String s : same_protein_seqs ) {
146                 out.println( new StringBuffer( s ).insert( s.indexOf( "|" ), "__" + c + "_OF_" + count ).toString() );
147                 c++;
148             }
149         }
150     }
151 }