X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fio%2FEmblFlatFile.java;h=33701f6934c991f3918174c268523b544d9073bf;hb=a9177efb4097815d9a8031aa22700c61a7d6260a;hp=9214f7e7963ab31a967f91d104018e54150865a8;hpb=98b152cadada4da85e79a572459f753ae8d92f4e;p=jalview.git
diff --git a/src/jalview/io/EmblFlatFile.java b/src/jalview/io/EmblFlatFile.java
index 9214f7e..33701f6 100644
--- a/src/jalview/io/EmblFlatFile.java
+++ b/src/jalview/io/EmblFlatFile.java
@@ -1,23 +1,30 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.io;
import java.io.IOException;
-import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import jalview.bin.Cache;
+import jalview.bin.Console;
import jalview.datamodel.DBRefEntry;
-import jalview.datamodel.DBRefSource;
-import jalview.datamodel.FeatureProperties;
-import jalview.datamodel.Sequence;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceI;
import jalview.util.DBRefUtils;
-import jalview.util.DnaUtils;
-import jalview.util.MappingUtils;
/**
* A class that provides selective parsing of the EMBL flatfile format.
@@ -38,47 +45,10 @@ import jalview.util.MappingUtils;
* @see ftp://ftp.ebi.ac.uk/pub/databases/ena/sequence/release/doc/usrman.txt
* @see ftp://ftp.ebi.ac.uk/pub/databases/embl/doc/FT_current.html
*/
-public class EmblFlatFile extends AlignFile // FileParse
+public class EmblFlatFile extends EMBLLikeFlatFile
{
/**
- * A data bean class to hold values parsed from one CDS Feature (FT)
- */
- class CdsData
- {
- String translation; // from CDS feature /translation
-
- String cdsLocation; // CDS /location raw value
-
- int codonStart = 1; // from CDS /codon_start
-
- String proteinName; // from CDS /product; TODO: use for protein description
-
- String proteinId; // from CDS /protein_id
-
- Map cdsProps = new Hashtable<>(); // CDS other qualifiers
- }
-
- private static final String WHITESPACE = "\\s+";
-
- private String sourceDb;
-
- /*
- * values parsed from the EMBL flatfile record
- */
- private String accession; // from ID (first token)
-
- private String version; // from ID (second token)
-
- private int length = 128; // from ID (7th token), with usable default
-
- private List dbrefs; // from DR and also CDS /db_xref qualifiers
-
- private String sequenceString; // from SQ lines
-
- private List cds;
-
- /**
- * Constructor
+ * Constructor given a data source and the id of the source database
*
* @param fp
* @param sourceId
@@ -86,10 +56,7 @@ public class EmblFlatFile extends AlignFile // FileParse
*/
public EmblFlatFile(FileParse fp, String sourceId) throws IOException
{
- super(false, fp); // don't parse immediately
- this.sourceDb = sourceId;
- dbrefs = new ArrayList<>();
- cds = new ArrayList<>();
+ super(fp, sourceId);
}
/**
@@ -98,6 +65,7 @@ public class EmblFlatFile extends AlignFile // FileParse
*
* @throws IOException
*/
+ @Override
public void parse() throws IOException
{
String line = nextLine();
@@ -107,24 +75,28 @@ public class EmblFlatFile extends AlignFile // FileParse
{
line = parseID(line);
}
+ else if (line.startsWith("DE"))
+ {
+ line = parseDE(line);
+ }
else if (line.startsWith("DR"))
{
line = parseDR(line);
}
else if (line.startsWith("SQ"))
{
- line = parseSQ();
+ line = parseSequence();
}
else if (line.startsWith("FT"))
{
- line = parseFT(line);
+ line = parseFeature(line.substring(2));
}
else
{
line = nextLine();
}
}
- assembleSequence();
+ buildSequence();
}
/**
@@ -172,7 +144,7 @@ public class EmblFlatFile extends AlignFile // FileParse
this.length = Integer.valueOf(bits[0]);
} catch (NumberFormatException e)
{
- Cache.log.error("bad length read in flatfile, line: " + line);
+ Console.error("bad length read in flatfile, line: " + line);
}
}
@@ -180,6 +152,38 @@ public class EmblFlatFile extends AlignFile // FileParse
}
/**
+ * Reads sequence description from the first DE line found. Any trailing
+ * period is discarded. If there are multiple DE lines, only the first (short
+ * description) is read, the rest are ignored.
+ *
+ * @param line
+ * @return
+ * @throws IOException
+ */
+ String parseDE(String line) throws IOException
+ {
+ String desc = line.substring(2).trim();
+ if (desc.endsWith("."))
+ {
+ desc = desc.substring(0, desc.length() - 1);
+ }
+ this.description = desc;
+
+ /*
+ * pass over any additional DE lines
+ */
+ while ((line = nextLine()) != null)
+ {
+ if (!line.startsWith("DE"))
+ {
+ break;
+ }
+ }
+
+ return line;
+ }
+
+ /**
* Processes one DR line and saves as a DBRefEntry cross-reference. Returns
* the line following the line processed.
*
@@ -208,7 +212,7 @@ public class EmblFlatFile extends AlignFile // FileParse
if (!secondaryId.isEmpty())
{
// todo: is this right? secondary id is not a version number
- // version = secondaryId;
+ // version = secondaryId;
}
}
this.dbrefs.add(new DBRefEntry(db, version, acc));
@@ -217,301 +221,9 @@ public class EmblFlatFile extends AlignFile // FileParse
return nextLine();
}
- /**
- * Reads and saves the sequence, read from the lines following the SQ line.
- * Whitespace and position counters are discarded. Returns the next line
- * following the sequence data (the next line that doesn't start with
- * whitespace).
- *
- * @throws IOException
- */
- String parseSQ() throws IOException
- {
- StringBuilder sb = new StringBuilder(this.length);
- String line = nextLine();
- while (line != null && line.startsWith(" "))
- {
- line = line.trim();
- String[] blocks = line.split(WHITESPACE);
-
- /*
- * omit the last block (position counter) on each line
- */
- for (int i = 0; i < blocks.length - 1; i++)
- {
- sb.append(blocks[i]);
- }
- line = nextLine();
- }
- this.sequenceString = sb.toString();
-
- return line;
- }
-
- /**
- * Processes an FT line. If it declares a feature type of interest (currently,
- * only CDS is processed), processes all of the associated lines (feature
- * qualifiers), and returns the next line after that, otherwise simply returns
- * the next line.
- *
- * @param line
- * @return
- * @throws IOException
- */
- String parseFT(String line) throws IOException
- {
- String[] tokens = line.split(WHITESPACE);
- if (tokens.length < 3 || !"CDS".equals(tokens[1]))
- {
- return nextLine();
- }
-
- CdsData data = new CdsData();
- data.cdsLocation = tokens[2];
-
- line = nextLine();
- while (line != null)
- {
- if (!line.startsWith("FT ")) // 4 spaces
- {
- // e.g. start of next feature "FT source..."
- break;
- }
-
- /*
- * extract qualifier, e.g. FT /protein_id="CAA37824.1"
- */
- int slashPos = line.indexOf('/');
- if (slashPos == -1)
- {
- Cache.log.error("Unexpected EMBL line ignored: " + line);
- continue;
- }
- int eqPos = line.indexOf('=', slashPos + 1);
- if (eqPos == -1)
- {
- Cache.log.error("Unexpected EMBL line ignored: " + line);
- continue;
- }
- String qualifier = line.substring(slashPos + 1, eqPos);
- String value = line.substring(eqPos + 1);
- if (value.startsWith("\"") && value.endsWith("\""))
- {
- value = value.substring(1, value.length() - 1);
- }
-
- if ("protein_id".equals(qualifier))
- {
- data.proteinId = value;
- line = nextLine();
- }
- else if ("codon_start".equals(qualifier))
- {
- try
- {
- data.codonStart = Integer.parseInt(value.trim());
- } catch (NumberFormatException e)
- {
- Cache.log.error("Invalid codon_start in XML for " + this.accession
- + ": " + e.getMessage());
- }
- line = nextLine();
- }
- else if ("db_xref".equals(qualifier))
- {
- String[] parts = value.split(":");
- if (parts.length == 2)
- {
- String db = parts[0].trim();
- db = DBRefUtils.getCanonicalName(db);
- DBRefEntry dbref = new DBRefEntry(db, "0", parts[1].trim());
- this.dbrefs.add(dbref);
- }
- line = nextLine();
- }
- else if ("product".equals(qualifier))
- {
- // sometimes name is returned e.g. for V00488
- data.proteinName = value;
- line = nextLine();
- }
- else if ("translation".equals(qualifier))
- {
- line = readTranslation(value, data);
- }
- else if (!"".equals(value))
- {
- // throw anything else into the additional properties hash
- data.cdsProps.put(qualifier, value);
- line = nextLine();
- }
- }
-
- this.cds.add(data);
-
- return line;
- }
-
- /**
- * Reads and returns the CDS translation from one or more lines of the file,
- * and returns the next line after that
- *
- * @param value
- * the first line of the translation (likely quoted)
- * @param data
- * @return
- * @throws IOException
- */
- String readTranslation(String value, CdsData data) throws IOException
- {
- StringBuilder sb = new StringBuilder(this.length / 3 + 1);
- sb.append(value.replace("\"", ""));
-
- String line;
- while ((line = nextLine()) != null)
- {
- if (!line.startsWith("FT "))
- {
- break; // reached next feature or other input line
- }
- String[] tokens = line.split(WHITESPACE);
- if (tokens.length < 2)
- {
- Cache.log.error("Ignoring bad EMBL line: " + line);
- break;
- }
- if (tokens[1].startsWith("/"))
- {
- break; // next feature qualifier
- }
- sb.append(tokens[1].replace("\"", ""));
- }
-
- data.translation = sb.toString();
-
- return line;
- }
-
- /**
- * Processes the parsed CDS feature data to
- *
- * - add a CDS feature to the sequence for each CDS start-end range
- * - create a protein product sequence for the translation
- * - create a cross-reference to protein with mapping from dna
- * - add any CDS dbrefs to the sequence and to the protein product
- *
- *
- * @param SequenceI
- * dna
- */
- void processCDS(SequenceI dna, CdsData data)
- {
- /*
- * parse location into a list of [start, end, start, end] positions
- */
- int[] exons = getCdsRanges(this.accession, data.cdsLocation);
- int exonNumber = 0;
-
- for (int xint = 0; exons != null && xint < exons.length - 1; xint += 2)
- {
- int exonStart = exons[xint];
- int exonEnd = exons[xint + 1];
- int begin = Math.min(exonStart, exonEnd);
- int end = Math.max(exonStart, exonEnd);
- exonNumber++;
- String desc = String.format("Exon %d for protein EMBLCDS:%s",
- exonNumber, data.proteinId);
-
- SequenceFeature sf = new SequenceFeature("CDS", desc, begin, end,
- this.sourceDb);
- for (Entry val : data.cdsProps.entrySet())
- {
- sf.setValue(val.getKey(), val.getValue());
- }
-
- sf.setEnaLocation(data.cdsLocation);
- boolean forwardStrand = exonStart <= exonEnd;
- sf.setStrand(forwardStrand ? "+" : "-");
- sf.setPhase(String.valueOf(data.codonStart - 1));
- sf.setValue(FeatureProperties.EXONPOS, exonNumber);
- sf.setValue(FeatureProperties.EXONPRODUCT, data.proteinName);
-
- dna.addSequenceFeature(sf);
- }
- }
-
- /**
- * Constructs a sequence for the protein product (if there is one), and dbrefs
- * with mappings from dna to protein and the reverse
- */
- void processTranslation()
- {
- // TODO Auto-generated method stub
-
- }
-
- /**
- * Constructs and saves the sequence from parsed components
- */
- void assembleSequence()
- {
- String name = this.accession;
- if (this.sourceDb != null)
- {
- name = this.sourceDb + "|" + name;
- }
- SequenceI seq = new Sequence(name, this.sequenceString);
- for (DBRefEntry dbref : this.dbrefs)
- {
- seq.addDBRef(dbref);
- }
-
- for (CdsData data : cds)
- {
- processCDS(seq, data);
- };
-
- processTranslation();
-
- seq.deriveSequence();
-
- addSequence(seq);
- }
-
- /**
- * Output (print) is not implemented for EMBL flat file format
- */
@Override
- public String print(SequenceI[] seqs, boolean jvsuffix)
+ protected boolean isFeatureContinuationLine(String line)
{
- return null;
- }
-
- /**
- * Returns the CDS location as a single array of [start, end, start, end...]
- * positions. If on the reverse strand, these will be in descending order.
- *
- * @param accession
- * @param location
- * @return
- */
- protected int[] getCdsRanges(String accession, String location)
- {
- if (location == null)
- {
- return new int[] {};
- }
-
- try
- {
- List ranges = DnaUtils.parseLocation(location);
- return MappingUtils.listToArray(ranges);
- } catch (ParseException e)
- {
- Cache.log.warn(
- String.format("Not parsing inexact CDS location %s in ENA %s",
- location, accession));
- return new int[] {};
- }
+ return line.startsWith("FT "); // 4 spaces
}
}