Search in sources :

Example 6 with Record

use of massbank.Record in project MassBank-web by MassBank.

the class Inspector method main.

public static void main(String[] arguments) throws Exception {
    if (arguments.length == 2) {
        String input = FileUtils.readFileToString(new File(arguments[0]), StandardCharsets.UTF_8);
        Validator.hasNonStandardChars(input);
        Set<String> config = new HashSet<String>();
        config.add("legacy");
        Record record = Validator.validate(input, "", config);
        if (record == null) {
            logger.error("Error in " + arguments[0] + ". Exiting...");
            System.exit(1);
        } else {
            logger.trace("Validation passed for " + arguments[0] + ".");
        }
        String accession = record.ACCESSION();
        String shortname = record.RECORD_TITLE().get(0) + " Mass Spectrum";
        // find InChIKey in CH_LINK
        String inchikey = record.CH_LINK().get("INCHIKEY");
        String keywords = accession + ", " + shortname + ", " + (inchikey != null ? inchikey + ", " : "") + "mass spectrum, MassBank record, mass spectrometry, mass spectral library";
        String description = "This MassBank Record with Accession " + accession + " contains the " + record.AC_MASS_SPECTROMETRY_MS_TYPE() + " mass spectrum" + " of '" + record.RECORD_TITLE().get(0) + "'" + (inchikey != null ? " with the InChIKey '" + inchikey + "'" : "") + ".";
        String recordstring = record.createRecordString();
        String structureddata = record.createStructuredData();
        IAtomContainer mol = record.CH_SMILES_obj();
        String svg = new DepictionGenerator().withAtomColors().depict(mol).toSvgStr(Depiction.UNITS_PX);
        String css = "<style>\n" + getResourceFileAsString("massbank.css") + "\n" + getResourceFileAsString("Common.css") + "\n" + getResourceFileAsString("st.css") + "\n" + "</style>\n";
        String js = "<script type=\"text/javascript\">\n" + getResourceFileAsString("Common.js") + "\n" + getResourceFileAsString("d3.v3.min.js") + "\n" + getResourceFileAsString("st.min.js") + "\n" + getResourceFileAsString("massbank_specktackle.js") + "\n" + "</script>\n";
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + "	<title>" + shortname + "</title>\n" + "	<meta charset=\"UTF-8\">\n" + "	<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">\n" + // "	<link rel=\"stylesheet\" href=\"https://www.w3schools.com/lib/w3-theme-grey.css\">\n" +
        "	<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js\"></script>\n" + "	<!-- 	hier anpassen -->\n" + "	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" + "	<meta name=\"variableMeasured\" content=\"m/z\">\n" + structureddata + "\n" + css + "\n" + "</head>\n");
        sb.append("<body class=\"w3-theme-gradient\">\n" + js + "\n" + "  	<header class=\"w3-container w3-top w3-text-dark-grey w3-grey\">\n" + "		<div class=\"w3-bar\">\n" + "			<div class=\"w3-left\">\n" + "				<h1>\n" + "					<b>MassBank Record: " + accession + "</b>\n" + "				</h1>\n" + "			</div>\n" + "	<div class=\"w3-padding\">\n" + "		<h3><b>" + record.RECORD_TITLE1() + "</b></h3>\n" + "			<div class=\"w3-row w3-padding-small\">\n" + "				<div class=\"w3-twothird w3-text-grey w3-small w3-padding-small\">\n" + "					Mass Spectrum\n" + "					<div id=\"spectrum_canvas\" peaks=\"" + record.createPeakListForSpectrumViewer() + "\" style=\"height:200px; width:600px; max-width:100%; background-color:white\"></div>\n" + "				</div>\n" + "				<div class=\"w3-third w3-text-grey w3-small w3-padding-small\">\n" + "					Chemical Structure<br>\n" + svg + "\n" + "				</div>\n" + "			</div>\n" + "	</div>\n" + "	<div class=\"monospace w3-padding w3-small\" style=\"height:auto;margin:auto\">\n" + "		<hr>\n" + recordstring + "\n" + "	</div>\n" + "</body>\n" + "</html>");
        File file = new File(arguments[1]);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            writer.write(sb.toString());
        }
    } else {
        logger.error("Input and output file required. Exiting...");
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) FileWriter(java.io.FileWriter) Record(massbank.Record) File(java.io.File) HashSet(java.util.HashSet) DepictionGenerator(org.openscience.cdk.depict.DepictionGenerator) BufferedWriter(java.io.BufferedWriter)

Example 7 with Record

use of massbank.Record in project MassBank-web by MassBank.

the class RefreshDatabase method main.

public static void main(String[] args) throws FileNotFoundException, SQLException, ConfigurationException, IOException {
    // load version and print
    final Properties properties = new Properties();
    try {
        properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("project.properties"));
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("RefreshDatabase version: " + properties.getProperty("version"));
    logger.trace("Creating a new database \"" + Config.get().tmpdbName() + "\" and initialize a MassBank database scheme.");
    DatabaseManager.init_db(Config.get().tmpdbName());
    logger.trace("Creating a DatabaseManager for \"" + Config.get().tmpdbName() + "\".");
    final DatabaseManager db = new DatabaseManager(Config.get().tmpdbName());
    logger.trace("Get version of data source.");
    String version = FileUtils.readFileToString(new File(Config.get().DataRootPath() + "/VERSION"), StandardCharsets.UTF_8);
    logger.info("Opening DataRootPath \"" + Config.get().DataRootPath() + "\" and iterate over content.");
    File dataRootPath = new File(Config.get().DataRootPath());
    List<File> recordfiles = new ArrayList<>();
    for (String file : dataRootPath.list(DirectoryFileFilter.INSTANCE)) {
        if (file.equals(".scripts"))
            continue;
        if (file.equals(".figure"))
            continue;
        recordfiles.addAll(FileUtils.listFiles(new File(dataRootPath, file), new String[] { "txt" }, true));
    }
    AtomicInteger index = new AtomicInteger(0);
    int chunkSize = 5000;
    Stream<List<File>> chunkedRecordfiles = recordfiles.stream().collect(Collectors.groupingBy(x -> index.getAndIncrement() / chunkSize)).entrySet().stream().map(Map.Entry::getValue);
    AtomicInteger processed = new AtomicInteger(1);
    int numRecordFiles = recordfiles.size();
    chunkedRecordfiles.forEach(chunk -> {
        chunk.parallelStream().map(filename -> {
            Record record = null;
            logger.info("Validating \"" + filename + "\".");
            String contributor = filename.getParentFile().getName();
            try {
                String recordAsString = FileUtils.readFileToString(filename, StandardCharsets.UTF_8);
                Set<String> config = new HashSet<String>();
                config.add("legacy");
                record = Validator.validate(recordAsString, contributor, config);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (record == null) {
                logger.error("Error reading/validating record \"" + filename.toString() + "\".");
            }
            return record;
        }).filter(Objects::nonNull).forEachOrdered((r) -> {
            db.persistAccessionFile(r);
            System.out.print("Processed: " + processed.getAndIncrement() + "/" + numRecordFiles + "\r");
        });
    });
    logger.trace("Setting Timestamp in database");
    PreparedStatement stmnt = db.getConnection().prepareStatement("INSERT INTO LAST_UPDATE (TIME,VERSION) VALUES (CURRENT_TIMESTAMP,?);");
    stmnt.setString(1, version);
    stmnt.executeUpdate();
    db.getConnection().commit();
    db.closeConnection();
    logger.trace("Moving new database to MassBank database.");
    DatabaseManager.move_temp_db_to_main_massbank();
}
Also used : DatabaseManager(massbank.db.DatabaseManager) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Properties(java.util.Properties) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) Record(massbank.Record) File(java.io.File) HashSet(java.util.HashSet)

Example 8 with Record

use of massbank.Record in project MassBank-web by MassBank.

the class RecordApiServiceImpl method recordIdGet.

@Override
public Response recordIdGet(String id, SecurityContext securityContext) throws NotFoundException {
    Record record = null;
    // load record from database
    DatabaseManager dbMan;
    try {
        dbMan = new DatabaseManager("MassBank");
        record = dbMan.getAccessionData(id);
        dbMan.closeConnection();
    } catch (SQLException | ConfigurationException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Database connection error").build();
    }
    if (record == null) {
        return Response.status(Status.NOT_FOUND).entity("Record \"" + id + "\" not found").build();
    }
    String recordstring = record.toString();
    return Response.ok().entity(recordstring).build();
}
Also used : DatabaseManager(massbank.db.DatabaseManager) SQLException(java.sql.SQLException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) Record(massbank.Record)

Example 9 with Record

use of massbank.Record in project MassBank-web by MassBank.

the class Validator2 method validate.

public static Record validate(String recordstring, String contributor) {
    // test non standard ASCII chars and print warnings
    for (int i = 0; i < recordstring.length(); i++) {
        if (recordstring.charAt(i) > 0x7F) {
            String[] tokens = recordstring.split("\\r?\\n");
            System.out.println("Warning: non standard ASCII charactet found. This might be an error. Please check carefully.");
            int line = 0, col = 0, offset = 0;
            for (String token : tokens) {
                offset = offset + token.length() + 1;
                if (i < offset) {
                    col = i - (offset - (token.length() + 1));
                    System.out.println(tokens[line]);
                    StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", tokens[line].length()));
                    error_at.setCharAt(col, '^');
                    System.out.println(error_at);
                    break;
                }
                line++;
            }
        }
    }
    Record record = new Record(contributor);
    Parser recordparser = new RecordParser(record);
    Result res = recordparser.parse(recordstring);
    if (res.isFailure()) {
        System.err.println();
        System.err.println(res.getMessage());
        int position = res.getPosition();
        String[] tokens = recordstring.split("\\n");
        int line = 0, col = 0, offset = 0;
        for (String token : tokens) {
            offset = offset + token.length() + 1;
            if (position < offset) {
                col = position - (offset - (token.length() + 1));
                System.err.println(tokens[line]);
                StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", col));
                error_at.append('^');
                System.err.println(error_at);
                // record = new Record();
                break;
            }
            line++;
        }
        return null;
    } else
        return record;
}
Also used : RecordParser(massbank.RecordParser) Record(massbank.Record) Parser(org.petitparser.parser.Parser) RecordParser(massbank.RecordParser) Result(org.petitparser.context.Result)

Example 10 with Record

use of massbank.Record in project MassBank-web by MassBank.

the class Validator2 method main.

public static void main(String[] arguments) throws Exception {
    boolean haserror = false;
    if (arguments.length == 0) {
        Record record = validate(recordstringExample, "Boise State University");
        if (record == null)
            System.err.println("Error.");
        else
            System.out.println(record.toString());
    } else {
        for (String filename : arguments) {
            // System.out.print("Validating " + filename + " ... ");
            String contributor = (new File(filename)).getParentFile().getName();
            recordstringExample = FileUtils.readFileToString(new File(filename), StandardCharsets.UTF_8);
            Record record = validate(recordstringExample, contributor);
            if (record == null) {
                System.err.println("Error in " + filename);
                haserror = true;
            }
        // else System.out.println("ok");
        }
    }
    if (haserror)
        System.exit(1);
}
Also used : Record(massbank.Record) File(java.io.File)

Aggregations

Record (massbank.Record)13 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 File (java.io.File)6 HashSet (java.util.HashSet)5 SQLException (java.sql.SQLException)4 Properties (java.util.Properties)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 DatabaseManager (massbank.db.DatabaseManager)3 CommandLine (org.apache.commons.cli.CommandLine)3 DefaultParser (org.apache.commons.cli.DefaultParser)3 HelpFormatter (org.apache.commons.cli.HelpFormatter)3 Options (org.apache.commons.cli.Options)3 ParseException (org.apache.commons.cli.ParseException)3 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2