text:
from twisted.internet import defer
from twisted.internet.base import ThreadedResolver
from twisted.internet.interfaces import IHostResolution, IHostnameResolver, IResolutionReceiver, IResolverSimple
from zope.interface.declarations import implementer, provider

from scrapy.utils.datatypes import LocalCache

# TODO: cache misses
dnscache = LocalCache(10000)

@implementer(IResolverSimple)
class CachingThreadedResolver(ThreadedResolver):
    """
    Default caching resolver. IPv4 only, supports setting a timeout value for DNS requests.
    """

    def __init__(self, reactor, cache_size, timeout):
        super().__init__(reactor)
        dnscache.limit = cache_size
        self.timeout = timeout

    @classmethod
    def from_crawler(cls, crawler, reactor):
        if crawler.settings.getbool('DNSCACHE_ENABLED'):
            cache_size = crawler.settings.getint('DNSCACHE_SIZE')
        else:
            cache_size = 0
        return cls(reactor, cache_size, crawler.settings.getfloat('DNS_TIMEOUT'))

    def install_on_reactor(self):
        self.reactor.installResolver(self)

    def getHostByName(self, name, timeout=None):
        if name in dnscache:
            return defer.succeed(dnscache[name])
        # in Twisted<=16.6, getHostByName() is always called with
        # a default timeout of 60s (actually passed as (1, 3, 11, 45) tuple),
        # so the input argument above is simply overridden
        # to enforce Scrapy's DNS_TIMEOUT setting's value
        timeout = (self.timeout,)
        d = super().getHostByName(name, timeout)
        if dnscache.limit:
            d.addCallback(self._cache_result, name)
        return d

    def _cache_result(self, result, name):
        dnscache[name] = result
        return result

@implementer(IHostResolution)
class HostResolution:
    def __init__(self, name):
        self.name = name

    def cancel(self):
        raise NotImplementedError()

@provider(IResolutionReceiver)
class _CachingResolutionReceiver:
    def __init__(self, resolutionReceiver, hostName):
        self.resolutionReceiver = resolutionReceiver
        self.hostName = hostName
        self.addresses = []

    def resolutionBegan(self, resolution):
        self.resolutionReceiver.resolutionBegan(resolution)
        self.resolution = resolution

    def addressResolved(self, address):
        self.resolutionReceiver.addressResolved(address)
        self.addresses.append(address)

    def resolutionComplete(self):
        self.resolutionReceiver.resolutionComplete()
        if self.addresses:
            dnscache[self.hostName] = self.addresses

@implementer(IHostnameResolver)
class CachingHostnameResolver:
    """
    Experimental caching resolver. Resolves IPv4 and IPv6 addresses,
    does not support setting a timeout value for DNS requests.
    """

    def __init__(self, reactor, cache_size):
        self.reactor = reactor
        self.original_resolver = reactor.nameResolver
        dnscache.limit = cache_size

    @classmethod
    def from_crawler(cls, crawler, reactor):
        if crawler.settings.getbool('DNSCACHE_ENABLED'):
            cache_size = crawler.settings.getint('DNSCACHE_SIZE')
        else:
            cache_size = 0
        return cls(reactor, cache_size)

    def install_on_reactor(self):
        self.reactor.installNameResolver(self)

    def resolveHostName(
        self, resolutionReceiver, hostName, portNumber=0, addressTypes=None, transportSemantics="TCP"
    ):
        try:
            addresses = dnscache[hostName]
        except KeyError:
            return self.original_resolver.resolveHostName(
                _CachingResolutionReceiver(resolutionReceiver, hostName),
                hostName,
                portNumber,
                addressTypes,
                transportSemantics,
            )
        else:
            resolutionReceiver.resolutionBegan(HostResolution(hostName))
            for addr in addresses:
                resolutionReceiver.addressResolved(addr)
            resolutionReceiver.resolutionComplete()
            return resolutionReceiver


text:
module.exports = {
  extends: [
    "plugin:vue/essential"
  ]
}

redpajama_set_name: RedPajamaGithub


text:
<!DOCTYPE html>

	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Kontiki</title>

	<link href="{{ asset('/css/app.css') }}" rel="stylesheet">
	<link href="{{ asset('/css/kitchen.css') }}" rel="stylesheet">
	<link href="{{ asset('/css/dark-theme.css') }}" rel="stylesheet">

	<link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>

				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
					Toggle Navigation

				</button>
				Kontiki

Home

					@if (Auth::guest())

Login

Register

					@else

							{{ Auth::user()->name }}

Logout

					@endif

Kitchen

            	    Order Number  @{{ order.order_number }}

            		    <button class="order_item" ng-class="order_item.status" ng-repeat="order_item in order.order_items" ng-click="order_item.next()">
            			    @{{ order_item.quantity }} x @{{ order_item.name }}
            		    </button>

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>

You broke it, well done

Not even Chuck Norris could do it, well done!

@{{app.error_status}}

        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>

<audio id="ping" src="{{ asset('/ping.mp3') }}" ></audio>


text:
package main

import (
	"bytes"
	"github.com/ChimeraCoder/anaconda"
	"github.com/bbalet/stopwords"
	"github.com/dchest/stemmer/dutch"
	"github.com/dchest/stemmer/german"
	"github.com/kljensen/snowball"
	"github.com/microcosm-cc/bluemonday"
	"log"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

// clean up results from 30-Day Search API and DM to user
func RefineAndPost(api *anaconda.TwitterApi, results Result, keywords []string, userId int64, lang string, policy *bluemonday.Policy) (message anaconda.DirectMessage) {
	var buffer bytes.Buffer

	rand.Seed(time.Now().Unix())

	peeps := []string{"tweeps", "accounts to follow", "individuals", "new Internet friends", "accounts", "people", "Twitter accounts", "folks"}
	connectors := []string{"here is a random selection of ", "here are ", "below are ", "I found ", "here are ", "I found you ", "you might be interested in these "}

	numberOfUsers, users := SortByConfidenceScore(results.Tweets, keywords, lang)
	keyword := strings.Join(keywords, " ")

	if numberOfUsers > 1 {
		buffer.WriteString("OK, " + connectors[rand.Intn(len(connectors))] + strconv.Itoa(numberOfUsers) + " " + peeps[rand.Intn(len(peeps))])
		buffer.WriteString(" who have been tweeting about " + keyword + " in the last 30 days, organized by overall Klout score (plus other topics they are influential in that might be related):\n")
		buffer.WriteString(users)
	} else if numberOfUsers == 1 {
		buffer.WriteString("OK, here is an account")
		buffer.WriteString(" who has been tweeting about " + keyword + " in the last 30 days, plus overall Klout score and areas they are influential in:\n")
		buffer.WriteString(users)
	} else {
		buffer.WriteString("Sad day! No influential accounts talking about " + keyword + " were found. Time to start a trend!")
	}

	toMsg := policy.Sanitize(buffer.String())
	message, err := api.PostDMToUserId(toMsg, userId)

	if err != nil {
		log.Fatal(err)
	}

	log.Println(toMsg)

	return message
}

func ContainsUser(users []User, userId int64) bool {
	for _, u := range users {
		if u.UserId == userId {
			return true
		}
	}

	return false
}

func ContainsWord(words []string, keyword string) bool {
	for _, w := range words {
		if strings.Contains(w, keyword) || strings.Contains(keyword, w) {
			return true
		}

	}

	return false
}

func DedupeResults(results []Tweet) (dedupedResults []User) {
	for _, tweet := range results {
		if (!ContainsUser(dedupedResults, tweet.User.UserId)) && (tweet.User.Derived.Klout.Score >= 20) {
			dedupedResults = append(dedupedResults, tweet.User)
		}
	}

	return dedupedResults
}

func DedupeKeywords(keywords []string) (dedupedResults []string) {
	for _, keyword := range keywords {
		cleanKeyword := stopwords.CleanString(strings.ToLower(keyword), "en", true)

		if cleanKeyword != " " && cleanKeyword != "" && !ContainsWord(dedupedResults, cleanKeyword) {
			dedupedResults = append(dedupedResults, cleanKeyword)
		}
	}

	return dedupedResults
}

func IsRelatedWordByStem(topic string, keyword string, lang string) bool {
  // obviously I don't have stemmers for all of these yet, maybe implementing them is the
  // next step here before hosting this thing and making it live?
	countryCodes := map[string]string{
		"zh": "chinese",
		"nl": "dutch",
		"en": "english",
		"fr": "french",
		"de": "german",
		"el": "greek",
		"he": "hebrew",
		"it": "italian",
		"ja": "japanese",
		"ko": "korean",
		"ms": "malay",
		"fa": "persian",
		"pt": "portuguese",
		"pa": "punjabi",
		"ru": "russian",
		"es": "spanish",
		"sv": "swedish",
		"tr": "turkish",
		"ur": "urdu",
		"uk": "ukrainian"}

	lowerTopic := stopwords.CleanString(strings.ToLower(topic), lang, true)
	var topicStem, keywordStem string

	if lang == "nl" {
		topicStem = dutch.Stemmer.Stem(lowerTopic)
		keywordStem = dutch.Stemmer.Stem(keyword)
	} else if lang == "de" {
		topicStem = german.Stemmer.Stem(lowerTopic)
		keywordStem = german.Stemmer.Stem(keyword)
	} else {
		var language string = countryCodes[lang]

		if language == "" {
			language = "english"
		}

		topicStem, _ = snowball.Stem(lowerTopic, language, true)
		keywordStem, _ = snowball.Stem(keyword, language, true)
	}

	if topicStem == keywordStem {
		return true
	}

	// in case topic or keyword isn't in stem library, check of last resort
	if ContainsWord([]string{lowerTopic}, keyword) {
		return true
	}

	return false
}

func IsRelatedWordBySplitStem(topic string, keywords []string, lang string) bool {
	for _, key := range keywords {
		if IsRelatedWordByStem(topic, key, lang) {
			return true
		}
	}

	if IsRelatedWordByStem(topic, strings.TrimSuffix(strings.Join(keywords, " "), " "), lang) {
		return true
	}

	return false
}

func RankByKeywordInKloutTopic(users []User, keywords []string, lang string) []User {
	dedupedKeywords := DedupeKeywords(keywords)

	var has, hasNot []User
	for _, user := range users {
		for _, topic := range user.Derived.Klout.InfluenceTopics {
			if IsRelatedWordBySplitStem(topic.Name, dedupedKeywords, lang) {
				has = append(has, user)
				break
			}
		}

		if !ContainsUser(has, user.UserId) {
			hasNot = append(hasNot, user)
		}
	}

	return append(has, hasNot...)
}

// the Klout enrichment enables us to figure out who has tweeted the most about $keyword
// so we can sort users who have tweeted about $keyword by how often they tweet about it
func SortByConfidenceScore(results []Tweet, keywords []string, lang string) (int, string) {
	overallKloutScore := func(a, b *User) bool {
		return a.Derived.Klout.Score > b.Derived.Klout.Score
	}

	var buffer bytes.Buffer

	users := DedupeResults(results)
	By(overallKloutScore).Sort(users)
	rankedUsers := RankByKeywordInKloutTopic(users, keywords, lang)

	for _, user := range rankedUsers {
		buffer.WriteString("@" + user.ScreenName + " (overall score: " + strconv.FormatInt(user.Derived.Klout.Score, 10))

		length := len(user.Derived.Klout.InfluenceTopics)

		if length > 0 {
			buffer.WriteString("; top rated interests: ")

			for i, topic := range user.Derived.Klout.InfluenceTopics {
				if topic.Score > 0.25 {
					buffer.WriteString(topic.String())

					if i < (length - 1) {
						buffer.WriteString(", ")
					}
				}
			}
		}

		buffer.WriteString("),\n ")
	}

	str := buffer.String()

	return len(users), strings.TrimSuffix(str, ",\n ") + "\n"
}


text:
package hlt;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Collections;
import java.util.Collection;

public class GameMap {
    private final int width, height;
    private final int playerId;
    private final List<Player> players;
    private final List<Player> playersUnmodifiable;
    private final Map<Integer, Planet> planets;
    private final List<Ship> allShips;
    private final List<Ship> allShipsUnmodifiable;

    // used only during parsing to reduce memory allocations
    private final List<Ship> currentShips = new ArrayList<>();

    public GameMap(final int width, final int height, final int playerId) {
        this.width = width;
        this.height = height;
        this.playerId = playerId;
        players = new ArrayList<>(Constants.MAX_PLAYERS);
        playersUnmodifiable = Collections.unmodifiableList(players);
        planets = new TreeMap<>();
        allShips = new ArrayList<>();
        allShipsUnmodifiable = Collections.unmodifiableList(allShips);
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public int getMyPlayerId() {
        return playerId;
    }

    public List<Player> getAllPlayers() {
        return playersUnmodifiable;
    }

    public Player getMyPlayer() {
        return getAllPlayers().get(getMyPlayerId());
    }

    public Ship getShip(final int playerId, final int entityId) throws IndexOutOfBoundsException {
        return players.get(playerId).getShip(entityId);
    }

    public Planet getPlanet(final int entityId) {
        return planets.get(entityId);
    }

    public Map<Integer, Planet> getAllPlanets() {
        return planets;
    }

    public List<Ship> getAllShips() {
        return allShipsUnmodifiable;
    }

    public ArrayList<Entity> objectsBetween(Position start, Position target) {
        final ArrayList<Entity> entitiesFound = new ArrayList<>();

        addEntitiesBetween(entitiesFound, start, target, planets.values());
        addEntitiesBetween(entitiesFound, start, target, allShips);

        return entitiesFound;
    }

    private static void addEntitiesBetween(final List<Entity> entitiesFound,
                                           final Position start, final Position target,
                                           final Collection<? extends Entity> entitiesToCheck) {

        for (final Entity entity : entitiesToCheck) {
            if (entity.equals(start) || entity.equals(target)) {
                continue;
            }
            if (Collision.segmentCircleIntersect(start, target, entity, Constants.FORECAST_FUDGE_FACTOR)) {
                entitiesFound.add(entity);
            }
        }
    }

    public Map<Double, Entity> nearbyEntitiesByDistance(final Entity entity) {
        final Map<Double, Entity> entityByDistance = new TreeMap<>();

        for (final Planet planet : planets.values()) {
            if (planet.equals(entity)) {
                continue;
            }
            entityByDistance.put(entity.getDistanceTo(planet), planet);
        }

        for (final Ship ship : allShips) {
            if (ship.equals(entity)) {
                continue;
            }
            entityByDistance.put(entity.getDistanceTo(ship), ship);
        }

        return entityByDistance;
    }

    public GameMap updateMap(final Metadata mapMetadata) {
        DebugLog.addLog("--- NEW TURN ---");
        final int numberOfPlayers = MetadataParser.parsePlayerNum(mapMetadata);

        players.clear();
        planets.clear();
        allShips.clear();

        // update players info
        for (int i = 0; i < numberOfPlayers; ++i) {
            currentShips.clear();
            final Map<Integer, Ship> currentPlayerShips = new TreeMap<>();
            final int playerId = MetadataParser.parsePlayerId(mapMetadata);

            final Player currentPlayer = new Player(playerId, currentPlayerShips);
            MetadataParser.populateShipList(currentShips, playerId, mapMetadata);
            allShips.addAll(currentShips);

            for (final Ship ship : currentShips) {
                currentPlayerShips.put(ship.getId(), ship);
            }
            players.add(currentPlayer);
        }

        final int numberOfPlanets = Integer.parseInt(mapMetadata.pop());

        for (int i = 0; i < numberOfPlanets; ++i) {
            final List<Integer> dockedShips = new ArrayList<>();
            final Planet planet = MetadataParser.newPlanetFromMetadata(dockedShips, mapMetadata);
            planets.put(planet.getId(), planet);
        }

        if (!mapMetadata.isEmpty()) {
            throw new IllegalStateException("Failed to parse data from Halite game engine. Please contact maintainers.");
        }

        return this;
    }
}


text:
namespace llvm {
namespace sys {

class self_process;

/// \brief Generic base class which exposes information about an operating
/// system process.
///
/// This base class is the core interface behind any OS process. It exposes
/// methods to query for generic information about a particular process.
///
/// Subclasses implement this interface based on the mechanisms available, and
/// can optionally expose more interfaces unique to certain process kinds.
class process {
protected:
  /// \brief Only specific subclasses of process objects can be destroyed.
  virtual ~process();

public:
  /// \brief Operating system specific type to identify a process.
  ///
  /// Note that the windows one is defined to 'void *' as this is the
  /// documented type for HANDLE on windows, and we don't want to pull in the
  /// Windows headers here.
#if defined(LLVM_ON_UNIX)
  typedef pid_t id_type;
#elif defined(LLVM_ON_WIN32)
  typedef void *id_type; // Must match the type of HANDLE.
#else
#error Unsupported operating system.
#endif

  /// \brief Get the operating system specific identifier for this process.
  virtual id_type get_id() = 0;

  /// \brief Get the user time consumed by this process.
  ///
  /// Note that this is often an approximation and may be zero on platforms
  /// where we don't have good support for the functionality.
  virtual TimeValue get_user_time() const = 0;

  /// \brief Get the system time consumed by this process.
  ///
  /// Note that this is often an approximation and may be zero on platforms
  /// where we don't have good support for the functionality.
  virtual TimeValue get_system_time() const = 0;

  /// \brief Get the wall time consumed by this process.
  ///
  /// Note that this is often an approximation and may be zero on platforms
  /// where we don't have good support for the functionality.
  virtual TimeValue get_wall_time() const = 0;

  /// \name Static factory routines for processes.
  /// @{

  /// \brief Get the process object for the current process.
  static self_process *get_self();

  /// @}

};

/// \brief The specific class representing the current process.
///
/// The current process can both specialize the implementation of the routines
/// and can expose certain information not available for other OS processes.
class self_process : public process {
  friend class process;

  /// \brief Private destructor, as users shouldn't create objects of this
  /// type.
  virtual ~self_process();

public:
  virtual id_type get_id();
  virtual TimeValue get_user_time() const;
  virtual TimeValue get_system_time() const;
  virtual TimeValue get_wall_time() const;

  /// \name Process configuration (sysconf on POSIX)
  /// @{

  /// \brief Get the virtual memory page size.
  ///
  /// Query the operating system for this process's page size.
  size_t page_size() const { return PageSize; };

  /// @}

private:
  /// \name Cached process state.
  /// @{

  /// \brief Cached page size, this cannot vary during the life of the process.
  size_t PageSize;

  /// @}

  /// \brief Constructor, used by \c process::get_self() only.
  self_process();
};

/// \brief A collection of legacy interfaces for querying information about the
/// current executing process.
class Process {
public:
  /// \brief Return process memory usage.
  /// This static function will return the total amount of memory allocated
  /// by the process. This only counts the memory allocated via the malloc,
  /// calloc and realloc functions and includes any "free" holes in the
  /// allocated space.
  static size_t GetMallocUsage();

  /// This static function will set \p user_time to the amount of CPU time
  /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
  /// time spent in system (kernel) mode.  If the operating system does not
  /// support collection of these metrics, a zero TimeValue will be for both
  /// values.
  /// \param elapsed Returns the TimeValue::now() giving current time
  /// \param user_time Returns the current amount of user time for the process
  /// \param sys_time Returns the current amount of system time for the process
  static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
                           TimeValue &sys_time);

  /// This static function will return the process' current user id number.
  /// Not all operating systems support this feature. Where it is not
  /// supported, the function should return 65536 as the value.
  static int GetCurrentUserId();

  /// This static function will return the process' current group id number.
  /// Not all operating systems support this feature. Where it is not
  /// supported, the function should return 65536 as the value.
  static int GetCurrentGroupId();

  /// This function makes the necessary calls to the operating system to
  /// prevent core files or any other kind of large memory dumps that can
  /// occur when a program fails.
  /// @brief Prevent core file generation.
  static void PreventCoreFiles();

  /// This function determines if the standard input is connected directly
  /// to a user's input (keyboard probably), rather than coming from a file
  /// or pipe.
  static bool StandardInIsUserInput();

  /// This function determines if the standard output is connected to a
  /// "tty" or "console" window. That is, the output would be displayed to
  /// the user rather than being put on a pipe or stored in a file.
  static bool StandardOutIsDisplayed();

  /// This function determines if the standard error is connected to a
  /// "tty" or "console" window. That is, the output would be displayed to
  /// the user rather than being put on a pipe or stored in a file.
  static bool StandardErrIsDisplayed();

  /// This function determines if the given file descriptor is connected to
  /// a "tty" or "console" window. That is, the output would be displayed to
  /// the user rather than being put on a pipe or stored in a file.
  static bool FileDescriptorIsDisplayed(int fd);

  /// This function determines if the given file descriptor is displayd and
  /// supports colors.
  static bool FileDescriptorHasColors(int fd);

  /// This function determines the number of columns in the window
  /// if standard output is connected to a "tty" or "console"
  /// window. If standard output is not connected to a tty or
  /// console, or if the number of columns cannot be determined,
  /// this routine returns zero.
  static unsigned StandardOutColumns();

  /// This function determines the number of columns in the window
  /// if standard error is connected to a "tty" or "console"
  /// window. If standard error is not connected to a tty or
  /// console, or if the number of columns cannot be determined,
  /// this routine returns zero.
  static unsigned StandardErrColumns();

  /// This function determines whether the terminal connected to standard
  /// output supports colors. If standard output is not connected to a
  /// terminal, this function returns false.
  static bool StandardOutHasColors();

  /// This function determines whether the terminal connected to standard
  /// error supports colors. If standard error is not connected to a
  /// terminal, this function returns false.
  static bool StandardErrHasColors();

  /// Whether changing colors requires the output to be flushed.
  /// This is needed on systems that don't support escape sequences for
  /// changing colors.
  static bool ColorNeedsFlush();

  /// This function returns the colorcode escape sequences.
  /// If ColorNeedsFlush() is true then this function will change the colors
  /// and return an empty escape sequence. In that case it is the
  /// responsibility of the client to flush the output stream prior to
  /// calling this function.
  static const char *OutputColor(char c, bool bold, bool bg);

  /// Same as OutputColor, but only enables the bold attribute.
  static const char *OutputBold(bool bg);

  /// This function returns the escape sequence to reverse forground and
  /// background colors.
  static const char *OutputReverse();

  /// Resets the terminals colors, or returns an escape sequence to do so.
  static const char *ResetColor();

  /// Get the result of a process wide random number generator. The
  /// generator will be automatically seeded in non-deterministic fashion.
  static unsigned GetRandomNumber();
};

}
}

#endif


text:
namespace arangodb::containers {

struct Helpers {
/// @brief calculate capacity for the container for at least one more
/// element.
/// if this would exceed the container's capacity, use a factor-of-2
/// growth strategy to calculate the capacity.
template<typename T>
static std::size_t nextCapacity(T const& container, std::size_t initialCapacity) {
  std::size_t capacity;
  if (container.empty()) {
    // reserve some initial space
    capacity = std::max(std::size_t(1), initialCapacity);
  } else {
    capacity = container.size() + 1;
    // allocate with power of 2 growth
    if (capacity > container.capacity()) {
      capacity *= 2;
    }
  }
  TRI_ASSERT(capacity > container.size());
  return capacity;
}

/// @brief reserve space for at least one more element in the container.
/// if this would exceed the container's capacity, use a factor-of-2
/// growth strategy to grow the container's memory.
template<typename T>
static void reserveSpace(T& container, std::size_t initialCapacity) {
  std::size_t capacity = nextCapacity(container, initialCapacity);

  // reserve space
  if (capacity > container.capacity()) {
    // if this fails, it will simply throw a bad_alloc exception,
    // and no harm has been done to the container
    container.reserve(capacity);
  }
}

}; // Helpers

} // namespace


text:
/*globals document*/
function initialize(container, application){
  var rootEl = document.querySelector(application.rootElement);
  var modalContainerEl = document.createElement('div');
  var modalContainerElId = 'modals';
  modalContainerEl.id = modalContainerElId;
  rootEl.appendChild(modalContainerEl);
}

export default {
  name: 'add-modals-element',
  initialize: initialize
};


text:
<?php

// =============================================================================
//   _____ _       _
//  / ____(_)     | |
// | |  __ _ _ __ | | ____ _  ___
// | | |_ | | '_ \| |/ / _` |/ _ \
// | |__| | | | | |   < (_| | (_) |
//  \_____|_|_| |_|_|\_\__, |\___/
//                      __/ |
//                     |___/   1.0
//
// =============================================================================

// =============================================================================
// == Configuration ============================================================
// =============================================================================

include "bootstrap.php";

// =============================================================================
// == Parse user query =========================================================
// =============================================================================

$query = explode("/", $_GET['q']);

// Extract page
$GINKGO_PAGE = $query[0];
if(!$GINKGO_PAGE)
    $GINKGO_PAGE = 'home';

// Extract user ID
$GINKGO_USER_ID = $query[1];
if(!$GINKGO_USER_ID)
    $GINKGO_USER_ID = generateID(20);

// Security patch: don't allow user IDs that aren't alphanumerical
// e.g. could use "a; echo b" as user ID; will create file "a" and write "b" to it
if(preg_match('/[^A-Za-z0-9_-]/', $GINKGO_USER_ID))
  exit;

// Don't allow users to modify demo analyses
if($GINKGO_USER_ID[0] == '_' && ($GINKGO_PAGE == "home" || $GINKGO_PAGE == "dashboard")) {
    header("Location: ?q=results/" . $GINKGO_USER_ID);
    exit;
}

// =============================================================================
// == Page-specific configuration ==============================================
// =============================================================================

// Step 1 (choose cells), Step 2 (job name, genome, etc), Step 3 (specify email)
if($GINKGO_PAGE == "dashboard")
  $MY_CELLS = getMyFiles($GINKGO_USER_ID);

// Step 4 (results)
if($GINKGO_PAGE == "results")
  $CURR_CELL = $query[2];

// =============================================================================
// == Session management =======================================================
// =============================================================================

$_SESSION["user_id"] = $GINKGO_USER_ID;

// Define user directories
$userDir = DIR_UPLOADS . '/' . $GINKGO_USER_ID;
$userUrl = URL_ROOT . '/uploads/' . $GINKGO_USER_ID;
$permalink = URL_ROOT . '?q=results/' . $GINKGO_USER_ID;

if(file_exists($descFile = $userDir . '/description.txt'))
    setcookie("ginkgo[$GINKGO_USER_ID]", file_get_contents($descFile), time()+36000000);

// =============================================================================
// == Template configuration ===================================================
// =============================================================================

// -- Panel for info -----------------------------------------------------------
if($GINKGO_PAGE == "results" || $GINKGO_PAGE == "analyze-subset")
{
    $configFile = $userDir . "/config";
    if(file_exists($configFile)) {
        $f = file($configFile);
        $config = array();
        foreach($f as $index => $val) {
            $values = explode("=", $val, 2);
            $config[$values[0]] = str_replace("'", "", trim($values[1]));
        }
    }
}
$tmpBinMeth = split('_', $config['binMeth']);
$binInfo    = $tmpBinMeth[0] . ' bins of ' . $tmpBinMeth[1]/1000 . 'kb size (' . $tmpBinMeth[3] . '/' . $tmpBinMeth[2] . 'bp reads)';
$clusteringInfo = $config['clustMeth'] . ' linkage, ' . str_replace('euclidian','euclidean',$config['distMeth']) . ' distance';
$segInfo    = 'normalized read counts';
if($config['segMeth'] == '1')
    $segInfo = 'sample with lowest IOD';
if($config['segMeth'] == '2')
    $segInfo = 'uploaded reference sample';
$genome = $config['chosen_genome'];

$PANEL_INFO = <<<PANEL

 Analysis Parameters

            Genome: $genome

            Binning: $binInfo

            Segmentation: using $segInfo

            Clustering: $clusteringInfo

PANEL;

// -- Panel for permalink ------------------------------------------------------
$PANEL_LATER = <<<PANEL

 View analysis later

Access your results later at the following address:

<textarea class="input-sm permalink">{$permalink}</textarea>

PANEL;

// -- Panel to show user's last analysis, if any -------------------------------
if(file_exists(DIR_UPLOADS . '/' . $GINKGO_USER_ID . '/status.xml'))
{
    $PANEL_PREVIOUS = <<<PANEL

 Previous analysis results

See your previous analysis results.

Note: Running another analysis will overwrite previous results.

PANEL;
}

// -- Panel for downloading tree -----------------------------------------------
$dloadFileSizes = array(
    'SegStats' => humanFileSize("{$userDir}/SegStats"),
    'SegBreaks' => humanFileSize("{$userDir}/SegBreaks"),
    'SegCopy' => humanFileSize("{$userDir}/SegCopy"),
    'SegNorm' => humanFileSize("{$userDir}/SegNorm"),
    'SegFixed' => humanFileSize("{$userDir}/SegFixed"),
    'CNV1' => humanFileSize("{$userDir}/CNV1"),
    'CNV2' => humanFileSize("{$userDir}/CNV2"),
);
$rnd = rand(1e6,2e6);
$PANEL_DOWNLOAD = <<<PANEL

 Tree display

<input type="radio" name="results-tree-view" onclick="javascript:drawTree('clust.xml');" checked>  Normalized read counts (newick | xml | pdf | jpeg)

<input type="radio" name="results-tree-view" onclick="javascript:drawTree('clust2.xml');">  Copy-number (newick | xml | pdf | jpeg)

<input type="radio" name="results-tree-view" onclick="javascript:drawTree('clust3.xml');">  Correlations (newick | xml | pdf | jpeg)

 Download processed data

Statistics: Bin count statistics for each cell ({$dloadFileSizes['SegStats']}).

Breakpoints: Matrix that encodes whether a cell has a breakpoint at a bin position; 1 = breakpoint present, 0 = no breakpoint at that position; rows = bins, columns = cells ({$dloadFileSizes['SegBreaks']}).

Copy Number: Integer coopy-number state for each cell at every bin position; rows = bins, columns = cells ({$dloadFileSizes['SegCopy']}).

Normalized Counts: Normalized bin counts for each cell at every bin position; rows = bins, columns = cells ({$dloadFileSizes['SegNorm']}).

Normalized and Segmented Counts: Normalized and segmented bin counts for each cell at every bin position; rows = bins, columns = cells ({$dloadFileSizes['SegFixed']}).

Copy number events: List of regions with copy number events ({$dloadFileSizes['CNV1']}).

Copy number regions: List of regions of amplifications (+1) and deletions (-1) ({$dloadFileSizes['CNV2']}).

PANEL;

// =============================================================================
// == Upload facs / binning file ===============================================
// =============================================================================

if($GINKGO_PAGE == 'admin-upload')
{
    // Create user directory if doesn't exist
    @mkdir($userDir);
    $result = "";

    // FACS file
    if(!empty($_FILES['params-facs-file']))
        // Upload facs file
        if(is_uploaded_file($_FILES['params-facs-file']['tmp_name']))
        {
            move_uploaded_file($_FILES['params-facs-file']['tmp_name'], $userDir . "/user-facs.txt");
            $result .= "facs";
        }

    // Segmentation file
    if(!empty($_FILES['params-segmentation-file']))
        // Upload binning file
        if(is_uploaded_file($_FILES['params-segmentation-file']['tmp_name']))
        {
            move_uploaded_file($_FILES['params-segmentation-file']['tmp_name'], $userDir . "/user-segmentation.txt");
            $result .= "segmentation";
        }

    die($result);
}

// =============================================================================
// == Launch analysis ==========================================================
// =============================================================================

if(isset($_POST['analyze']))
{
    // Create user directory if doesn't exist
    @mkdir($userDir);

    // Sanitize user input (see bootstrap.php)
    array_walk_recursive($_POST, 'sanitize');
    $user = $GINKGO_USER_ID;
    sanitize($user);

    // Defaults for new analysis
    $init = 1;
    $process = 1;
    $fix = 0;

    // Did the user change the analysis parameters from the last time?
    // Load previous configuration
    $configFile = $userDir . "/config";
    if(file_exists($configFile))
    {
        //
        $f = file($configFile);
        $oldParams = array();
        foreach($f as $index => $val)
        {
            $values = explode("=", $val, 2);
            $oldParams[$values[0]] = str_replace("", "", trim($values[1]));
        }

        // Defaults for old analysis (do nothing)
        $init = 0;
        $process = 0;
        $fix = 0;

        // Do we need to remap? This sets init to 1 if yes, 0 if not
        $newBinParams   = ($oldParams['binMeth']    != $_POST['binMeth']) ||
                            ($oldParams['binList']  != $_POST['binList']) ||
                            ($oldParams['rmbadbins']!= $_POST['rmbadbins']) ||
                            ($oldParams['rmpseudoautosomal']!= $_POST['rmpseudoautosomal']);
        $newFacs        = ($oldParams['facs']       != $_POST['facs']);
        $newSegParams   = ($oldParams['segMeth']    != $_POST['segMeth']) || ($_POST['segMethCustom'] != '');
        $newClustering  = ($oldParams['clustMeth']  != $_POST['clustMeth']);
        $newDistance    = ($oldParams['distMeth']   != $_POST['distMeth']);
        $newColor       = ($oldParams['color']      != $_POST['color']);
        $sexChange      = ($oldParams['sex']        != $_POST['sex']);

        // Different cells to analyze than last time?
        $cells = '';
        foreach($_POST['cells'] as $cell)
            $cells .= str_replace("'", "", $cell) . "\n";
        if($cells != file_get_contents($userDir . '/list'))
            $newBinParams = 1;

        // -- Set new variable values
        // Redo the mapping for all files
        if($newBinParams)
            $init = 1;
        // Redo binning stuff
        if($newBinParams || $newSegParams || $newColor || $newFacs)
            $process = 1;
        // Redraw dendrogams
        // Only need to run fix when not running process.R
        if(!$process && !$init && ($newClustering || $newDistance || $sexChange))
            $fix = 1;
        // When redirect, if status file isn't changed quickly enough, will show up as 100% completed
        // However, only delete status file if we need to redo at least 1 part of the analysis
        if($init || $process || $fix) {
            $statusFile = DIR_UPLOADS . '/' . $GINKGO_USER_ID . '/status.xml';
            unlink($statusFile);
        }
    }

    // Make sure have enough cells for analysis
    if(count($_POST['cells']) < $GINKGO_MIN_NB_CELLS)
        die("Please select at least " . $GINKGO_MIN_NB_CELLS . " cells for your analysis.");

    // Create list-of-cells-to-analyze file
    $cells = '';
    foreach($_POST['cells'] as $cell)
        $cells .= str_replace("'", "", $cell) . "\n";
    file_put_contents($userDir . '/list', $cells);

    // Create config file
    $config = '#!/bin/bash' . "\n";
    $config.= 'user=' . $user . "\n";
    $config.= 'email=' . $_POST['email'] . "\n";
    $config.= 'permalink=\'' . URL_ROOT . '/?q=results/' . str_replace("'", "", $user) . "'\n";
    //
    $config.= 'segMeth=' . $_POST['segMeth'] . "\n";
    $config.= 'binMeth=' . $_POST['binMeth'] . "\n";
    $config.= 'clustMeth=' . $_POST['clustMeth'] . "\n";
    $config.= 'distMeth=' . $_POST['distMeth'] . "\n";
    //
    $config.= 'f=' . $_POST['f'] . "\n";
    $config.= 'facs=' . $_POST['facs'] . "\n";
    $config.= 'chosen_genome=' . $_POST['chosen_genome'] . "\n";
    //
    $config.= 'init=' . $init . "\n";
    $config.= 'process=' . $process . "\n";
    $config.= 'fix=' . $fix . "\n";
    //
    $config.= 'ref=' . $_POST['segMethCustom'] . "\n";
    //
    $config.= 'color=' . $_POST['color'] . "\n";
    $config.= 'sex=' . $_POST['sex'] . "\n";
    $config.= 'rmbadbins=' . $_POST['rmbadbins'] . "\n";
    $config.= 'rmpseudoautosomal=' . $_POST['rmpseudoautosomal'] . "\n";
    //
    file_put_contents($userDir . '/config', $config);

    // Start analysis
    $cmd = "./scripts/analyze.sh $GINKGO_USER_ID >> $userDir/ginkgo.out 2>&1  &";
    session_regenerate_id(TRUE);
    $handle = popen($cmd, 'r');
    pclose($handle);

    // Save to cookie and file
    setcookie("ginkgo[$GINKGO_USER_ID]", $_POST['job_name'], time()+36000000);
    file_put_contents($userDir . '/description.txt', $_POST['job_name']);

    // Return OK status
    echo "OK";
    exit;
}

// =============================================================================
// == Launch analysis **on subset of cells** ===================================
// =============================================================================

if(isset($_POST['analyze-subset']))
{
    // Job settings
    $selectedCells = $_POST['selectedCells'];
    $analysisType  = $_POST['analysisType'];
    //
    $analysisID    = generateID(10);

    // Save job settings
    $configTxt = $analysisType . "\n";
    foreach($selectedCells as $cell)
        $configTxt .= $cell . "\n";
    file_put_contents($userDir . '/' . $analysisID . '.config', $configTxt);

    // Start analysis
    $PAR = 'original';
    if($config['rmpseudoautosomal'] == '1')
        $PAR = 'pseudoautosomal';

    $cmd = "./scripts/analyze-subset.R $GINKGO_USER_ID $analysisID $genome {$config['binMeth']} $PAR >> $userDir/ginkgo-" . $analysisID . ".out 2>&1  &";
    session_regenerate_id(TRUE);
    $handle = popen($cmd, 'r');
    pclose($handle);

    echo $analysisID;
    exit;
}

if($GINKGO_PAGE == "results-subset")
{
    if(file_exists('uploads/' . $GINKGO_USER_ID . '/' . $query[2] . '.done'))
        echo '';
    else
        echo 'Generating figure...<meta http-equiv="refresh" content="1">';
    exit;
}

// =============================================================================
// == Prepare file for UCSC custom track =======================================
// =============================================================================

if(isset($_POST['ucsc']))
{
    // Job settings
    $cell  = $_POST['cell'];
    $range = $_POST['range'];
    //
    $analysisID    = generateID(10);

    // Save job settings
    $configTxt = "browser position {$range}\n";

    $CMD  = <<<CM
awk -v CELL='{$cell}' 'BEGIN{ print "track name=Amplifications description="CELL" color=0,0,255,"; }{  if(NR==1){ for(i=1;i<=NF;i++){if(\$i==CELL)cellID=i;} }else{ if(\$cellID>2)print \$1"\t"\$2"\t"\$3;  }  }' ./uploads/{$GINKGO_USER_ID}/SegCopy;
awk -v CELL='{$cell}' 'BEGIN{ print "track name=Deletions description="CELL" color=255,0,0,";      }{  if(NR==1){ for(i=1;i<=NF;i++){if(\$i==CELL)cellID=i;} }else{ if(\$cellID<2)print \$1"\t"\$2"\t"\$3;  }  }' ./uploads/{$GINKGO_USER_ID}/SegCopy;
CM;

    file_put_contents($userDir . '/' . $analysisID . '.ucsc', "browser position {$range}\n".shell_exec($CMD));
    echo $analysisID;
    exit;
}

// =============================================================================
// == If analysis under way, redirect to status page ===========================
// =============================================================================

// Load status.xml if exists and check if analysis under way
if($GINKGO_PAGE == "" || $GINKGO_PAGE == "home" || $GINKGO_PAGE == "dashboard") {
    $statusFile = DIR_UPLOADS . '/' . $GINKGO_USER_ID . '/status.xml';
    if(file_exists($statusFile)) {
        $status = simplexml_load_file($statusFile);

        if($status->step < 3 && $status->percentdone < 100) {
            header("Location: ?q=results/" . $GINKGO_USER_ID);
            exit;
        }
    }
}

// Load current settings
$configFile = $userDir . "/config";
if(file_exists($configFile)) {
    $f = file($configFile);
    $config = array();
    foreach($f as $index => $val) {
        $values = explode("=", $val, 2);
        $config[$values[0]] = str_replace("'", "", trim($values[1]));
    }
}

//
if($GINKGO_PAGE == 'admin-search')
{
    $file = DIR_ROOT . '/genomes/' . $config['chosen_genome'] . '/' . ($config['rmpseudoautosomal'] == '1' ? 'pseudoautosomal' : 'original') . '/genes_' . $config['binMeth'];
    $gene = escapeshellarg($_GET['gene']);
    $bin  = escapeshellarg($_GET['binNumber']);

    //
    if(isset($_GET['gene']))
        die(`grep -i -w $gene $file | head -n 1 | cut -f3`);
    //
    if(isset($_GET['binNumber']))
        die(`awk '{if($3==$bin) print $2}' $file | sort | uniq`);
}

// =============================================================================
// == HTML template ============================================================
// =============================================================================

?><!DOCTYPE html>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Ginkgo</title>

        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css">

        <link rel="icon" href="includes/tinycon/ginkgo.ico" />

                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

                    </button>

                             Ginkgo

Home

Code on Github

Our Paper

DOP-PCR Polygenomic breast tumor — Navin et al, 2011

DOP-PCR Breast cancer + liver metastasis — Navin et al, 2011

DOP-PCR Neurons — McConnell et al, 2013

MALBAC  Circulating lung tumor cells — Ni et al, 2013

MALBAC  Oocytes — Hou et al, 2013

MALBAC  Sperm — Lu et al, 2012

MDA         Sperm — Kirkness et al, 2013

MDA         Sperm — Wang et al, 2012

MDA         Neurons — Evrony et al, 2012

                                <?php if(count($_COOKIE['ginkgo']) > 0): ?>

                                    <?php foreach($_COOKIE['ginkgo'] as $id => $name): ?>
                                        <?php if($id != "sample"): ?>

<a href="?q=dashboard/<?php echo $id;?>"><?php echo str_replace("'", "", $name);?>

                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                <?php endif; ?>

<a style="text-decoration:none; color:#000" href="?q=/<?php echo $GINKGO_USER_ID; ?>">Ginkgo <?php echo str_replace("'", "", @file_get_contents($userDir.'/description.txt')); ?>

                    <?php if($GINKGO_PAGE == 'home'): ?>
                    A web tool for analyzing single-cell sequencing data.

                      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">Sample analyses </button>

DOP-PCR Polygenomic breast tumor — Navin et al, 2011

DOP-PCR Breast cancer + liver metastasis — Navin et al, 2011

DOP-PCR Neurons — McConnell et al, 2013

MALBAC  Circulating lung tumor cells — Ni et al, 2013

MALBAC  Oocytes — Hou et al, 2013

MALBAC  Sperm — Lu et al, 2012

MDA         Bone marrow — Hou et al, 2012

MDA         Kidney — Xu et al, 2012

MDA         Neurons — Evrony et al, 2012

                    <?php if(count($_COOKIE['ginkgo']) > 0): ?>

                      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">Load previous analysis </button>

                            <?php foreach($_COOKIE['ginkgo'] as $id => $name): ?>
                                <?php if($id != "sample"): ?>

<a href="?q=dashboard/<?php echo $id;?>"><?php echo str_replace("'", "", $name);?>

                                <?php endif; ?>
                            <?php endforeach; ?>

                    <?php endif; ?>

                    <?php elseif($GINKGO_PAGE == 'dashboard'): ?>

Your files are uploaded. Now let's do some analysis:

                    <?php elseif($GINKGO_PAGE == 'results'): ?>

                        Updating status...

                    <?php endif; ?>

            <?php // ================================================================ ?>
            <?php // == Home: Upload files ========================================== ?>
            <?php // ================================================================ ?>
            <?php if($GINKGO_PAGE == 'home'): ?>

            <?php if($GINKGO_USER_ID != 'sample' && $GINKGO_USER_ID != 'sample2'): ?>

STEP 0 Upload your .bed files (We accept *.bed and *.bed.gz, max 1GB/file, min 3 cells)

                    <iframe id="upload-iframe" style="width:100%; height:100%; border:0;" src="includes/fileupload/?user_id=<?php echo $GINKGO_USER_ID; ?>">

                            <a id="fileupload-next-step" class="btn btn-lg btn-primary" href="?q=dashboard/<?php echo $GINKGO_USER_ID; ?>">Next step

                    <?php echo $PANEL_PREVIOUS; ?>
                    <?php echo $PANEL_LATER; ?>

 Help

Sample .bed file

chrom	chromStart	chromEnd

chr1	555485	555533

chr1	676584	676632

chr1	745136	745184

How to make .bed files

If your mapped reads are saved in the file reads.bam:

bamToBed -i reads.bam > reads.bed

Detailed instructions

                                                Step 1

                                                If you do not have a reference genome index (e.g. hg19), download it from the Bowtie2 website (menu on right, under Indexes).
                                                Then, map your reads (in reads.fastq) to the genome, and output the results to reads.sam:

bowtie2 -x hg19 -U reads.fastq -S reads.sam

If you have paired-end reads, use the following command instead:

bowtie2 -x hg19 -1 reads_r1.fastq -2 reads_r2.fastq -S reads.sam

                                                Step 2

                                                Convert reads.sam to reads.bam:

samtools view -Sb reads.sam -q 20 -o reads.bam

                                            Step 3

                                            Convert reads.bam to reads.bed:

bamToBed -i reads.bam > reads.bed

            <?php else: ?>

            <?php endif; ?>

            <?php // ================================================================ ?>
            <?php // == Dashboard: Analysis settings ================================ ?>
            <?php // ================================================================ ?>
            <?php elseif($GINKGO_PAGE == 'dashboard'): ?>

                <form id="form-dashboard">

STEP 1 Choose cells for analysis

                    <?php if($GINKGO_USER_ID != 'sample' && $GINKGO_USER_ID != 'sample2'): ?>
                    <button id="dashboard-toggle-cells" class="btn btn-info" style="margin:20px;">Select all cells</button>

                        <?php $previouslySelected = file(DIR_UPLOADS . '/' . $GINKGO_USER_ID . '/list', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); ?>
                        <?php $selected = array(); ?>
                        <?php foreach($MY_CELLS as $currCell): ?>
                        <?php
                                // Was the current cell previously selected in an analysis?
                                $selected[$currCell] = "";
                                if(in_array($currCell, $previouslySelected))
                                    $selected[$currCell] = " checked";
                        ?>

<input type="checkbox" name="dashboard_cells[]" value="<?php echo $currCell; ?>"<?php echo $selected[$currCell];?>><?php echo $currCell; ?>

                        <?php endforeach; ?>

STEP 2 Set analysis options

                                Job name:

                                    <input id="param-job-name" class="form-control" type="text" placeholder="Single-cells from tissue X" value="<?php echo str_replace("'", "", file_get_contents($userDir . '/description.txt')); ?>">

                                Genome:

                                    <select id="param-genome" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                        <optgroup label="Latest genomes">

                                            <?php $selected = array(); $selected[$config['chosen_genome']] = ' selected'; ?>
                                            <option value="hg19"<?php echo $selected['hg19']; ?>>Human (hg19)</option>
                                            <option value="panTro4"<?php echo $selected['panTro4']; ?>>Chimpanzee (panTro4)</option>
                                            <option value="mm10"<?php echo $selected['mm10']; ?>>Mus musculus (mm10)</option>
                                            <option value="rheMac8"<?php echo $selected['rheMac8']; ?>>Rhesus macaque (rheMac8)</option>
                                            <option value="rn5"<?php echo $selected['rn5']; ?>>R. norvegicus (rn5)</option>
                                            <option value="dm3"<?php echo $selected['dm3']; ?>>D. Melanogaster (dm3)</option>
                                        </optgroup>
                                        <optgroup label="Older genomes">
                                            <option value="hg18"<?php echo $selected['hg18']; ?>>Human (hg18)</option>
                                            <option value="panTro3"<?php echo $selected['panTro3']; ?>>Chimpanzee (panTro3)</option>
                                            <option value="rheMac7"<?php echo $selected['rheMac7']; ?>>Rhesus macaque (rheMac7)</option>
                                        </optgroup>
                                    </select>

STEP 3 E-mail notification

If you want to be notified once the analysis is done, enter your e-mail here:

                            <?php if($config['email'] != '') $email = $config['email']; ?>

                            <input id="email" class="form-control" type="text" placeholder="my@email.com" value="<?php echo $email; ?>">

OPTIONAL Advanced parameters

Sample Parameters

                                CNV Profile Color Scheme
                                <?php $selected = array(); $selected[$config['color']] = ' selected'; ?>

                                    Use <select id="param-color-scheme" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="3"<?php echo $selected['3']; ?>>dark blue / red</option>
                                    <option value="1"<?php echo $selected['1']; ?>>light blue / orange</option>
                                    <option value="2"<?php echo $selected['2']; ?>>magenta / gold</option>
                                    </select> color scheme.

                                General Binning Options
                                <?php
                                    if(empty($config))
                                        $config['binMeth'] = 'variable_500000_101_bowtie';
                                    $binMeth = split('_', $config['binMeth']);
                                ?>

                                    <?php $selected = array(); $selected[$binMeth[0]] = ' selected'; ?>
                                    Use a <select id="param-bins-type" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="variable_"<?php echo $selected['variable']; ?>>variable</option>
                                    <option value="fixed_"<?php echo $selected['fixed']; ?>>fixed</option>
                                    </select> bin size of

                                    <?php $selected = array(); $selected[$binMeth[1]] = ' selected'; ?>
                                    <select id="param-bins-value" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">

                                    <option class="param-bins-value-hg19" value="10000000_"<?php echo $selected['10000000']; ?>>10Mb</option>
                                    <option class="param-bins-value-hg19" value="5000000_"<?php echo $selected['5000000']; ?>>5Mb</option>
                                    <option class="param-bins-value-hg19" value="2500000_"<?php echo $selected['2500000']; ?>>2.5Mb</option>
                                    <option class="param-bins-value-hg19" value="1000000_"<?php echo $selected['1000000']; ?>>1Mb</option>

                                    <option value="500000_"<?php echo $selected['500000']; ?>>500kb</option>
                                    <option value="250000_"<?php echo $selected['250000']; ?>>250kb</option>
                                    <option value="175000_"<?php echo $selected['175000']; ?>>175kb</option>
                                    <option value="100000_"<?php echo $selected['100000']; ?>>100kb</option>
                                    <option value="50000_"<?php echo $selected['50000']; ?>>50kb</option>
                                    <option value="25000_"<?php echo $selected['25000']; ?>>25kb</option>
                                    <option value="10000_"<?php echo $selected['10000']; ?>>10kb</option>
                                    </select> size.

                                Binning Simulation Options

                                    <?php $selected = array(); $selected[$binMeth[2]] = ' selected'; ?>
                                    Bins based on simulations of <select id="param-bins-sim-rlen" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="150_"<?php echo $selected['150']; ?>>150</option>
                                    <option value="101_"<?php echo $selected['101']; ?>>101</option>
                                    <option value="76_"<?php echo $selected['76']; ?>>76</option>
                                    <option value="48_"<?php echo $selected['48']; ?>>48</option>
                                    </select> bp reads, mapped with

                                    <?php $selected = array(); $selected[$binMeth[3]] = ' selected'; ?>
                                    <select id="param-bins-sim-mapper" class="input-mini" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="bowtie"<?php echo $selected['bowtie']; ?>>bowtie</option>
                                    <option value="bwa"<?php echo $selected['bwa']; ?>>bwa</option>
                                    </select>.

                                Segmentation
                                <?php $selected = array(); $selected[$config['segMeth']] = ' selected'; ?>
                                Use <select id="param-segmentation" class="input-medium" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                <option value="0"<?php echo $selected[0]; ?>>Independent (normalized read counts)</option>
                                <option value="1"<?php echo $selected[1]; ?>>Global (sample with lowest IOD)</option>
                                <option value="2"<?php echo $selected[2]; ?>>Custom (using uploaded reference sample)</option>
                                </select> method to segment.

                                Custom segmentation
Upload the .bed file of a cell to normalize your data by

                                                Select .bed file
                                                Change
                                                <input type="file" name="params-segmentation-file" />

                                            Remove

                                Mask bad bins (experimental)
Removes bins with consistent read pileups from the analysis (e.g. at chromosome boundaries)

                                    <?php /* Uncomment this later */ $checked = " "; if($config['rmbadbins'] == '0') $checked=""; ?>
                                    <input type="checkbox" id="dashboard-rmbadbins"<?php echo $checked;?>>

                                Mask Y-chr pseudoautosomal regions (experimental)
Description

                                    <?php /* Uncomment this later */ $checked = " "; if($config['rmpseudoautosomal'] == '0') $checked=""; ?>
                                    <input type="checkbox" id="dashboard-rmpseudoautosomal"<?php echo $checked;?>>

Clustering Parameters

                                Clustering

                                    <?php $selected = array(); $selected[$config['clustMeth']] = ' selected'; ?>
                                    Use <select id="param-clustering" class="input-small" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="ward"<?php echo $selected['ward']; ?>>ward linkage</option>
                                    <option value="single"<?php echo $selected['single']; ?>>single linkage</option>
                                    <option value="complete"<?php echo $selected['complete']; ?>>complete linkage</option>
                                    <option value="average"<?php echo $selected['average']; ?>>average linkage</option>
                                    <option value="NJ"<?php echo $selected['NJ']; ?>>neighbor-joining</option>
                                    </select>.

                                Distance metric

                                    <?php $selected = array(); $selected[$config['distMeth']] = ' selected'; ?>
                                    Use <select id="param-distance" class="input-small" style="margin-top:8px; font-size:11px; padding-top:3px; padding-bottom:0; height:25px; ">
                                    <option value="euclidian"<?php echo $selected['euclidian']; ?>>Euclidean</option>
                                    <option value="maximum"<?php echo $selected['maximum']; ?>>maximum</option>
                                    <option value="manhattan"<?php echo $selected['manhattan']; ?>>Manhattan</option>
                                    <option value="canberra"<?php echo $selected['canberra']; ?>>Canberra</option>
                                    <option value="binary"<?php echo $selected['binary']; ?>>binary</option>
                                    <option value="minkowski"<?php echo $selected['minkowski']; ?>>Minkowski</option>
                                    </select> distance.

                                Include sex chromosomes?
Uncheck this box for mixed-gender samples

                                    <?php $checked = " checked"; if($config['sex'] == '0') $checked=""; ?>
                                    <input type="checkbox" id="dashboard-include-sex"<?php echo $checked;?>>

FACS File

                                FACS file:
Upload a file with 2 columns: first column is cell name, second column is estimated ploidy by FACS (by DAPI stain)

                                                Select .txt file
                                                Change
                                                <input type="file" name="params-facs-file" />

                                            Remove

                <?php else: ?>

                <?php endif; ?>

                    <?php
                        $btnCaption = 'Start Analysis';
                        if(file_exists($configFile))
                            $btnCaption = 'View Results';
                    ?>

<a class="btn btn-lg btn-primary" href="?q=/<?php echo $GINKGO_USER_ID; ?>"> Manage Files

<?php echo $btnCaption; ?>

                </form>

                    <?php echo $PANEL_PREVIOUS; ?>
                    <?php echo $PANEL_LATER; ?>

            <?php // ================================================================ ?>
            <?php // == Dashboard: Results/Main ===================================== ?>
            <?php // ================================================================ ?>
            <?php elseif($GINKGO_PAGE == 'results' && $CURR_CELL == ""): ?>

STEP 3 View results

 Tree

                                Loading tree...

 Summary

                                    With selected cells, plot:
                                    CNV profiles
                                    Lorenz curve
                                    GC bias
                                    MAD

 Heatmaps

Heatmaps are generated using the unique breakpoints found across all samples, and their values correspond to their relative read counts or copy number as determined by their dissimilarity structure for each respective dendrogram.

                                    Heatmap of copy number values across all segment breakpoints (using <?php echo ucfirst($config['distMeth']); ?> distance metric)

                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatCN.jpeg"; ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatCN.jpeg?uniq=" . rand(1e6,2e6); ?>">

                                    Heatmap of copy number values across all segment breakpoints (using correlation)

                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatCor.jpeg"; ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatCor.jpeg?uniq=" . rand(1e6,2e6); ?>">

                                    Heatmap of normalized read counts across segment breakpoints (using <?php echo ucfirst($config['distMeth']); ?> distance metric)

                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatNorm.jpeg"; ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/heatNorm.jpeg?uniq=" . rand(1e6,2e6); ?>">

                        <?php if($GINKGO_USER_ID != '_t10breast_navin' && $GINKGO_USER_ID != '_t16breast_liver_met_navin' && $GINKGO_USER_ID != '_neuron_mcconnell' && $GINKGO_USER_ID != '_ctc_ni' && $GINKGO_USER_ID != '_oocyte_hou' && $GINKGO_USER_ID != '_sperm_lu' && $GINKGO_USER_ID != '_sperm_kirkness' && $GINKGO_USER_ID != '_sperm_wang' && $GINKGO_USER_ID != '_neuron_evrony'): ?>

<a class="btn btn-lg btn-primary" href="?q=dashboard/<?php echo $GINKGO_USER_ID; ?>"> Analysis Options

                        <?php endif; ?>

                    <?php echo $PANEL_LATER; ?>
                    <?php echo $PANEL_INFO; ?>
                    <?php echo $PANEL_DOWNLOAD; ?>

            <?php // ================================================================ ?>
            <?php // == Dashboard: Results/Cell ==================================== ?>
            <?php // ================================================================ ?>
            <?php elseif($GINKGO_PAGE == 'results' && $CURR_CELL != ""): ?>

Viewing cell <?php echo $CURR_CELL; ?>

<a class="btn btn-lg btn-primary" href="?q=results/<?php echo $GINKGO_USER_ID; ?>"> Back to tree

                             Interactive Profile Viewer

                                View region in UCSC browser

                                    Click + Drag: zoom in
                                    Shift + Click: move profile
                                    Double-click: zoom out

                                    <form class="form-inline" role="form">

                                            Find gene:
                                            <input id="searchForGeneName" type="text" class="form-control" style="width:120px !important;">

                                        Search
                                    </form>

                                    <form class="form-inline" role="form">

                                            List genes in bin #
                                            <input id="searchForBin" type="text" class="form-control" style="width:90px !important;">

                                        List
                                    </form>

 Static Profile Viewer

<a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_CN.jpeg?uniq=" . rand(1e6,2e6);?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_CN.jpeg?uniq=" . rand(1e6,2e6);?>">

 Quality Control

                                    Genome-wide read distribution
                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_dist.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_dist.jpeg?uniq=" . rand(1e6,2e6);?>">

The read count distribution spanning the full genome once reads have been tallied within bin boundaries.

                                    Histogram of read count frequency
                                    Lorenz Curve (for
                                    <?php
                                    preg_match('/_([0-9]*)_/', $config['binMeth'], $matches);
                                    echo $matches[1] / 1000;
                                    ?>kb bins)
                                    Frequency of Bin Counts

                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_counts.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_counts.jpeg?uniq=" . rand(1e6,2e6);?>">
                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_lorenz.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_lorenz.jpeg?uniq=" . rand(1e6,2e6);?>">
                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_hist.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_hist.jpeg?uniq=" . rand(1e6,2e6);?>">

Single cell data should fit a negative binomial distribution with narrower histograms representing higher quality data. Wide histograms without a distinct peak are representative of a high degree of amplification bias. Histograms with a mode near zero have a high degree of "read dropout" and are generally the result of poor library preparation or technical sequencing error.

The Lorenz curve gives the cumulative fraction of reads as a function of the cumulative fraction of the genome.  Perfect coverage uniformity results in a straight line with slope 1.  The wider the curve below the line of "perfect uniformity" the lower the coverage uniformity of a sample.

A histogram of the scaled read count distribution.  Samples with a low coverage dispersion will have reads counts the closely bound the interger copy number state.  These can be seen as clear peaks at integer values of the histrogram.  Samples without clear peaks are indicative of lower quality samples and we recommend choosing a larger binning interval to control the signal to noise ratio.

 Analysis details

                                    GC Correction
                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_GC.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_GC.jpeg?uniq=" . rand(1e6,2e6);?>">

                                    Sum Of Squares Error
                                    <a href="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_SoS.jpeg?uniq=" . rand(1e6,2e6); ?>"><img style="width:100%;" src="<?php echo URL_UPLOADS . "/" . $GINKGO_USER_ID . "/" . $CURR_CELL . "_SoS.jpeg?uniq=" . rand(1e6,2e6);?>">

<a class="btn btn-lg btn-primary" href="?q=results/<?php echo $GINKGO_USER_ID; ?>"> Back to tree

            <?php endif; ?>

                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

Start a new analysis

Are you sure? This will require uploading new files.

                    Note: You can always come back to this analysis later:

                    <textarea class="input-sm permalink"><?php echo $permalink; ?></textarea>

                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="btn-new-analysis">Create new analysis</button>

        <link rel="stylesheet" type="text/css" href="includes/jasny/bootstrap-fileupload.min.css">

        <link rel="stylesheet" type="text/css" href="includes/unitip/unitip.css">

        <link rel="stylesheet" type="text/css" href="includes/datatables/1.10.4/dataTables.tableTools.css">
        <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css">


text:
package event

import (
	"os"

	"github.com/sirupsen/logrus"
)

// HookConsole implements event log hook, which prints entries to the console
type HookConsole struct {
	logger *logrus.Logger
}

// NewHookConsole creates a new HookConsole
func NewHookConsole(level logrus.Level) *HookConsole {
	return &HookConsole{
		logger: &logrus.Logger{
			Out:       os.Stderr,
			Formatter: new(logrus.TextFormatter),
			Hooks:     make(logrus.LevelHooks),
			Level:     level,
		},
	}
}

// Levels defines on which log levels this hook should be fired
func (hook *HookConsole) Levels() []logrus.Level {
	return logrus.AllLevels
}

// Fire processes a single log entry
func (hook *HookConsole) Fire(e *logrus.Entry) error {
	msg := e.Message
	if scope, ok := e.Data["scope"]; ok {
		msg = "(" + scope.(string) + ") " + msg
	}

	switch e.Level {
	case logrus.PanicLevel:
		hook.logger.Panic(msg)
	case logrus.FatalLevel:
		hook.logger.Fatal(msg)
	case logrus.ErrorLevel:
		hook.logger.Error(msg)
	case logrus.WarnLevel:
		hook.logger.Warn(msg)
	case logrus.InfoLevel:
		hook.logger.Info(msg)
	case logrus.DebugLevel:
		hook.logger.Debug(msg)
	}

	return nil
}


text:
<?php
$this->includeCss('settings.css');
$this->includeJavascript('services.js');
?>

<form method="post" action="<?php $this->html('form.action'); ?>" class="extensions">

	<?php if ($this->get('extension.isScript')) : ?>

			<?php echo $this->msg('Extensions.form.name'); ?>

			<?php echo DekiForm::singleInput('text', 'name', $this->get('extension.name'), array('class' => 'long')); ?>

<?php echo $this->msg('Extensions.form.configuration'); ?>

			<?php echo $this->msg('Extensions.form.manifest'); ?>

			<?php echo DekiForm::singleInput('text', 'manifest', $this->get('extension.manifest'), array('class' => 'long')); ?>

			<?php echo DekiForm::singleInput('checkbox', 'debug', 'true', array('checked' => $this->get('extension.debug') == 'true'), $this->msg('Extensions.form.debug') ); ?>

			<?php echo DekiForm::singleInput('checkbox', 'pref_protected', 'true', array('checked' => $this->get('pref.protected')), $this->msg('Extensions.form.protected')); ?>

	<?php else : ?>

			<?php echo $this->msg('Extensions.form.type'); ?>

			<?php echo DekiForm::multipleInput('radio', 'init_type', $this->get('form.init-options'), $this->get('extension.init')); ?>

			<?php echo $this->msg('Extensions.form.name'); ?>

			<?php echo DekiForm::singleInput('text', 'name', $this->get('extension.name'), array('class' => 'long')); ?>

			<?php echo $this->msg('Extensions.form.sid'); ?> <?php echo $this->msg('Extensions.form.required'); ?>

			<?php echo DekiForm::singleInput('text', 'sid', $this->get('extension.sid'), array('class' => 'long')); ?>

			<?php echo $this->msg('Extensions.form.uri'); ?> <?php echo $this->msg('Extensions.form.required'); ?>

			<?php echo DekiForm::singleInput('text', 'uri', $this->get('extension.uri'), array('class' => 'long')); ?>

<?php echo $this->msg('Extensions.form.configuration'); ?>

			<?php echo DekiForm::singleInput('checkbox', 'pref_protected', 'true', array('checked' => $this->get('pref.protected')), $this->msg('Extensions.form.protected')); ?>

	<?php endif; ?>

		<?php $this->html('config-table'); ?>

<?php echo $this->msg('Extensions.form.preferences'); ?>

<?php echo $this->msg('Extensions.form.title'); ?>

			<?php echo DekiForm::singleInput('text', 'pref_title', $this->get('pref.title'), array('class' => 'short')); ?>

<?php echo $this->msg('Extensions.form.description'); ?>

			<?php echo DekiForm::singleInput('text', 'pref_description', $this->get('pref.description'), array('class' => 'short')); ?>

<?php echo $this->msg('Extensions.form.namespace'); ?>

			<?php echo DekiForm::singleInput('text', 'pref_namespace', $this->get('pref.namespace'), array('class' => 'short')); ?>

<?php echo $this->msg('Extensions.form.logouri'); ?>

			<?php echo DekiForm::singleInput('text', 'pref_logo_uri', $this->get('pref.logoUri'), array('class' => 'short')); ?>

<?php echo $this->msg('Extensions.form.functions'); ?>

			<?php echo DekiForm::singleInput('text', 'pref_functions', $this->get('pref.functions'), array('class' => 'short')); ?>

		<?php echo DekiForm::singleInput('button', 'action', 'save', array(), $this->get('extension.isScript') ? $this->msg('Extensions.add.script.save'): $this->msg('Extensions.add.extension.save')); ?>
		<?php echo $this->msgRaw('Extensions.form.cancel', $this->get('form.cancel')); ?>

</form>


text:
package cmd

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path"
	"sync"

	"golang.org/x/net/context"
	"golang.org/x/oauth2"
)

const (
	// auth scopes needed by the program
	scopeDriveReadOnly = "https://www.googleapis.com/auth/drive.readonly"

	// program credentials for installed apps
	googClient = "183908478743-e8rth9fbo7juk9eeivgp23asnt791g63.apps.googleusercontent.com"
	googSecret = "ljELuf5jUrzcOxZGL7OQfkIC"

	// token providers
	providerGoogle = "goog"
)

var (
	// OAuth2 configs for OOB flow
	authConfig = map[string]*oauth2.Config{
		providerGoogle: {
			ClientID:     googClient,
			ClientSecret: googSecret,
			Scopes:       []string{scopeDriveReadOnly},
			RedirectURL:  "urn:ietf:wg:oauth:2.0:oob",
			Endpoint: oauth2.Endpoint{
				AuthURL:  "https://accounts.google.com/o/oauth2/auth",
				TokenURL: "https://accounts.google.com/o/oauth2/token",
			},
		},
	}

	// reusable HTTP clients
	clientsMu sync.Mutex // guards clients
	clients   map[string]*http.Client
)

func init() {
	clients = make(map[string]*http.Client)
}

// driveClient returns an HTTP client which knows how to perform authenticated
// requests to Google Drive API.
func driveClient() (*http.Client, error) {
	clientsMu.Lock()
	defer clientsMu.Unlock()
	if hc, ok := clients[providerGoogle]; ok {
		return hc, nil
	}
	ts, err := tokenSource(providerGoogle)
	if err != nil {
		return nil, err
	}
	t := &oauth2.Transport{
		Source: ts,
		Base:   http.DefaultTransport,
	}
	hc := &http.Client{Transport: t}
	clients[providerGoogle] = hc
	return hc, nil
}

// tokenSource creates a new oauth2.TokenSource backed by tokenRefresher,
// using previously stored user credentials if available.
func tokenSource(provider string) (oauth2.TokenSource, error) {
	// Ignore provider if *authToken is given.
	if *authToken != "" {
		tok := &oauth2.Token{AccessToken: *authToken}
		return oauth2.StaticTokenSource(tok), nil
	}

	conf := authConfig[provider]
	if conf == nil {
		return nil, fmt.Errorf("no auth config for %q", provider)
	}
	t, err := readToken(provider)
	if err != nil {
		t, err = authorize(conf)
	}
	if err != nil {
		return nil, fmt.Errorf("unable to obtain access token for %q", provider)
	}
	cache := &cachedTokenSource{
		src:      conf.TokenSource(context.Background(), t),
		provider: provider,
		config:   conf,
	}
	return oauth2.ReuseTokenSource(nil, cache), nil
}

// authorize performs user authorization flow, asking for permissions grant.
func authorize(conf *oauth2.Config) (*oauth2.Token, error) {
	aurl := conf.AuthCodeURL("unused", oauth2.AccessTypeOffline)
	fmt.Printf("Authorize me at following URL, please:\n\n%s\n\nCode: ", aurl)
	var code string
	if _, err := fmt.Scan(&code); err != nil {
		return nil, err
	}
	return conf.Exchange(context.Background(), code)
}

// cachedTokenSource stores tokens returned from src on local disk.
// It is usually combined with oauth2.ReuseTokenSource.
type cachedTokenSource struct {
	src      oauth2.TokenSource
	provider string
	config   *oauth2.Config
}

func (c *cachedTokenSource) Token() (*oauth2.Token, error) {
	t, err := c.src.Token()
	if err != nil {
		t, err = authorize(c.config)
	}
	if err != nil {
		return nil, err
	}
	writeToken(c.provider, t)
	return t, nil
}

// readToken deserializes token from local disk.
func readToken(provider string) (*oauth2.Token, error) {
	l, err := tokenLocation(provider)
	if err != nil {
		return nil, err
	}
	b, err := ioutil.ReadFile(l)
	if err != nil {
		return nil, err
	}
	t := &oauth2.Token{}
	return t, json.Unmarshal(b, t)
}

// writeToken serializes token tok to local disk.
func writeToken(provider string, tok *oauth2.Token) error {
	l, err := tokenLocation(provider)
	if err != nil {
		return err
	}
	w, err := os.Create(l)
	if err != nil {
		return err
	}
	defer w.Close()
	b, err := json.MarshalIndent(tok, "", "  ")
	if err != nil {
		return err
	}
	_, err = w.Write(b)
	return err
}

// tokenLocation returns a local file path, suitable for storing user credentials.
func tokenLocation(provider string) (string, error) {
	d := homedir()
	if d == "" {
		log.Printf("WARNING: unable to identify user home dir")
	}
	d = path.Join(d, ".config", "claat")
	if err := os.MkdirAll(d, 0700); err != nil {
		return "", err
	}
	return path.Join(d, provider+"-cred.json"), nil
}

func homedir() string {
	if v := os.Getenv("HOME"); v != "" {
		return v
	}
	d, p := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH")
	if d != "" && p != "" {
		return d + p
	}
	return os.Getenv("USERPROFILE")
}


text:
namespace VRTK.Examples
{
    using UnityEngine;

    public class VRTK_ControllerInteract_ListenerExample : MonoBehaviour
    {
        private void Start()
        {
            if (GetComponent<VRTK_InteractTouch>() == null || GetComponent<VRTK_InteractGrab>() == null)
            {
                VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_ControllerInteract_ListenerExample", "VRTK_InteractTouch and VRTK_InteractGrab", "the Controller Alias"));
                return;
            }

            //Setup controller event listeners
            GetComponent<VRTK_InteractTouch>().ControllerTouchInteractableObject += new ObjectInteractEventHandler(DoInteractTouch);
            GetComponent<VRTK_InteractTouch>().ControllerUntouchInteractableObject += new ObjectInteractEventHandler(DoInteractUntouch);
            GetComponent<VRTK_InteractGrab>().ControllerGrabInteractableObject += new ObjectInteractEventHandler(DoInteractGrab);
            GetComponent<VRTK_InteractGrab>().ControllerUngrabInteractableObject += new ObjectInteractEventHandler(DoInteractUngrab);
        }

        private void DebugLogger(uint index, string action, GameObject target)
        {
            VRTK_Logger.Info("Controller on index '" + index + "' is " + action + " an object named " + target.name);
        }

        private void DoInteractTouch(object sender, ObjectInteractEventArgs e)
        {
            if (e.target)
            {
                DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "TOUCHING", e.target);
            }
        }

        private void DoInteractUntouch(object sender, ObjectInteractEventArgs e)
        {
            if (e.target)
            {
                DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "NO LONGER TOUCHING", e.target);
            }
        }

        private void DoInteractGrab(object sender, ObjectInteractEventArgs e)
        {
            if (e.target)
            {
                DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "GRABBING", e.target);
            }
        }

        private void DoInteractUngrab(object sender, ObjectInteractEventArgs e)
        {
            if (e.target)
            {
                DebugLogger(VRTK_ControllerReference.GetRealIndex(e.controllerReference), "NO LONGER GRABBING", e.target);
            }
        }
    }
}


text:
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace  Model
{

    /// <summary>
    ///
    /// </summary>
    [DataContract]
    public class Snapshot : IEquatable<Snapshot>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Snapshot" /> class.
        /// </summary>
        public Snapshot()
        {

        }

        /// <summary>
        /// The resource's unique identifier
        /// </summary>
        /// <value>The resource's unique identifier</value>
        [DataMember(Name = "id", EmitDefaultValue = false)]
        public string Id { get; set; }

        /// <summary>
        /// The type of object that has been created
        /// </summary>
        /// <value>The type of object that has been created</value>
        [DataMember(Name = "type", EmitDefaultValue = false)]
        public string Type { get; set; }

        /// <summary>
        /// URL to the object's representation (absolute path)
        /// </summary>
        /// <value>URL to the object's representation (absolute path)</value>
        [DataMember(Name = "href", EmitDefaultValue = false)]
        public string Href { get; set; }

        /// <summary>
        /// Backend managed resource metadata
        /// </summary>
        /// <value>Backend managed resource metadata</value>
        [DataMember(Name = "metadata", EmitDefaultValue = false)]
        public DatacenterElementMetadata Metadata { get; set; }

        /// <summary>
        /// Resource's properties
        /// </summary>
        /// <value>Resource's properties</value>
        [DataMember(Name = "properties", EmitDefaultValue = false)]
        public SnapshotProperties Properties { get; set; }

        public string Request { get; set; }

        /// <summary>
        /// Returns the string presentation of the object
        /// </summary>
        /// <returns>String presentation of the object</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append("class Snapshot {\n");
            sb.Append("  Id: ").Append(Id).Append("\n");
            sb.Append("  Type: ").Append(Type).Append("\n");
            sb.Append("  Href: ").Append(Href).Append("\n");
            sb.Append("  Metadata: ").Append(Metadata).Append("\n");
            sb.Append("  Properties: ").Append(Properties).Append("\n");

            sb.Append("}\n");
            return sb.ToString();
        }

        /// <summary>
        /// Returns the JSON string presentation of the object
        /// </summary>
        /// <returns>JSON string presentation of the object</returns>
        public string ToJson()
        {
            return JsonConvert.SerializeObject(this, Formatting.Indented);
        }

        /// <summary>
        /// Returns true if objects are equal
        /// </summary>
        /// <param name="obj">Object to be compared</param>
        /// <returns>Boolean</returns>
        public override bool Equals(object obj)
        {
            // credit: http://stackoverflow.com/a/10454552/677735
            return this.Equals(obj as Snapshot);
        }

        /// <summary>
        /// Returns true if Snapshot instances are equal
        /// </summary>
        /// <param name="other">Instance of Snapshot to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(Snapshot other)
        {
            // credit: http://stackoverflow.com/a/10454552/677735
            if (other == null)
                return false;

            return
                (
                    this.Id == other.Id ||
                    this.Id != null &&
                    this.Id.Equals(other.Id)
                ) &&
                (
                    this.Type == other.Type ||
                    this.Type != null &&
                    this.Type.Equals(other.Type)
                ) &&
                (
                    this.Href == other.Href ||
                    this.Href != null &&
                    this.Href.Equals(other.Href)
                ) &&
                (
                    this.Metadata == other.Metadata ||
                    this.Metadata != null &&
                    this.Metadata.Equals(other.Metadata)
                ) &&
                (
                    this.Properties == other.Properties ||
                    this.Properties != null &&
                    this.Properties.Equals(other.Properties)
                );
        }

        /// <summary>
        /// Gets the hash code
        /// </summary>
        /// <returns>Hash code</returns>
        public override int GetHashCode()
        {
            // credit: http://stackoverflow.com/a/263416/677735
            unchecked // Overflow is fine, just wrap
            {
                int hash = 41;
                // Suitable nullity checks etc, of course :)

                if (this.Id != null)
                    hash = hash * 59 + this.Id.GetHashCode();

                if (this.Type != null)
                    hash = hash * 59 + this.Type.GetHashCode();

                if (this.Href != null)
                    hash = hash * 59 + this.Href.GetHashCode();

                if (this.Metadata != null)
                    hash = hash * 59 + this.Metadata.GetHashCode();

                if (this.Properties != null)
                    hash = hash * 59 + this.Properties.GetHashCode();

                return hash;
            }
        }

    }

}


text:
create table "MANAGED_TABLE1" (col1 float, col2 float) using rcfile as
select
  sum(l_orderkey) as total1,
  avg(l_partkey) as total2
from
  lineitem
group by
  l_quantity
order by
  l_quantity
limit
  3;


text:
App.Collections.Dictionary = App.Backbone.Collection.extend({
	url: function() {
		return this.core.hosts.request + 'Dictionary/Get/';
	},
	model: App.Models.DictionaryItem,

	initialize: function() {
		this.name = null;
		this.callback = null;
	},

	processRequestData: function() {
		return {
			Name: this.name,
			LoadAll: YES
		};
	},

	parse: function(response, xhr) {
		if (response.Success) {
			return response.Result.Items;
		}
	}
});


text:
package org.smurn.jply;

/**
 * Data type of values stored in PLY files.
 */
public enum DataType {

    /** One byte signed integer. */
    CHAR,
    /** One byte unsigned integer. */
    UCHAR,
    /** Two byte signed integer. */
    SHORT,
    /** Two byte unsigned integer. */
    USHORT,
    /** Four byte signed integer. */
    INT,
    /** Four byte unsigned integer. */
    UINT,
    /** four byte floating point number. */
    FLOAT,
    /** Eight byte byte floating point number. */
    DOUBLE;

    /**
     * Parses the PLY name of a data type.
     * @param typeName Name of the data type.
     * @return Data type.
     */
    static DataType parse(final String typeName) {
        if (typeName == null) {
            throw new NullPointerException("typeName must not be null.");
        }
        if ("char".equals(typeName)) {
            return CHAR;
        } else if ("uchar".equals(typeName)) {
            return UCHAR;
        } else if ("short".equals(typeName)) {
            return SHORT;
        } else if ("ushort".equals(typeName)) {
            return USHORT;
        } else if ("int".equals(typeName)) {
            return INT;
        } else if ("uint".equals(typeName)) {
            return UINT;
        } else if ("float".equals(typeName)) {
            return FLOAT;
        } else if ("double".equals(typeName)) {
            return DOUBLE;
        } else if ("int8".equals(typeName)) {
            return CHAR;
        } else if ("uint8".equals(typeName)) {
            return UCHAR;
        } else if ("int16".equals(typeName)) {
            return SHORT;
        } else if ("uint16".equals(typeName)) {
            return USHORT;
        } else if ("int32".equals(typeName)) {
            return INT;
        } else if ("uint32".equals(typeName)) {
            return UINT;
        } else if ("float32".equals(typeName)) {
            return FLOAT;
        } else if ("float64".equals(typeName)) {
            return DOUBLE;
        } else {
            throw new IllegalArgumentException("Not a valid PLY data type.");
        }
    }
}


text:
import pytest

from selenium.webdriver.remote.remote_connection import RemoteConnection

def test_basic_auth(mocker):
    def check(request, timeout):
        assert request.headers['Authorization'] == 'Basic dXNlcjpwYXNz'

    try:
        method = mocker.patch('urllib.request.OpenerDirector.open')
    except ImportError:
        method = mocker.patch('urllib2.OpenerDirector.open')
    method.side_effect = check

    with pytest.raises(AttributeError):
        RemoteConnection('http://user:pass@remote', resolve_ip=False) \
            .execute('status', {})


text:
'use strict';

const loadRule = require('../utils/load-rule');
const ContextBuilder = require('../utils/contextBuilder');
const RequestBuilder = require('../utils/requestBuilder');

const ruleName = 'saml-configuration';

describe(ruleName, () => {
  let context;
  let rule;

  beforeEach(() => {
    rule = loadRule(ruleName);
  });

  describe('should do nothing', () => {
    beforeEach(() => {
      const request = new RequestBuilder().build();
      context = new ContextBuilder()
        .withRequest(request)
        .build();
    });

    it('if it isn`t correct client', (done) => {
      rule({}, context, (err, u, c) => {
        expect(err).toBeFalsy();
        expect(c.samlConfiguration).toEqual({});

        done();
      });
    });
  });

  describe('should set samlConfiguration', () => {
    beforeEach(() => {
      const request = new RequestBuilder().build();
      context = new ContextBuilder()
        .withRequest(request)
        .build();

      context.clientID = '{YOUR_SAMLP_OR_WSFED_CLIENT_ID}';
    });

    it('if it is correct client', (done) => {
      const expectedConfig = {
        audience: 'urn:foo',
        recipient: 'http://foo',
        destination: 'http://foo',
        lifetimeInSeconds: 3600
      };

      rule({}, context, (err, u, c) => {
        expect(err).toBeFalsy();
        expect(c.samlConfiguration).toEqual(expectedConfig);

        done();
      });
    });
  });
});


text:
Leeroy requires you to bring your own deployment scripts. This is actually pretty easy and allows you to run whatever you want. The output of your scripts will be saved to the build log and displayed on the webinterface.

If your scripts work when you run them manually it should also "just work" with Leeroy.

Your scripts get two arguments. The first one is the repository URL, the second one the branch name to which was pushed.

## Deploying to AWS Elastic Beanstalk
The following scripts demonstrates one way to deploy to AWS EB

     #! /bin/bash
     cd /home/ec2-user/test-deploy
     git fetch
     get reset --hard origin/master
     git aws.push


text:
class Samwise::Message

  # distribution strategies
  DIST_T = [:redundant, :roundrobin]

  # offers access to the underlying FFI::Struct
  attr_reader :pub_t

  ##
  # RabbitMQ options

  class Pub_options_t < FFI::Struct
    layout :content_type,     :pointer,
           :content_encoding, :pointer,
           :delivery_mode,    :pointer,
           :priority,         :pointer,
           :correlation_id,   :pointer,
           :reply_to,         :pointer,
           :expiration,       :pointer,
           :message_id,       :pointer,
           :type,             :pointer,
           :user_id,          :pointer,
           :app_id,           :pointer,
           :cluster_id,       :pointer
  end

  ##
  # AMQP headers

  class Pub_headers_t < FFI::Struct
    layout :keys,   :pointer,
           :values, :pointer,
           :amount, :size_t
  end

  ##
  # Layout of the samwise_pub_t struct

  class Pub_t < FFI::Struct
    layout :disttype,    :int,
           :distcount,   :int,

           :exchange,    :pointer,
           :routing_key, :pointer,
           :mandatory,   :int,
           :immediate,   :int,

           :options,     Pub_options_t,
           :headers,     Pub_headers_t,

           :msg,         :pointer,
           :size,        :size_t
  end

  #
  #  generate getter/setter for first-level attributes
  #

  attr_reader :exchange, :routing_key, :mandatory, :immediate

  ["exchange=", "routing_key="].each do |meth|
    define_method meth do |val|
      key = meth[0..-2]
      set_ptr @pub_t, key.to_sym, val
      instance_variable_set "@#{key}", val
    end
  end

  ["mandatory=", "immediate="].each do |meth|
    define_method meth do |val|
      key = meth[0..-2]
      set_int @pub_t, key.to_sym, val
      instance_variable_set "@#{key}", val
    end
  end

  #
  #  headers and options
  #
  attr_reader :options, :headers

  ##
  # set AMQP options

  def options= options
    options.each do |key, val|
      set_ptr @pub_t[:options], key, val
    end

    @options = options
  end

  ##
  # set AMQP headers

  def headers= headers
    @pub_t[:headers][:amount] = size = headers.size

    keys = FFI::MemoryPointer.new :pointer, size
    vals = FFI::MemoryPointer.new :pointer, size

    # instead of using #keys and #values b/c I don't know ruby well
    # enough to be sure that the order is maintained
    pointer = { keys: [], vals: [] }
    headers.each do |key, val|
      pointer[:keys] << key
      pointer[:vals] << val
    end

    pointer[:keys] = pointer[:keys].collect do |key|
      FFI::MemoryPointer.from_string key.to_s
    end

    pointer[:vals] = pointer[:vals].collect do |val|
      FFI::MemoryPointer.from_string val
    end

    keys.write_array_of_pointer pointer[:keys]
    vals.write_array_of_pointer pointer[:vals]

    @pub_t[:headers][:keys] = keys
    @pub_t[:headers][:values] = vals

    @headers = headers
  end

  ##
  # Constructor accepts a string containing the message and the
  # distribution strategy

  def initialize msg, disttype=:roundrobin, distcount=nil

    raise "invalid disttype" unless DIST_T.include? disttype

    @pub_t = Pub_t.new

    # set payload
    set_ptr @pub_t, :msg, msg
    @pub_t[:size] = msg.size

    # set distribution strategy
    @pub_t[:disttype] = (disttype == :roundrobin)? 0: 1;
    @pub_t[:distcount] = distcount.nil?? 0: distcount;
  end

  private

  ##
  # Sets a pointer to a string as a structs property

  def set_ptr struct, key, val
    struct[key]= FFI::MemoryPointer.from_string val
  end

  ##
  # Sets an integer as a structs property

  def set_int struct, key, val
    struct[key] = val
  end

end


text:
package idemix

import (
	amcl "github.com/hyperledger/fabric-amcl/core"
	"github.com/hyperledger/fabric-amcl/core/FP256BN"
	"github.com/pkg/errors"
)

// nonRevokedProver is the Prover of the ZK proof system that handles revocation.
type nonRevokedProver interface {
	// getFSContribution returns the non-revocation contribution to the Fiat-Shamir hash, forming the challenge of the ZKP,
	getFSContribution(rh *FP256BN.BIG, rRh *FP256BN.BIG, cri *CredentialRevocationInformation, rng *amcl.RAND) ([]byte, error)

	// getNonRevokedProof returns a proof of non-revocation with the respect to passed challenge
	getNonRevokedProof(chal *FP256BN.BIG) (*NonRevocationProof, error)
}

// nopNonRevokedProver is an empty nonRevokedProver
type nopNonRevokedProver struct{}

func (prover *nopNonRevokedProver) getFSContribution(rh *FP256BN.BIG, rRh *FP256BN.BIG, cri *CredentialRevocationInformation, rng *amcl.RAND) ([]byte, error) {
	return nil, nil
}

func (prover *nopNonRevokedProver) getNonRevokedProof(chal *FP256BN.BIG) (*NonRevocationProof, error) {
	ret := &NonRevocationProof{}
	ret.RevocationAlg = int32(ALG_NO_REVOCATION)
	return ret, nil
}

// getNonRevocationProver returns the nonRevokedProver bound to the passed revocation algorithm
func getNonRevocationProver(algorithm RevocationAlgorithm) (nonRevokedProver, error) {
	switch algorithm {
	case ALG_NO_REVOCATION:
		return &nopNonRevokedProver{}, nil
	default:
		// unknown revocation algorithm
		return nil, errors.Errorf("unknown revocation algorithm %d", algorithm)
	}
}