3 import java.io.IOException;
4 import java.text.ParseException;
5 import java.util.ArrayList;
6 import java.util.Hashtable;
9 import java.util.Map.Entry;
11 import jalview.bin.Cache;
12 import jalview.datamodel.DBRefEntry;
13 import jalview.datamodel.DBRefSource;
14 import jalview.datamodel.FeatureProperties;
15 import jalview.datamodel.Sequence;
16 import jalview.datamodel.SequenceFeature;
17 import jalview.datamodel.SequenceI;
18 import jalview.util.DBRefUtils;
19 import jalview.util.DnaUtils;
20 import jalview.util.MappingUtils;
23 * A class that provides selective parsing of the EMBL flatfile format.
25 * The initial implementation is limited to extracting fields used by Jalview
26 * after fetching an EMBL or EMBLCDS entry:
29 * accession, version, sequence, xref
30 * and (for CDS feature) location, protein_id, product, codon_start, translation
33 * For a complete parser, it may be best to adopt that provided in
34 * https://github.com/enasequence/sequencetools/tree/master/src/main/java/uk/ac/ebi/embl/flatfile
35 * (but note this has a dependency on the Apache Commons library)
38 * @see ftp://ftp.ebi.ac.uk/pub/databases/ena/sequence/release/doc/usrman.txt
39 * @see ftp://ftp.ebi.ac.uk/pub/databases/embl/doc/FT_current.html
41 public class EmblFlatFile extends AlignFile // FileParse
44 * A data bean class to hold values parsed from one CDS Feature (FT)
48 String translation; // from CDS feature /translation
50 String cdsLocation; // CDS /location raw value
52 int codonStart = 1; // from CDS /codon_start
54 String proteinName; // from CDS /product; TODO: use for protein description
56 String proteinId; // from CDS /protein_id
58 Map<String, String> cdsProps = new Hashtable<>(); // CDS other qualifiers
61 private static final String WHITESPACE = "\\s+";
63 private String sourceDb;
66 * values parsed from the EMBL flatfile record
68 private String accession; // from ID (first token)
70 private String version; // from ID (second token)
72 private int length = 128; // from ID (7th token), with usable default
74 private List<DBRefEntry> dbrefs; // from DR and also CDS /db_xref qualifiers
76 private String sequenceString; // from SQ lines
78 private List<CdsData> cds;
87 public EmblFlatFile(FileParse fp, String sourceId) throws IOException
89 super(false, fp); // don't parse immediately
90 this.sourceDb = sourceId;
91 dbrefs = new ArrayList<>();
92 cds = new ArrayList<>();
96 * Parses the flatfile, and if successful, saves as an annotated sequence
97 * which may be retrieved by calling {@code getSequence()}
101 public void parse() throws IOException
103 String line = nextLine();
106 if (line.startsWith("ID"))
108 line = parseID(line);
110 else if (line.startsWith("DR"))
112 line = parseDR(line);
114 else if (line.startsWith("SQ"))
118 else if (line.startsWith("FT"))
120 line = parseFT(line);
131 * Extracts and saves the primary accession and version (SV value) from an ID
132 * line, or null if not found. Returns the next line after the one processed.
135 * @throws IOException
137 String parseID(String line) throws IOException
139 String[] tokens = line.substring(2).split(";");
142 * first is primary accession
144 String token = tokens[0].trim();
145 if (!token.isEmpty())
147 this.accession = token;
151 * second token is 'SV versionNo'
153 if (tokens.length > 1)
155 token = tokens[1].trim();
156 if (token.startsWith("SV"))
158 String[] bits = token.trim().split(WHITESPACE);
159 this.version = bits[bits.length - 1];
164 * seventh token is 'length BP'
166 if (tokens.length > 6)
168 token = tokens[6].trim();
169 String[] bits = token.trim().split(WHITESPACE);
172 this.length = Integer.valueOf(bits[0]);
173 } catch (NumberFormatException e)
175 Cache.log.error("bad length read in flatfile, line: " + line);
183 * Processes one DR line and saves as a DBRefEntry cross-reference. Returns
184 * the line following the line processed.
187 * @throws IOException
189 String parseDR(String line) throws IOException
191 String[] tokens = line.substring(2).split(";");
192 if (tokens.length > 1)
195 * ensure UniProtKB/Swiss-Prot converted to UNIPROT
197 String db = tokens[0].trim();
198 db = DBRefUtils.getCanonicalName(db);
199 String acc = tokens[1].trim();
200 if (acc.endsWith("."))
202 acc = acc.substring(0, acc.length() - 1);
204 String version = "0";
205 if (tokens.length > 2)
207 String secondaryId = tokens[2].trim();
208 if (!secondaryId.isEmpty())
210 // todo: is this right? secondary id is not a version number
211 // version = secondaryId;
214 this.dbrefs.add(new DBRefEntry(db, version, acc));
221 * Reads and saves the sequence, read from the lines following the SQ line.
222 * Whitespace and position counters are discarded. Returns the next line
223 * following the sequence data (the next line that doesn't start with
226 * @throws IOException
228 String parseSQ() throws IOException
230 StringBuilder sb = new StringBuilder(this.length);
231 String line = nextLine();
232 while (line != null && line.startsWith(" "))
235 String[] blocks = line.split(WHITESPACE);
238 * omit the last block (position counter) on each line
240 for (int i = 0; i < blocks.length - 1; i++)
242 sb.append(blocks[i]);
246 this.sequenceString = sb.toString();
252 * Processes an FT line. If it declares a feature type of interest (currently,
253 * only CDS is processed), processes all of the associated lines (feature
254 * qualifiers), and returns the next line after that, otherwise simply returns
259 * @throws IOException
261 String parseFT(String line) throws IOException
263 String[] tokens = line.split(WHITESPACE);
264 if (tokens.length < 3 || !"CDS".equals(tokens[1]))
269 CdsData data = new CdsData();
270 data.cdsLocation = tokens[2];
275 if (!line.startsWith("FT ")) // 4 spaces
277 // e.g. start of next feature "FT source..."
282 * extract qualifier, e.g. FT /protein_id="CAA37824.1"
284 int slashPos = line.indexOf('/');
287 Cache.log.error("Unexpected EMBL line ignored: " + line);
290 int eqPos = line.indexOf('=', slashPos + 1);
293 Cache.log.error("Unexpected EMBL line ignored: " + line);
296 String qualifier = line.substring(slashPos + 1, eqPos);
297 String value = line.substring(eqPos + 1);
298 if (value.startsWith("\"") && value.endsWith("\""))
300 value = value.substring(1, value.length() - 1);
303 if ("protein_id".equals(qualifier))
305 data.proteinId = value;
308 else if ("codon_start".equals(qualifier))
312 data.codonStart = Integer.parseInt(value.trim());
313 } catch (NumberFormatException e)
315 Cache.log.error("Invalid codon_start in XML for " + this.accession
316 + ": " + e.getMessage());
320 else if ("db_xref".equals(qualifier))
322 String[] parts = value.split(":");
323 if (parts.length == 2)
325 String db = parts[0].trim();
326 db = DBRefUtils.getCanonicalName(db);
327 DBRefEntry dbref = new DBRefEntry(db, "0", parts[1].trim());
328 this.dbrefs.add(dbref);
332 else if ("product".equals(qualifier))
334 // sometimes name is returned e.g. for V00488
335 data.proteinName = value;
338 else if ("translation".equals(qualifier))
340 line = readTranslation(value, data);
342 else if (!"".equals(value))
344 // throw anything else into the additional properties hash
345 data.cdsProps.put(qualifier, value);
356 * Reads and returns the CDS translation from one or more lines of the file,
357 * and returns the next line after that
360 * the first line of the translation (likely quoted)
363 * @throws IOException
365 String readTranslation(String value, CdsData data) throws IOException
367 StringBuilder sb = new StringBuilder(this.length / 3 + 1);
368 sb.append(value.replace("\"", ""));
371 while ((line = nextLine()) != null)
373 if (!line.startsWith("FT "))
375 break; // reached next feature or other input line
377 String[] tokens = line.split(WHITESPACE);
378 if (tokens.length < 2)
380 Cache.log.error("Ignoring bad EMBL line: " + line);
383 if (tokens[1].startsWith("/"))
385 break; // next feature qualifier
387 sb.append(tokens[1].replace("\"", ""));
390 data.translation = sb.toString();
396 * Processes the parsed CDS feature data to
398 * <li>add a CDS feature to the sequence for each CDS start-end range</li>
399 * <li>create a protein product sequence for the translation</li>
400 * <li>create a cross-reference to protein with mapping from dna</li>
401 * <li>add any CDS dbrefs to the sequence and to the protein product</li>
407 void processCDS(SequenceI dna, CdsData data)
410 * parse location into a list of [start, end, start, end] positions
412 int[] exons = getCdsRanges(this.accession, data.cdsLocation);
415 for (int xint = 0; exons != null && xint < exons.length - 1; xint += 2)
417 int exonStart = exons[xint];
418 int exonEnd = exons[xint + 1];
419 int begin = Math.min(exonStart, exonEnd);
420 int end = Math.max(exonStart, exonEnd);
422 String desc = String.format("Exon %d for protein EMBLCDS:%s",
423 exonNumber, data.proteinId);
425 SequenceFeature sf = new SequenceFeature("CDS", desc, begin, end,
427 for (Entry<String, String> val : data.cdsProps.entrySet())
429 sf.setValue(val.getKey(), val.getValue());
432 sf.setEnaLocation(data.cdsLocation);
433 boolean forwardStrand = exonStart <= exonEnd;
434 sf.setStrand(forwardStrand ? "+" : "-");
435 sf.setPhase(String.valueOf(data.codonStart - 1));
436 sf.setValue(FeatureProperties.EXONPOS, exonNumber);
437 sf.setValue(FeatureProperties.EXONPRODUCT, data.proteinName);
439 dna.addSequenceFeature(sf);
444 * Constructs a sequence for the protein product (if there is one), and dbrefs
445 * with mappings from dna to protein and the reverse
447 void processTranslation()
449 // TODO Auto-generated method stub
454 * Constructs and saves the sequence from parsed components
456 void assembleSequence()
458 String name = this.accession;
459 if (this.sourceDb != null)
461 name = this.sourceDb + "|" + name;
463 SequenceI seq = new Sequence(name, this.sequenceString);
464 for (DBRefEntry dbref : this.dbrefs)
469 for (CdsData data : cds)
471 processCDS(seq, data);
474 processTranslation();
476 seq.deriveSequence();
482 * Output (print) is not implemented for EMBL flat file format
485 public String print(SequenceI[] seqs, boolean jvsuffix)
491 * Returns the CDS location as a single array of [start, end, start, end...]
492 * positions. If on the reverse strand, these will be in descending order.
498 protected int[] getCdsRanges(String accession, String location)
500 if (location == null)
507 List<int[]> ranges = DnaUtils.parseLocation(location);
508 return MappingUtils.listToArray(ranges);
509 } catch (ParseException e)
512 String.format("Not parsing inexact CDS location %s in ENA %s",
513 location, accession));