Skip to content
Snippets Groups Projects
GFFStreamFeature.java 41.1 KiB
Newer Older
  • Learn to ignore specific revisions
  •    *         the attribute has no value then the Hashtable value will be a zero
       *         length vector.
    
    tjc's avatar
    tjc committed
       **/
    
      private Hashtable<String, StringVector> parseAttributes(
          final String att_val_list) {
    
    tcarver's avatar
    tcarver committed
        final Hashtable<String, StringVector> attr = new Hashtable<String, StringVector>();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        int ind_start = 0;
        int ind_end;
    
        while ((ind_end = att_val_list.indexOf(";", ind_start)) > -1
            || ind_start < att_val_list.length()) {
          if (ind_end < 0)
    
    tjc's avatar
    tjc committed
            ind_end = att_val_list.length();
    
    
          final String this_token = GFF3Encoder.decode(att_val_list.substring(ind_start,
              ind_end).trim());
          ind_start = ind_end + 1;
    
    tjc's avatar
    tjc committed
    
    
          int index_of_first_space = this_token.indexOf(" ");
    
    tcarver's avatar
    tcarver committed
          final String att_name;
    
          StringVector att_values = new StringVector();
    
          if (this_token.indexOf("=") > -1
              && (this_token.indexOf("=") < index_of_first_space || index_of_first_space == -1)) {
    
            index_of_first_space = this_token.indexOf("=");
            att_name = this_token.substring(0, index_of_first_space);
    
            att_values.add(this_token.substring(index_of_first_space + 1).trim());
          } else if (index_of_first_space == -1)
    
    tjc's avatar
    tjc committed
            att_name = this_token;
    
          else {
    
            att_name = this_token.substring(0, index_of_first_space);
    
    tjc's avatar
    tjc committed
    
    
            String rest_of_token = this_token.substring(index_of_first_space + 1)
                .trim();
    
    tjc's avatar
    tjc committed
    
    
            while (rest_of_token.length() > 0) {
              if (rest_of_token.startsWith("\"")) {
    
    tjc's avatar
    tjc committed
                int quote_index = 0;
    
    tjc's avatar
    tjc committed
                  quote_index++;
    
                  quote_index = rest_of_token.indexOf("\"", quote_index);
    
                } while (quote_index > -1
                    && rest_of_token.charAt(quote_index - 1) == '\\');
    
    tjc's avatar
    tjc committed
    
    
                if (quote_index < 0) {
    
    tjc's avatar
    tjc committed
                  // no closing quote - panic
    
                  final Hashtable<String, StringVector> panic_attributes = new Hashtable<String, StringVector>();
    
                  final StringVector notes = new StringVector();
                  notes.add(att_val_list);
                  panic_attributes.put("note", notes);
    
    tjc's avatar
    tjc committed
    
                  return panic_attributes;
                }
    
    
                final String next_bit = rest_of_token.substring(1, quote_index);
                att_values.add(next_bit);
                rest_of_token = rest_of_token.substring(quote_index + 1).trim();
    
              } else {
    
                final int index_of_next_space = rest_of_token.indexOf(" ");
    
    
                if (index_of_next_space == -1) {
    
                  att_values.add(rest_of_token);
    
    tjc's avatar
    tjc committed
                  rest_of_token = "";
    
                } else {
                  final String next_bit = rest_of_token.substring(0,
                      index_of_next_space);
    
    tjc's avatar
    tjc committed
    
    
                  att_values.add(next_bit);
    
                  rest_of_token = rest_of_token.substring(index_of_next_space)
                      .trim();
    
    tjc's avatar
    tjc committed
                }
              }
            }
    
    
            if (!rest_of_token.equals(""))
    
              att_values.add(rest_of_token);
    
    tjc's avatar
    tjc committed
          }
    
          if (att_name.equals("Dbxref") || att_name.equals("Alias")) // convert to
                                                                     // multi-line
    
            StringTokenizer stok = new StringTokenizer((String) att_values.get(0),
                ",");
    
            StringVector str_values = new StringVector();
    
            while (stok.hasMoreTokens())
    
              str_values.add(stok.nextToken());
    
          if (att_name.equals("timelastmodified")) {
            try {
              this.timelastmodified = new Timestamp(
                  Long.parseLong((String) att_values.get(0)));
              SimpleDateFormat date_format = new SimpleDateFormat(
                  "dd.MM.yyyy hh:mm:ss z");
              att_values.set(0, date_format.format(timelastmodified));
            } catch (NumberFormatException e) {
              att_values.set(0, (String) att_values.get(0));
    
    tjc's avatar
    tjc committed
            }
    
    tcarver's avatar
    tcarver committed
    
    
          if (attr.get(att_name) != null)
    
    tcarver's avatar
    tcarver committed
            attr.get(att_name).add(att_values);
    
    tcarver's avatar
    tcarver committed
            attr.put(att_name, att_values);
    
    tjc's avatar
    tjc committed
        }
    
    
    tcarver's avatar
    tcarver committed
        return attr;
    
    tjc's avatar
    tjc committed
      }
    
    
      /**
       * Get the feature time last modified timestamp.
    
      public Timestamp getLastModified() {
    
        return timelastmodified;
      }
    
    tjc's avatar
    tjc committed
      /**
       * Get the GFF_source value of a Dbxref qualifier.
    
    tjc's avatar
    tjc committed
       * @param qualifier
    
       * @return the gff_source value or NULL
    
      private String getDbxrefGFFSource(final Qualifier qualifier) {
        StringVector qualifier_strings = StreamQualifier.toStringVector(null,
            qualifier);
    
        for (int i = 0; i < qualifier_strings.size(); i++) {
          String qualifier_string = (String) qualifier_strings.elementAt(i);
    
          if (qualifier_string.indexOf("GFF_source:") > -1) {
            int index = qualifier_string.indexOf(":") + 1;
    
    tjc's avatar
    tjc committed
            int len = qualifier_string.length();
    
            if (qualifier_string.endsWith("\""))
    
    tjc's avatar
    tjc committed
              len--;
            return qualifier_string.substring(index, len);
          }
        }
        return null;
      }
    
      /**
       * Set the feature time last modified timestamp.
    
       * @param timelastmodified
       */
    
      public void setLastModified(final Timestamp timelastmodified) {
    
        this.timelastmodified = timelastmodified;
    
        // now update the qualifier value itself
        QualifierVector qualifiers = getQualifiers();
        Qualifier qualifier = qualifiers.getQualifierByName("timelastmodified");
    
        SimpleDateFormat date_format = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss z");
    
        if (qualifier != null)
          qualifier.removeValue((String) qualifier.getValues().get(0));
        else {
          try {
    
            qualifier = new Qualifier("timelastmodified",
    
                date_format.format(timelastmodified));
    
    tjc's avatar
    tjc committed
            setQualifier(qualifier);
            return;
    
          } catch (EntryInformationException eie) {
          } catch (ReadOnlyException roe) {
    
        qualifier.addValue(date_format.format(timelastmodified));
      }
    
       * Returns true if and only if this Feature can't be changed or can't be
       * removed from it's entry.
    
      public boolean isReadOnly() {
        if (readOnlyFeature)
    
          return true;
        return super.isReadOnly();
      }
    
    tjc's avatar
    tjc committed
    
    
      public void setReadOnlyFeature(boolean readOnlyFeature) {
    
        this.readOnlyFeature = readOnlyFeature;
      }
    
      public ChadoCanonicalGene getChadoGene() {
    
    tjc's avatar
    tjc committed
        return chadoGene;
      }
    
    
      public void setChadoGene(ChadoCanonicalGene chadoGene) {
    
    tjc's avatar
    tjc committed
        this.chadoGene = chadoGene;
      }
    
      public boolean isVisible() {
    
    tjc's avatar
    tjc committed
        return visible;
      }
    
    
      public void setVisible(boolean visible) {
    
    tjc's avatar
    tjc committed
        this.visible = visible;
      }
    
      public String getGffSeqName() {
    
      public void setGffSeqName(String gffSeqName) {
    
        this.gffSeqName = gffSeqName;
      }
    
    
      public String getGffSource() {
    
      public void setGffSource(String gffSource) {
    
        this.gffSource = gffSource;
      }
    
    tjc's avatar
    tjc committed
    
    
      public boolean isLazyLoaded() {
    
    tjc's avatar
    tjc committed
        return lazyLoaded;
      }
    
    
      public void setLazyLoaded(boolean lazyLoaded) {
    
    tjc's avatar
    tjc committed
        this.lazyLoaded = lazyLoaded;
      }
    
    
      public org.gmod.schema.sequence.Feature getChadoLazyFeature() {
    
    tjc's avatar
    tjc committed
        return chadoLazyFeature;
      }
    
      public void setChadoLazyFeature(
    
          org.gmod.schema.sequence.Feature chadoLazyFeature) {
    
    tjc's avatar
    tjc committed
        this.chadoLazyFeature = chadoLazyFeature;
      }
    
      protected static boolean isGTF(Feature feature) {
        if (!(feature instanceof GFFStreamFeature))
    
    tcarver's avatar
    tcarver committed
          return false;
    
        final String names[] = { "ID", "Name", "Alias", "Parent", "Derives_from",
            "Target", "Gap", "Note", "Dbxref", "Ontology_term" };
    
        for (String name : names) {
          if (feature.getQualifiers().getQualifierByName(name) != null)
    
    tcarver's avatar
    tcarver committed
            return false;
        }
    
        if (feature.getQualifiers().getQualifierByName("gene_id") != null
            && feature.getQualifiers().getQualifierByName("transcript_id") != null) {
          if (feature.getEntry() != null)
            logger4j.debug(feature.getEntry().getName() + " is in GTF format");
    
    tcarver's avatar
    tcarver committed
          return true;
        }
        return false;
      }
    
    tjc's avatar
    tjc committed
    }