- /**
- * 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
- * <ul>
- * <li>add a CDS feature to the sequence for each CDS start-end range</li>
- * <li>create a protein product sequence for the translation</li>
- * <li>create a cross-reference to protein with mapping from dna</li>
- * <li>add any CDS dbrefs to the sequence and to the protein product</li>
- * </ul>
- *
- * @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<String, String> 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
- */