- }
-
- private void process_as_fasta(AlignmentI align, List<SequenceI> newseqs)
- throws IOException
- {
- try
- {
- mark();
- } catch (IOException q)
- {
- }
- FastaFile parser = new FastaFile(this);
- List<SequenceI> includedseqs = parser.getSeqs();
- SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
- // iterate over includedseqs, and replacing matching ones with newseqs
- // sequences. Generic iterator not used here because we modify includedseqs
- // as we go
- for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
- {
- // search for any dummy seqs that this sequence can be used to update
- SequenceI dummyseq = smatcher.findIdMatch(includedseqs.get(p));
- if (dummyseq != null)
- {
- // dummyseq was created so it could be annotated and referred to in
- // alignments/codon mappings
-
- SequenceI mseq = includedseqs.get(p);
- // mseq is the 'template' imported from the FASTA file which we'll use
- // to coomplete dummyseq
- if (dummyseq instanceof SequenceDummy)
- {
- // probably have the pattern wrong
- // idea is that a flyweight proxy for a sequence ID can be created for
- // 1. stable reference creation
- // 2. addition of annotation
- // 3. future replacement by a real sequence
- // current pattern is to create SequenceDummy objects - a convenience
- // constructor for a Sequence.
- // problem is that when promoted to a real sequence, all references
- // need
- // to be updated somehow.
- ((SequenceDummy) dummyseq).become(mseq);
- includedseqs.set(p, dummyseq); // template is no longer needed
- }
- }
- }
- // finally add sequences to the dataset
- for (SequenceI seq : includedseqs)
- {
- align.addSequence(seq);
- }
- }
-
- /**
- * take a sequence feature and examine its attributes to decide how it should
- * be added to a sequence
- *
- * @param seq
- * - the destination sequence constructed or discovered in the
- * current context
- * @param sf
- * - the base feature with ATTRIBUTES property containing any
- * additional attributes
- * @param gFFFile
- * - true if we are processing a GFF annotation file
- * @return true if sf was actually added to the sequence, false if it was
- * processed in another way
- */
- public boolean processOrAddSeqFeature(AlignmentI align,
- List<SequenceI> newseqs, SequenceI seq, SequenceFeature sf,
- boolean gFFFile, boolean relaxedIdMatching)
- {
- String attr = (String) sf.getValue("ATTRIBUTES");
- boolean add = true;
- if (gFFFile && attr != null)
- {
- int nattr = 8;
-
- for (String attset : attr.split("\t"))
- {
- if (attset == null || attset.trim().length() == 0)
- {
- continue;
- }
- nattr++;
- Map<String, List<String>> set = new HashMap<String, List<String>>();
- // normally, only expect one column - 9 - in this field
- // the attributes (Gff3) or groups (gff2) field
- for (String pair : attset.trim().split(";"))
- {
- pair = pair.trim();
- if (pair.length() == 0)
- {
- continue;
- }
-
- // expect either space seperated (gff2) or '=' separated (gff3)
- // key/value pairs here
-
- int eqpos = pair.indexOf('='), sppos = pair.indexOf(' ');
- String key = null, value = null;
-
- if (sppos > -1 && (eqpos == -1 || sppos < eqpos))
- {
- key = pair.substring(0, sppos);
- value = pair.substring(sppos + 1);
- }
- else
- {
- if (eqpos > -1 && (sppos == -1 || eqpos < sppos))
- {
- key = pair.substring(0, eqpos);
- value = pair.substring(eqpos + 1);
- }
- else
- {
- key = pair;
- }
- }
- if (key != null)
- {
- List<String> vals = set.get(key);
- if (vals == null)
- {
- vals = new ArrayList<String>();
- set.put(key, vals);
- }
- if (value != null)
- {
- vals.add(value.trim());
- }
- }
- }
- try
- {
- add &= processGffKey(set, nattr, seq, sf, align, newseqs,
- relaxedIdMatching); // process decides if
- // feature is actually
- // added
- } catch (InvalidGFF3FieldException ivfe)
- {
- System.err.println(ivfe);
- }
- }
- }
- if (add)
- {
- seq.addSequenceFeature(sf);
- }
- return add;
- }
-
- public class InvalidGFF3FieldException extends Exception
- {
- String field, value;
-
- public InvalidGFF3FieldException(String field,
- Map<String, List<String>> set, String message)
- {
- super(message + " (Field was " + field + " and value was "
- + set.get(field).toString());
- this.field = field;
- this.value = set.get(field).toString();
- }
-