Esempio n. 1
0
  /**
   * Creates the mgf file object from an existing mgf file with a pre-parsed index of ms2 spectra.
   * The index must hold the offsets of all "BEGIN IONS" lines in the order they appear in the file.
   *
   * @param file The mgf file
   * @param index An ArrayList holding the
   * @throws JMzReaderException
   */
  public MgfFile(File file, List<IndexElement> index) throws JMzReaderException {
    // open the file
    try {
      // save the file
      sourceFile = file;
      // save the index
      this.index = index;

      BufferedRandomAccessFile reader = new BufferedRandomAccessFile(sourceFile, "r", 1024 * 1000);

      // process the file line by line
      String line;
      boolean inHeader = true; // indicates whether we're still in the attribute section

      while ((line = reader.getNextLine()) != null) {
        // remove any comments from the line (if the line will be processed)
        line = line.replaceAll(mgfCommentRegex, "").trim();

        // ignore empty lines
        if (line.length() < 1) {
          continue;
        }

        // break the loop as soon as a ms2 query is encountered
        if (line.contains("BEGIN IONS")) break;

        // check if it's an attribute line
        if (inHeader && line.contains("=")) {
          Matcher matcher = attributePattern.matcher(line);

          if (!matcher.find()) throw new JMzReaderException("Malformatted attribute encountered");
          if (matcher.groupCount() != 2)
            throw new JMzReaderException("Malformatted attribute encountered");

          // process the attribute
          processAttribute(matcher.group(1), matcher.group(2));

          continue;
        } else if (!inHeader && line.contains("=")) {
          throw new JMzReaderException(
              "Attribute encountered at illegal position. Attributes must all be at the beginning of the file");
        } else {
          inHeader = false;
        }

        // if we're not in the header and it's not a ms2 it must be a pmf query
        if (!inHeader) {
          PmfQuery query = new PmfQuery(line);
          pmfQueries.add(query);
        }
      }

      reader.close();
    } catch (FileNotFoundException e) {
      throw new JMzReaderException("MgfFile does not exist.", e);
    } catch (IOException e) {
      throw new JMzReaderException("Failed to read from mgf file.", e);
    }
  }
Esempio n. 2
0
  /**
   * Creates the mgf file object from an existing mgf file.
   *
   * @param file The mgf file
   * @param allowCustomTags Indicates if the parser should throw an exception when encountering
   *     non-standard tags
   * @throws JMzReaderException
   */
  public MgfFile(File file, boolean allowCustomTags) throws JMzReaderException {

    setAllowCustomTags(allowCustomTags);

    // open the file
    try {
      // save the file
      sourceFile = file;

      BufferedRandomAccessFile braf =
          new BufferedRandomAccessFile(sourceFile.getAbsolutePath(), "r", 1024 * 100);

      // process the file line by line
      String line;
      boolean inHeader = true; // indicates whether we're still in the attribute section
      boolean inMs2 = false;
      long lastPosition = 0;
      long beginIonsIndex = 0; // the index where the last "BEGIN IONS" was encountered

      while ((line = braf.getNextLine()) != null) {

        // remove any comments from the line (if the line will be processed)
        if (!inMs2) line = line.replaceAll(mgfCommentRegex, "").trim();

        // ignore empty lines
        if (line.length() < 1) {

          // always update file pointer before continue
          lastPosition = braf.getFilePointer();
          continue;
        }

        // check if a ms2 block started
        if (!inMs2 && line.contains("BEGIN IONS")) {
          // save the offset of the spectrum
          beginIonsIndex = lastPosition;
          inMs2 = true;
        }
        if (inMs2 && line.contains("END IONS")) {
          inMs2 = false;

          // index.add(new IndexElement(beginIonsIndex, reader.getFilePointer()));
          int size = (int) (braf.getFilePointer() - beginIonsIndex);
          index.add(new IndexElementImpl(beginIonsIndex, size));

          // always update file pointer before continue
          lastPosition = braf.getFilePointer();
          continue;
        }

        if (inMs2) {
          // always update file pointer before continue
          lastPosition = braf.getFilePointer();
          continue;
        }

        // check if it's an attribute line
        if (inHeader && line.contains("=")) {
          Matcher matcher = attributePattern.matcher(line);

          if (!matcher.find()) throw new JMzReaderException("Malformatted attribute encountered");
          if (matcher.groupCount() != 2)
            throw new JMzReaderException("Malformatted attribute encountered");

          // process the attribute
          processAttribute(matcher.group(1), matcher.group(2));

          // always update file pointer before continue
          lastPosition = braf.getFilePointer();
          continue;

        } else if (!inHeader && line.contains("=")) {
          throw new JMzReaderException(
              "Attribute encountered at illegal position. Attributes must all be at the beginning of the file");
        } else {
          inHeader = false;
        }

        // if we're not in the header and it's not a ms2 it must be a pmf query
        if (!inHeader) {
          PmfQuery query = new PmfQuery(line);
          pmfQueries.add(query);
        }

        // always update file pointer before continue
        lastPosition = braf.getFilePointer();
      }

      braf.close();
    } catch (FileNotFoundException e) {
      throw new JMzReaderException("MgfFile does not exist.", e);
    } catch (IOException e) {
      throw new JMzReaderException("Failed to read from mgf file.", e);
    }
  }