2 package org.forester.application;
4 import java.io.FileInputStream;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashSet;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
13 import org.forester.io.parsers.FastaParser;
14 import org.forester.sequence.Sequence;
15 import org.forester.util.ForesterUtil;
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.
22 public static void main( final String args[] ) {
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() );
31 if ( matcher.find() ) {
32 gn = matcher.group( 1 );
35 System.out.println( "ERROR: no gene for: " + seq.getIdentifier() );
38 new_seqs.add( ">" + gn + "|" + seq.getIdentifier() + "\n" + seq.getMolecularSequenceAsString() );
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 );
51 if ( matcher_ra.find() ) {
52 range = matcher_ra.group( 1 );
55 System.out.println( "ERROR: no range for: " + seq );
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();
69 System.out.println( seq );
73 for( final String s : same_protein_seqs ) {
74 System.out.println( new StringBuffer( s ).insert( s.indexOf( "|" ),
75 "__" + c + "_OF_" + count )
82 System.out.println( seq );
86 duplicate_counter_mol_seq++;
90 duplicate_counter_gn_ra++;
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 );
97 catch ( final Exception e ) {