[Biojava-l] Using Annotation to Model a Tree Hierarchy

Ron Kuhn rkuhn@Cellomics.com
Thu, 18 Oct 2001 11:06:02 -0400


Here are the changes to SwissprotProcessor.java and GenbankProcessor.java:

1) Add import statement:
import org.biojava.utils.ChangeVetoException;

2) Add a new class member variable:
	private Annotation mReference = null;	// holds the latest
references

3) At the beginning of the endSequence method, reset the variable for the
next seq:
		mReference = null;	// remove ref to last reference in
sequence

4SP) Add code to handle the reference block: For Swissprot, use:
Note: This method assumes that only and all reference tags begin with an R.
This is OK for now. For an alternative, see Genbank method below.

	public void addSequenceProperty(Object key, Object value) throws
ParseException
	{
		try
		{
			if(mBadFeature)
			{
				// If this feature is bad in some way,
ignore it.
				String featureLine = value.toString();
				if((key.equals("FT")) &&
(featureLine.charAt(0) != ' '))
				{
					// If the offending feature is past,
start reading data again
					mBadFeature = false;
	
features.startFeature(featureLine.substring(0, 8).trim());
	
features.featureData(featureLine.substring(9));
				}
			}
			else
			{
		    	// Tidy up any end-of-block jobbies
				if (features.inFeature() &&
!key.equals("FT"))
				{
					features.endFeature();
				}

				if (key.equals("FT"))
				{
					String featureLine =
value.toString();
					if (featureLine.charAt(0) != ' ')
					{
						// This is a featuretype
field
						if (features.inFeature())
						{
	
features.endFeature();
						}

	
features.startFeature(featureLine.substring(0, 8).trim());
					}
	
features.featureData(featureLine.substring(9));
				}
				// RAK Add Reference block
				else if (key.toString().startsWith("R"))
// if reference type key
				{
					if (key.equals("RN"))	// new
reference
					{
						mReference = new
SimpleAnnotation();
	
getDelegate().addSequenceProperty(key, mReference);
					}
					if (mReference == null)
					{
						ParseErrorEvent e = new
ParseErrorEvent(this, "Reference key outside reference block: " +
key.toString() + "=" + value.toString());
						notifyParseErrorEvent(e);
					}
					else
					{
						if
(mReference.containsProperty(key))
						{
							Object oldValue =
mReference.getProperty(key);
							if (oldValue
instanceof Collection)
	
((Collection) oldValue).add(value);
							else
							{
								List
newValue = new ArrayList();
	
newValue.add(oldValue);
	
newValue.add(value);
	
mReference.setProperty(key, newValue);
							}
						}
						else
	
mReference.setProperty(key, value);
					}
				}
				else
				{
	
getDelegate().addSequenceProperty(key, value);

					if (key.equals("AC"))
					{
						String acc=
value.toString();
						StringTokenizer toke = new
StringTokenizer(acc, "; ");
						while (toke.hasMoreTokens())
						{
	
accessions.add(toke.nextToken());
						}
					}
				}
			}
		}
		catch (ChangeVetoException ex)	// RAK
		{
			ParseErrorEvent e = new ParseErrorEvent(this, "This
reference line could not be handled: " + value.toString());
			this.notifyParseErrorEvent(e);
		}
		catch (BioException ex)
		{
			// If an exception is thrown, read past the
offending feature
			mBadFeature = true;
			ParseErrorEvent offendingLineEvent = new
ParseErrorEvent(this, "This line could not be parsed: " + value.toString());
			this.notifyParseErrorEvent(offendingLineEvent);
	    }
	    catch (IndexOutOfBoundsException ex)
	    {
	    	// This occurs when for some line min > max
	    	mBadFeature = true;
	    	ParseErrorEvent offendingLineEvent = new
ParseErrorEvent(this, "From must be less than To: " + value.toString());
			this.notifyParseErrorEvent(offendingLineEvent);
	    }
	}
4GB) Add code to handle the reference block: For Genbank, use:
Define above:
	static final String REFERENCE_KEYS = "REFERENCE AUTHORS TITLE
JOURNAL MEDLINE PUBMED REMARK";

And replace the addSequenceProperty method with:

	public void addSequenceProperty(Object key, Object value) throws
ParseException
	{
		try
		{
			if(mBadFeature)
			{
				// If this feature is bad in some way,
ignore it.
				String featureLine = value.toString();
				if((key.equals(GenbankFormat.FEATURE_FLAG))
&& (featureLine.charAt(0) != ' '))
				{
					// If the offending feature is past,
start reading data again
					mBadFeature = false;
	
features.startFeature(featureLine.substring(0, 16).trim());
	
features.featureData(featureLine.substring(16));
				}
			}
			else
			{
				if (features.inFeature() &&
!(key.equals(GenbankFormat.FEATURE_FLAG)))
				{
					features.endFeature();
				}

				if(key.equals(GenbankFormat.FEATURE_FLAG))
				{
					String featureLine =
value.toString();
					if (featureLine.charAt(0) != ' ')
					{
						// This is a featuretype
field
						if (features.inFeature())
						{
	
features.endFeature();
						}
	
features.startFeature(featureLine.substring(0, 16).trim());
					}
	
features.featureData(featureLine.substring(16));
				}
				// RAK Add Reference block
				else if
(REFERENCE_KEYS.indexOf(key.toString()) >= 0)	// if reference type key
				{
					if (key.equals("REFERENCE"))	//
new reference
					{
						mReference = new
SimpleAnnotation();
	
getDelegate().addSequenceProperty(key, mReference);
					}
					if (mReference == null)
					{
						ParseErrorEvent e = new
ParseErrorEvent(this, "Reference key outside reference block: " +
key.toString() + "=" + value.toString());
						notifyParseErrorEvent(e);
					}
					else
					{
						if
(mReference.containsProperty(key))
						{
							Object oldValue =
mReference.getProperty(key);
							if (oldValue
instanceof Collection)
	
((Collection) oldValue).add(value);
							else
							{
								List
newValue = new ArrayList();
	
newValue.add(oldValue);
	
newValue.add(value);
	
mReference.setProperty(key, newValue);
							}
						}
						else
	
mReference.setProperty(key, value);
					}
				}
				else
				{
	
getDelegate().addSequenceProperty(key, value);
					if
(key.equals(GenbankFormat.ACCESSION_TAG))
					{
						accessions.add(value);
					}
				}
			}
		}
		catch (ChangeVetoException ex)	// RAK
		{
			ParseErrorEvent e = new ParseErrorEvent(this, "This
reference line could not be handled: " + value.toString());
			this.notifyParseErrorEvent(e);
		}
		catch (BioException ex)
		{
			// If an exception is thrown, notify the listeners
and read past
			// the offending feature
			mBadFeature = true;
			ParseErrorEvent offendingLineEvent = new
ParseErrorEvent(this, "This line could not be parsed: " + value.toString());
			this.notifyParseErrorEvent(offendingLineEvent);
		}
	    catch (IndexOutOfBoundsException ex)
	    {
	    	// This occurs when for some line min > max
	    	mBadFeature = true;
	    	ParseErrorEvent offendingLineEvent = new
ParseErrorEvent(this, "From must be less than To: " + value.toString());
			this.notifyParseErrorEvent(offendingLineEvent);
	    }
	}

Ron
-----Original Message-----
From: Thomas Down [mailto:td2@sanger.ac.uk]
Sent: Thursday, October 18, 2001 10:36 AM
To: Ron Kuhn
Cc: biojava-l@biojava.org
Subject: Re: [Biojava-l] Using Annotation to Model a Tree Hierarchy


On Thu, Oct 18, 2001 at 09:15:00AM -0400, Ron Kuhn wrote:
> Sure... How do I get my changes into the Biojava???

If you're just changing a few files, a good approach is
just to send the code to this mailing list -- we'll take
a look then (with your permission) check it into the
repository.

[If you become a regular contributor, we can create an
account which gives you direct access to the CVS repository,
but that's probably more trouble that it's worth for a single
checkin].

Thanks,

   Thomas.