Java String Format formatProtein(String seq)

Here you can find the source of formatProtein(String seq)

Description

Format a protein sequence in 6 columns per line, with 10 aa for each column.

License

Open Source License

Parameter

Parameter Description
seq Sequence to be formated.

Return

Formated proteinSequence.

Declaration

public static String formatProtein(String seq) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from  w  w w.java2 s . co m
     * Format a protein sequence in 6 columns per line, with 10 aa for each column.
     * 
     * 
     * @param seq Sequence to be formated.
     * @return Formated proteinSequence.
     */
    public static String formatProtein(String seq) {

        StringBuilder sb = new StringBuilder();

        int count = 0;
        boolean first = true;
        List<String> lineList = new ArrayList<>();

        for (char c : seq.toCharArray()) {

            if (count % 60 == 0 && !first) {
                lineList.add(sb.toString().trim());
                sb = new StringBuilder();
            }

            first = false;

            if (count % 10 == 0) {
                sb.append(" ");
            }

            sb.append(c);

            count++;

        }

        lineList.add(sb.toString().trim());

        sb = new StringBuilder();

        for (String s : lineList) {
            sb.append(s).append("\n");
        }

        return sb.toString().substring(0, sb.length() - 1);

    }

    /**
     * Format a protein sequence in 6 columns per line, with 10 aa for each column.
     * Considering a previous quantity of processed chars.
     * 
     * @param seq Sequence to be formated.
     * @param jump How many chars to jump.
     * @return Formated proteinSequence.
     */
    public static String formatProtein(String seq, int jump) {

        StringBuilder sb = new StringBuilder();

        int count = jump;
        boolean first = true;
        List<String> lineList = new ArrayList<>();

        for (char c : seq.toCharArray()) {

            if (count % 60 == 0 && !first) {
                lineList.add(sb.toString().trim());
                sb = new StringBuilder();
            }

            first = false;

            if (count % 10 == 0) {
                sb.append(" ");
            }

            sb.append(c);

            count++;

        }

        lineList.add(sb.toString().trim());

        sb = new StringBuilder();

        for (String s : lineList) {
            sb.append(s).append("\n");
        }

        return sb.toString().substring(0, sb.length() - 1).trim();

    }
}

Related

  1. formatLore(String str)
  2. formatMailtoLink(String emailAddress)
  3. formatMessage(String message)
  4. formatNameToMimeType(final String formatName)
  5. formatParam(String param)
  6. formatString(String stringFormat, String... args)
  7. formatStringTo80Columns(String str)
  8. formatTooltip(String tooltip)
  9. formatTreeLines(String tree)