X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FSequenceAccessionTools.java;h=cce1b2e578d469863cce64fe343f6fbe2623ceb3;hb=a1114eb8610e592961a40e5c3d46d647c02b5108;hp=4136049ad6ff6eee146ffc8c7db42ee67c1afce4;hpb=3171b9b28762e02b3fbd9b5f8a5b1946497f2178;p=jalview.git diff --git a/forester/java/src/org/forester/util/SequenceAccessionTools.java b/forester/java/src/org/forester/util/SequenceAccessionTools.java index 4136049..cce1b2e 100644 --- a/forester/java/src/org/forester/util/SequenceAccessionTools.java +++ b/forester/java/src/org/forester/util/SequenceAccessionTools.java @@ -33,16 +33,11 @@ import java.util.regex.Pattern; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.Accession; +import org.forester.phylogeny.data.Accession.Source; import org.forester.phylogeny.data.Sequence; public final class SequenceAccessionTools { - public final static Pattern UNIPROT_KB_PATTERN_0 = Pattern - .compile( "\\b([A-Z][0-9][A-Z0-9]{3}[0-9])\\b" ); - public final static Pattern UNIPROT_KB_PATTERN_1 = Pattern - .compile( "(?:\\b|_)(?:sp|tr)[\\.|\\-_=/\\\\]([A-Z][0-9][A-Z0-9]{3}[0-9])(?:\\b|_)" ); - public final static Pattern UNIPROT_KB_PATTERN_2 = Pattern - .compile( "(?:\\b|_)(?:[A-Z0-9]{2,5}|(?:[A-Z][0-9][A-Z0-9]{3}[0-9]))_(([A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA)(?:\\b|_)" ); // gb_ADF31344_1_segmented_worms_ // gb_AAA96518_1 // gb_EHB07727_1_rodents_ @@ -56,230 +51,214 @@ public final class SequenceAccessionTools { //Nucleotide: 1 letter + 5 numerals OR 2 letters + 6 numerals //Protein: 3 letters + 5 numerals //http://www.ncbi.nlm.nih.gov/Sequin/acc.html - private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_1 = Pattern - .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); - private final static Pattern GENBANK_NUCLEOTIDE_AC_PATTERN_2 = Pattern - .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}\\d{6}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); - private final static Pattern GENBANK_PROTEIN_AC_PATTERN = Pattern - .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{3}\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); - private final static Pattern GI_PATTERN = Pattern - .compile( "(?:\\b|_)(?:GI|gi)[|_=:](\\d+)(?:\\b|_)" ); + public final static Pattern GENBANK_NUC_PATTERN_1 = Pattern + .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); + public final static Pattern GENBANK_NUC_PATTERN_2 = Pattern + .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}\\d{6}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); + public final static Pattern GENBANK_PROT_PATTERN = Pattern + .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{3}\\d{5}(?:\\.\\d+)?)(?:[^a-zA-Z0-9]|\\Z)" ); + public final static Pattern GI_PATTERN = Pattern.compile( "(?:\\b|_)(?:GI|gi)[|_=:](\\d+)(?:\\b|_)" ); + public final static Pattern UNIPROT_KB_PATTERN_0 = Pattern + .compile( "(?:\\b|_)([A-Z][0-9][A-Z0-9]{3}[0-9])(?:\\b|_)" ); + public final static Pattern UNIPROT_KB_PATTERN_1 = Pattern + .compile( "(?:\\b|_)(?:sp|tr)[\\.|\\-_=/\\\\]([A-Z][0-9][A-Z0-9]{3}[0-9])(?:\\b|_)" ); + public final static Pattern UNIPROT_KB_PATTERN_2 = Pattern + .compile( "(?:\\b|_)(?:[A-Z0-9]{2,5}|(?:[A-Z][0-9][A-Z0-9]{3}[0-9]))_(([A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA)(?:\\b|_)" ); // RefSeq accession numbers can be distinguished from GenBank accessions // by their distinct prefix format of 2 characters followed by an // underscore character ('_'). For example, a RefSeq protein accession is NP_015325. - private final static Pattern REFSEQ_PATTERN = Pattern - .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}_\\d{6,})(?:[^a-zA-Z0-9]|\\Z)" ); + private final static Pattern REFSEQ_PATTERN = Pattern + .compile( "(?:\\A|.*[^a-zA-Z0-9])([A-Z]{2}_\\d{6,})(?:[^a-zA-Z0-9]|\\Z)" ); private SequenceAccessionTools() { // Hiding the constructor. } - public static String extractGenbankAccessor( final PhylogenyNode node ) { - String v = null; - if ( node.getNodeData().isHasSequence() ) { - final Sequence seq = node.getNodeData().getSequence(); - if ( !ForesterUtil.isEmpty( seq.getSymbol() ) ) { - v = parseGenbankAccessor( seq.getSymbol() ); - } - if ( !ForesterUtil.isEmpty( seq.getGeneName() ) ) { - v = parseGenbankAccessor( seq.getGeneName() ); - } - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( seq.getName() ) ) { - v = parseGenbankAccessor( seq.getName() ); - } - if ( ForesterUtil.isEmpty( v ) && ( node.getNodeData().getSequence().getAccession() != null ) - && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { - v = parseGenbankAccessor( seq.getAccession().getValue() ); - } + public final static boolean isProteinDbQuery( final String query ) { + final String r1 = parseRefSeqAccessorFromString( query ); + if ( !ForesterUtil.isEmpty( r1 ) && ( r1.charAt( 1 ) == 'P' ) ) { + return true; + } + final String r2 = parseUniProtAccessorFromString( query ); + if ( !ForesterUtil.isEmpty( r2 ) ) { + return true; } - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( node.getName() ) ) { - v = parseGenbankAccessor( node.getName() ); + return GENBANK_PROT_PATTERN.matcher( query ).lookingAt(); + } + + public final static Accession obtainAccessorFromDataFields( final PhylogenyNode n ) { + String a = obtainUniProtAccessorFromDataFields( n ); + if ( !ForesterUtil.isEmpty( a ) ) { + return new Accession( a, Source.UNIPROT ); + } + a = obtainGenbankAccessorFromDataFields( n ); + if ( !ForesterUtil.isEmpty( a ) ) { + return new Accession( a, Source.NCBI ); } - return v; + a = obtainRefSeqAccessorFromDataFields( n ); + if ( !ForesterUtil.isEmpty( a ) ) { + return new Accession( a, Source.REFSEQ ); + } + a = obtainGiNumberFromDataFields( n ); + if ( !ForesterUtil.isEmpty( a ) ) { + return new Accession( a, Source.GI ); + } + return null; } - public static String extractGInumber( final PhylogenyNode node ) { - String v = null; - if ( node.getNodeData().isHasSequence() ) { - final Sequence seq = node.getNodeData().getSequence(); - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( seq.getName() ) ) { - v = parseGInumber( seq.getName() ); + public final static Accession obtainFromSeqAccession( final PhylogenyNode n ) { + if ( n.getNodeData().isHasSequence() && ( n.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getAccession().getSource() ) + && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getAccession().getValue() ) ) { + final String source = n.getNodeData().getSequence().getAccession().getSource().toLowerCase(); + final String value = n.getNodeData().getSequence().getAccession().getValue(); + if ( ( source.startsWith( "uniprot" ) || source.equals( "swissprot" ) || source.equals( "trembl" ) || source + .equals( "sp" ) ) ) { + return new Accession( value, Source.UNIPROT ); } - if ( ForesterUtil.isEmpty( v ) && ( node.getNodeData().getSequence().getAccession() != null ) - && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { - v = parseGInumber( seq.getAccession().getValue() ); + else if ( source.equals( "embl" ) || source.equals( "ebi" ) ) { + return new Accession( value, Source.EMBL ); + } + else if ( source.equals( "ncbi" ) || source.equals( "genbank" ) ) { + return new Accession( value, Source.NCBI ); + } + else if ( source.equals( "refseq" ) ) { + return new Accession( value, Source.REFSEQ ); + } + else if ( source.equals( "gi" ) ) { + return new Accession( value, Source.GI ); } } - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( node.getName() ) ) { - v = parseGInumber( node.getName() ); - } - return v; + return null; } - public static String extractRefSeqAccessor( final PhylogenyNode node ) { - String v = null; - if ( node.getNodeData().isHasSequence() ) { - final Sequence seq = node.getNodeData().getSequence(); + public final static String obtainGenbankAccessorFromDataFields( final PhylogenyNode n ) { + String a = null; + if ( n.getNodeData().isHasSequence() ) { + final Sequence seq = n.getNodeData().getSequence(); if ( !ForesterUtil.isEmpty( seq.getSymbol() ) ) { - v = parseRefSeqAccessor( seq.getSymbol() ); + a = parseGenbankAccessorFromString( seq.getSymbol() ); } if ( !ForesterUtil.isEmpty( seq.getGeneName() ) ) { - v = parseRefSeqAccessor( seq.getGeneName() ); + a = parseGenbankAccessorFromString( seq.getGeneName() ); } - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( seq.getName() ) ) { - v = parseRefSeqAccessor( seq.getName() ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getName() ) ) { + a = parseGenbankAccessorFromString( seq.getName() ); } - if ( ForesterUtil.isEmpty( v ) && ( node.getNodeData().getSequence().getAccession() != null ) + if ( ForesterUtil.isEmpty( a ) && ( n.getNodeData().getSequence().getAccession() != null ) && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { - v = parseRefSeqAccessor( seq.getAccession().getValue() ); + a = parseGenbankAccessorFromString( seq.getAccession().getValue() ); } } - if ( ForesterUtil.isEmpty( v ) && !ForesterUtil.isEmpty( node.getName() ) ) { - v = parseRefSeqAccessor( node.getName() ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( n.getName() ) ) { + a = parseGenbankAccessorFromString( n.getName() ); } - return v; + return a; } - public static String extractUniProtKbProteinSeqIdentifier( final PhylogenyNode node ) { + public final static String obtainGiNumberFromDataFields( final PhylogenyNode n ) { String a = null; - if ( node.getNodeData().isHasSequence() ) { - final Sequence seq = node.getNodeData().getSequence(); - if ( !ForesterUtil.isEmpty( seq.getSymbol() ) ) { - a = SequenceAccessionTools.extractUniProtKbProteinSeqIdentifier( seq.getSymbol() ); - } + if ( n.getNodeData().isHasSequence() ) { + final Sequence seq = n.getNodeData().getSequence(); if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getName() ) ) { - a = SequenceAccessionTools.extractUniProtKbProteinSeqIdentifier( seq.getName() ); + a = parseGInumberFromString( seq.getName() ); } if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getGeneName() ) ) { - a = SequenceAccessionTools.extractUniProtKbProteinSeqIdentifier( seq.getGeneName() ); + a = parseGInumberFromString( seq.getGeneName() ); } - if ( ForesterUtil.isEmpty( a ) && ( node.getNodeData().getSequence().getAccession() != null ) + if ( ForesterUtil.isEmpty( a ) && ( n.getNodeData().getSequence().getAccession() != null ) && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { - a = SequenceAccessionTools.extractUniProtKbProteinSeqIdentifier( seq.getAccession().getValue() ); + a = parseGInumberFromString( seq.getAccession().getValue() ); } } - if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( node.getName() ) ) { - a = SequenceAccessionTools.extractUniProtKbProteinSeqIdentifier( node.getName() ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( n.getName() ) ) { + a = parseGInumberFromString( n.getName() ); } return a; } - public static String extractUniProtKbProteinSeqIdentifier( final String str ) { - Matcher m = UNIPROT_KB_PATTERN_0.matcher( str ); - if ( m.find() ) { - return m.group( 1 ); - } - m = UNIPROT_KB_PATTERN_1.matcher( str ); - if ( m.find() ) { - return m.group( 1 ); - } - m = UNIPROT_KB_PATTERN_2.matcher( str ); - if ( m.find() ) { - return m.group(); - } - return null; - } - - public final static boolean isProtein( final String query ) { - final String r1 = parseRefSeqAccessor( query ); - if ( !ForesterUtil.isEmpty( r1 ) && ( r1.charAt( 1 ) == 'P' ) ) { - return true; - } - final String r2 = extractUniProtKbProteinSeqIdentifier( query ); - if ( !ForesterUtil.isEmpty( r2 ) ) { - return true; - } - return GENBANK_PROTEIN_AC_PATTERN.matcher( query ).lookingAt(); - } - - public final static Accession parse( final PhylogenyNode n ) { - String v = extractUniProtKbProteinSeqIdentifier( n ); - if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.UNIPROT ); - } - v = extractGenbankAccessor( n ); - if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.NCBI ); - } - v = extractRefSeqAccessor( n ); - if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.REFSEQ ); + public final static String obtainRefSeqAccessorFromDataFields( final PhylogenyNode n ) { + String a = null; + if ( n.getNodeData().isHasSequence() ) { + final Sequence seq = n.getNodeData().getSequence(); + if ( !ForesterUtil.isEmpty( seq.getSymbol() ) ) { + a = parseRefSeqAccessorFromString( seq.getSymbol() ); + } + if ( !ForesterUtil.isEmpty( seq.getGeneName() ) ) { + a = parseRefSeqAccessorFromString( seq.getGeneName() ); + } + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getName() ) ) { + a = parseRefSeqAccessorFromString( seq.getName() ); + } + if ( ForesterUtil.isEmpty( a ) && ( n.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { + a = parseRefSeqAccessorFromString( seq.getAccession().getValue() ); + } } - v = extractGInumber( n ); - if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.GI ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( n.getName() ) ) { + a = parseRefSeqAccessorFromString( n.getName() ); } - return null; + return a; } - public final static Accession obtainFromSeqAccession( final PhylogenyNode node ) { - if ( node.getNodeData().isHasSequence() && ( node.getNodeData().getSequence().getAccession() != null ) - && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getSource() ) - && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().getValue() ) ) { - final String source = node.getNodeData().getSequence().getAccession().getSource().toLowerCase(); - final String value = node.getNodeData().getSequence().getAccession().getValue(); - if ( ( source.startsWith( "uniprot" ) || source.equals( "swissprot" ) || source.equals( "trembl" ) || source - .equals( "sp" ) ) ) { - return new Accession( value, Accession.UNIPROT ); - } - else if ( source.equals( "embl" ) || source.equals( "ebi" ) ) { - return new Accession( value, Accession.EMBL ); + public final static String obtainUniProtAccessorFromDataFields( final PhylogenyNode n ) { + String a = null; + if ( n.getNodeData().isHasSequence() ) { + final Sequence seq = n.getNodeData().getSequence(); + if ( !ForesterUtil.isEmpty( seq.getSymbol() ) ) { + a = SequenceAccessionTools.parseUniProtAccessorFromString( seq.getSymbol() ); } - else if ( source.equals( "ncbi" ) || source.equals( "genbank" ) ) { - return new Accession( value, Accession.NCBI ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getName() ) ) { + a = SequenceAccessionTools.parseUniProtAccessorFromString( seq.getName() ); } - else if ( source.equals( "refseq" ) ) { - return new Accession( value, Accession.REFSEQ ); + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( seq.getGeneName() ) ) { + a = SequenceAccessionTools.parseUniProtAccessorFromString( seq.getGeneName() ); } - else if ( source.equals( "gi" ) ) { - return new Accession( value, Accession.GI ); + if ( ForesterUtil.isEmpty( a ) && ( n.getNodeData().getSequence().getAccession() != null ) + && !ForesterUtil.isEmpty( seq.getAccession().getValue() ) ) { + a = SequenceAccessionTools.parseUniProtAccessorFromString( seq.getAccession().getValue() ); } } - return null; + if ( ForesterUtil.isEmpty( a ) && !ForesterUtil.isEmpty( n.getName() ) ) { + a = SequenceAccessionTools.parseUniProtAccessorFromString( n.getName() ); + } + return a; } - /** - * Returns null if no match. - * - */ - public final static Accession parse( final String s ) { + public final static Accession parseAccessorFromString( final String s ) { if ( !ForesterUtil.isEmpty( s ) ) { - String v = extractUniProtKbProteinSeqIdentifier( s ); + String v = parseUniProtAccessorFromString( s ); if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.UNIPROT ); + return new Accession( v, Source.UNIPROT ); } - v = parseGenbankAccessor( s ); + v = parseGenbankAccessorFromString( s ); if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.NCBI ); + return new Accession( v, Source.NCBI ); } - v = parseRefSeqAccessor( s ); + v = parseRefSeqAccessorFromString( s ); if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.REFSEQ ); + return new Accession( v, Source.REFSEQ ); } - v = parseGInumber( s ); + v = parseGInumberFromString( s ); if ( !ForesterUtil.isEmpty( v ) ) { - return new Accession( v, Accession.GI ); + return new Accession( v, Source.GI ); } } return null; } - /** - * Returns null if no match. - * - */ - public static String parseGenbankAccessor( final String query ) { - Matcher m = GENBANK_NUCLEOTIDE_AC_PATTERN_1.matcher( query ); + public final static String parseGenbankAccessorFromString( final String s ) { + Matcher m = GENBANK_NUC_PATTERN_1.matcher( s ); if ( m.lookingAt() ) { return m.group( 1 ); } else { - m = GENBANK_NUCLEOTIDE_AC_PATTERN_2.matcher( query ); + m = GENBANK_NUC_PATTERN_2.matcher( s ); if ( m.lookingAt() ) { return m.group( 1 ); } else { - m = GENBANK_PROTEIN_AC_PATTERN.matcher( query ); + m = GENBANK_PROT_PATTERN.matcher( s ); if ( m.lookingAt() ) { return m.group( 1 ); } @@ -290,8 +269,8 @@ public final class SequenceAccessionTools { } } - public static String parseGenbankProteinAccessor( final String query ) { - final Matcher m = GENBANK_PROTEIN_AC_PATTERN.matcher( query ); + public final static String parseGenbankProteinAccessorFromString( final String s ) { + final Matcher m = GENBANK_PROT_PATTERN.matcher( s ); if ( m.lookingAt() ) { return m.group( 1 ); } @@ -300,23 +279,35 @@ public final class SequenceAccessionTools { } } - public static String parseGInumber( final String query ) { - final Matcher m = GI_PATTERN.matcher( query ); + public final static String parseGInumberFromString( final String s ) { + final Matcher m = GI_PATTERN.matcher( s ); if ( m.find() ) { return m.group( 1 ); } return null; } - /** - * Returns null if no match. - * - */ - public final static String parseRefSeqAccessor( final String query ) { - final Matcher m = REFSEQ_PATTERN.matcher( query ); + public final static String parseRefSeqAccessorFromString( final String s ) { + final Matcher m = REFSEQ_PATTERN.matcher( s ); if ( m.lookingAt() ) { return m.group( 1 ); } return null; } + + public final static String parseUniProtAccessorFromString( final String s ) { + Matcher m = UNIPROT_KB_PATTERN_1.matcher( s ); + if ( m.find() ) { + return m.group( 1 ); + } + m = UNIPROT_KB_PATTERN_2.matcher( s ); + if ( m.find() ) { + return m.group(); + } + m = UNIPROT_KB_PATTERN_0.matcher( s ); + if ( m.find() ) { + return m.group( 1 ); + } + return null; + } }